package com.onaro.sanscreen.client.view.tabular.value; import java.text.Collator; import org.apache.commons.lang3.StringUtils; /** * Parses names of the format Bay 2, Card 1 Port A. This class is immutable and can be cached. */ public class IBMTableValue implements TableValue{ private final Object display; private final int bay; private final int card; private final String suffix; public IBMTableValue(Object display, int bay, int card, String suffix) { this.bay = bay; this.card = card; this.display = display; this.suffix = suffix; } public Object getDisplay() { return display != null ? display : StringUtils.EMPTY; } public int getBay() { return bay; } public int getCard() { return card; } public String getSuffix() { return suffix; } @Override public int compareTo (TableValue o) { if (o instanceof IBMTableValue) { IBMTableValue other = (IBMTableValue) o; int bayCompare = getBay() - other.getBay(); if(bayCompare == 0){ int cardCompare = getCard() - other.getCard(); if(cardCompare == 0){ return getSuffix().compareTo(other.getSuffix()); }else{ return cardCompare; } }else{ return bayCompare; } } 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 + bay; result = prime * result + card; result = prime * result + ((suffix == null) ? 0 : suffix.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; IBMTableValue other = (IBMTableValue) obj; if (bay != other.bay) return false; if (card != other.card) return false; if (suffix == null) { if (other.suffix != null) return false; } else if (!suffix.equals(other.suffix)) return false; if (display == null) { if (other.display != null) return false; } else if (!display.equals(other.display)) return false; return true; } }