package com.onaro.sanscreen.client.view.tabular.value; import java.text.Collator; import org.apache.commons.lang3.StringUtils; /** * Parses names of the format fc1/1. This class is immutable and can be cached. */ public class SwitchTableValue implements TableValue { private final Object display; private final String prefix; private final String suffix; private final int blade; private final int port; public SwitchTableValue(String prefix, int blade, int port, String suffix, Object display) { this.blade = blade; this.display = display; this.port = port; this.prefix = prefix; this.suffix = suffix; } public SwitchTableValue(String prefix, int port, String suffix, Object display) { this(prefix, 0, port, suffix, display); } public Object getDisplay() { return display != null ? display : StringUtils.EMPTY; } public int getBlade() { return blade; } public int getPort() { return port; } public String getPrefix() { return prefix; } public String getSuffix() { return suffix; } @Override public int compareTo (TableValue o) { if (o instanceof SwitchTableValue) { SwitchTableValue other = (SwitchTableValue) o; int comp = getPrefix().compareTo(other.getPrefix()); if (comp != 0) return comp; comp = getBlade() - other.getBlade(); if (comp != 0) return comp; comp = getPort() - other.getPort(); if (comp != 0) return comp; String suffixThis = getSuffix(); if (suffixThis == null) suffixThis = StringUtils.EMPTY; String suffixOther = other.getSuffix(); if (suffixOther == null) suffixOther = StringUtils.EMPTY; // Use natural language sort order. return Collator.getInstance().compare(suffixThis, suffixOther); } 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(); } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof SwitchTableValue)) return false; final SwitchTableValue switchTableValue = (SwitchTableValue) o; if (blade != switchTableValue.blade) return false; if (port != switchTableValue.port) return false; if (prefix != null ? !prefix.equals(switchTableValue.prefix) : switchTableValue.prefix != null) return false; if (suffix != null ? !suffix.equals(switchTableValue.suffix) : switchTableValue.suffix != null) return false; return true; } public int hashCode() { int result; result = (prefix != null ? prefix.hashCode() : 0); result = 29 * result + (suffix != null ? suffix.hashCode() : 0); result = 29 * result + blade; result = 29 * result + port; return result; } }