package com.onaro.sanscreen.client.view.tabular.value; import java.text.Collator; import org.apache.commons.lang3.StringUtils; /** * Parses names of the format SP A Port: 2. This class is immutable and can be cached. */ public class EMCTableValue implements TableValue{ private final Object display; private final String prefix; private final int port; public EMCTableValue(String prefix, int port, Object display) { this.display = display; this.port = port; this.prefix = prefix; } public Object getDisplay() { return display != null ? display : StringUtils.EMPTY; } public int getPort() { return port; } public String getPrefix() { return prefix; } @Override public int compareTo (TableValue o) { if (o instanceof EMCTableValue) { EMCTableValue other = (EMCTableValue) o; int prefixCompare = getPrefix().compareTo(other.getPrefix()); if (prefixCompare == 0) { return getPort() - other.getPort(); } else { return prefixCompare; } } else { // Use natural language sort order. return Collator.getInstance().compare(getDisplay().toString(), (o.getDisplay()).toString()); } } /** * 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 + port; result = prime * result + ((prefix == null) ? 0 : prefix.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; EMCTableValue other = (EMCTableValue) obj; if (port != other.port) return false; if (prefix == null) { if (other.prefix != null) return false; } else if (!prefix.equals(other.prefix)) return false; if (display == null) { if (other.display != null) return false; } else if (!display.equals(other.display)) return false; return true; } }