package com.onaro.sanscreen.client.view.tabular.value; import java.util.Comparator; import org.apache.commons.lang3.StringUtils; public class ComparatorTableValue implements TableValue{ private final Comparator comparator; private final Object display; /** * * @param comparator should be able to handle null values. */ public ComparatorTableValue(Comparator comparator, Object display) { this.comparator = comparator; this.display = display; } public Object getDisplay() { return display != null ? display : StringUtils.EMPTY; } @Override public int compareTo (TableValue o) { return comparator.compare(this.getDisplay(), o.getDisplay()); } /** * Since this method is used by he grouping-table, it returns the display value. * @return Return's the same value as {@link TableValue#getDisplay} does */ public String toString() { return getDisplay().toString(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((comparator == null) ? 0 : comparator.hashCode()); result = prime * result + ((display == null) ? 0 : display.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ComparatorTableValue other = (ComparatorTableValue) obj; if (comparator == null) { if (other.comparator != null) return false; } else if (!comparator.equals(other.comparator)) return false; if (display == null) { if (other.display != null) return false; } else if (!display.equals(other.display)) return false; return true; } }