package com.onaro.sanscreen.client.view.common.columns; import com.onaro.commons.util.ImmutableCache; import com.onaro.commons.util.Unit; import com.onaro.sanscreen.client.view.common.renderers.CapacityValueRenderer; import com.onaro.sanscreen.client.view.tabular.AbstractTabularColumn; import com.onaro.sanscreen.client.view.tabular.TabularPropertyColumn; import com.onaro.sanscreen.client.view.tabular.value.CapacityValue; import com.onaro.sanscreen.client.view.tabular.value.NumberValue; import com.onaro.sanscreen.util.Constant; import com.onaro.util.Units; import com.onaro.util.jfc.tables.filter.NumberColumnFilter; /** * Configuration options assisting in the creation of CapacityValue objects in Capacity Columns. * * This class is immutable and uses an ImmutableCache to cache created instances. */ public final class CapacityColumnOptions { private final Unit sourceUnit; private final Unit displayUnit; private final boolean showZero; private final String format; private final String unitsText; public static CapacityColumnOptions getOptions(Unit sourceUnit, Unit displayUnit, boolean showUnits, boolean showZero) { CapacityColumnOptions options = new CapacityColumnOptions(sourceUnit, displayUnit, showUnits, showZero); // Get the cached instance of the given options return ImmutableCache.getObject(options, CapacityColumnOptions.class); } public static CapacityColumnOptions getOptions(Unit sourceUnit, Unit displayUnit, boolean showUnits, boolean showZero, String format) { CapacityColumnOptions options = new CapacityColumnOptions(sourceUnit, displayUnit, showUnits, showZero, format); // Get the cached instance of the given options return ImmutableCache.getObject(options, CapacityColumnOptions.class); } private CapacityColumnOptions(Unit sourceUnit, Unit displayUnit, boolean showUnits, boolean showZero) { this(sourceUnit, displayUnit, showUnits, showZero, (sourceUnit == displayUnit ? CapacityValue.FORMAT_INTEGER : CapacityValue.FORMAT_DOUBLE)); } private CapacityColumnOptions(Unit sourceUnit, Unit displayUnit, boolean showUnits, boolean showZero, String format) { this.sourceUnit = sourceUnit; this.displayUnit = displayUnit; this.showZero = showZero; this.format = format; this.unitsText = showUnits ? Units.getUnitDisplay(displayUnit) : null; } public static void initializeColumn(AbstractTabularColumn column) { column.setValueType(NumberValue.class); column.setFilter(new NumberColumnFilter()); column.setCellRenderer(new CapacityValueRenderer()); } public static void initializeColumn(TabularPropertyColumn column) { column.setValueType(NumberValue.class); column.setFilter(new NumberColumnFilter()); column.setCellRenderer(new CapacityValueRenderer()); } /** * Get the appropriate CapacityValue based on the configuration of the options. * @param value the number value of the capacity, in the units matching sourceUnit (may be null) * @param identifier the identifier for the capacity value (may be null) * @return the CapacityValue instance, or null when appropriate */ public CapacityValue getCapacityValue(Number value, String identifier) { if (Constant.isUndeterminedCapacity(value) || (!showZero && value.doubleValue() == 0.0)) { return null; } return new CapacityValue(value.doubleValue(), identifier, sourceUnit, displayUnit, unitsText, format); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((displayUnit == null) ? 0 : displayUnit.hashCode()); result = prime * result + ((format == null) ? 0 : format.hashCode()); result = prime * result + (showZero ? 1231 : 1237); result = prime * result + ((sourceUnit == null) ? 0 : sourceUnit.hashCode()); result = prime * result + ((unitsText == null) ? 0 : unitsText.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; CapacityColumnOptions other = (CapacityColumnOptions) obj; if (displayUnit == null) { if (other.displayUnit != null) return false; } else if (!displayUnit.equals(other.displayUnit)) return false; if (format == null) { if (other.format != null) return false; } else if (!format.equals(other.format)) return false; if (showZero != other.showZero) return false; if (sourceUnit == null) { if (other.sourceUnit != null) return false; } else if (!sourceUnit.equals(other.sourceUnit)) return false; if (unitsText == null) { if (other.unitsText != null) return false; } else if (!unitsText.equals(other.unitsText)) return false; return true; } }