package com.onaro.sanscreen.client.view.common.functions; import com.onaro.client.leekui.runtime.OnaroAdapterUtils; import com.onaro.commons.util.TextAndNumbersString; import com.onaro.commons.util.WwnComparable; import com.onaro.sanscreen.client.view.tabular.columns.AbstractColumnValueRetriever; import com.onaro.sanscreen.server.interfaces.data.inventory.Port; import org.apache.commons.lang3.StringUtils; import org.eclipse.core.runtime.IAdaptable; /** * Retriever for port names. This will calculate the display text based on the availability of the Port.name value, * substituting with wwn if necessary. Much of the complexity is in the implementation of the Comparable interface. * The WWN substitution is treated as a special case (sorts lowest; then by the hex values). Otherwise, for * comparing, this class assumes all port names, if any, to be of the format [text][number][text][number]...

* * Thus the names are compared against corresponding parts. */ public class PortNameRetriever extends AbstractColumnValueRetriever { @Override public Class getValueType() { return Object.class; } @Override public Object getValue(IAdaptable rowObj) { Port port = OnaroAdapterUtils.getAdapter(rowObj, Port.class); return getValue(port); } // Broken out as a separate method so it can be used outside of a column retriever public Object getValue(Port port) { return new PortName(port); } private static class PortName implements Comparable { private final TextAndNumbersString name; private final WwnComparable wwn; public PortName(Port port) { String portName = port.getName(); this.name = StringUtils.isEmpty(portName)? null : new TextAndNumbersString(portName); this.wwn = new WwnComparable(port.getWwn()); } @Override public int compareTo(PortName o) { // wwn's sort first, so long as it is in a :... format: if (shouldCompareByWwn()) { if (o.shouldCompareByWwn()) { return wwn.compareTo(o.wwn); } return -1; } if (o.shouldCompareByWwn()) { return 1; } // Compare by name (text or non-standard-format WWN). if (name != null) { return name.compareTo(o.name); } return o.name == null ? 0 : -1; } public String toString() { return isNameWwn() ? wwn.toString() : name.toString(); } private boolean isNameWwn() { return name == null; } private boolean shouldCompareByWwn() { return isNameWwn() && wwn.isStandardFormat(); } } }