package com.onaro.sanscreen.client.view.annotations; import java.text.Collator; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.lang3.StringUtils; import com.onaro.commons.util.SwingCollectionPrinter; import com.onaro.sanscreen.server.interfaces.data.inventory.AutoTierPolicyConstraint; /** * Class containing miscellaneous methods working with AutoTierPolicy objects. */ public class AutoTierPolicyUtils { /* * Printer used to construct a single string from a set of policy constraints. */ private static SwingCollectionPrinter printer = new SwingCollectionPrinter(); /** * Return a string representation (suitable for the UI) of a Collection of AutoTierPolicyConstraints. The output string would * be a comma-separated list of constraints where of the format "% , ...". For example: "75% Gold, 25% Silver" * * @return a string representation (suitable for the UI) of a Collection of AutoTierPolicyConstraints. */ public static final String toString(Collection constraints) { if(constraints != null && constraints.size() > 0) { Collection constraintHolders = new ArrayList(constraints.size()); /* Create a collection of AutoTierPolicyConstraintHolder objects for the SwingCollectionPrinter to use. We need this since the * AutoTierPolicyConstraint object is a server object and we don't have control over its toString() method that SwingCollectionPrinter uses. */ for(AutoTierPolicyConstraint constraint : constraints) { constraintHolders.add(new AutoTierPolicyConstraintHolder(constraint)); } return(printer.toStringNoSort(constraintHolders)); } else { return StringUtils.EMPTY; } } /** * @return a string representation (suitable for the UI) of a AutoTierPolicyConstraint. The format of the output string is: xx% . * For example, "75% Gold". */ public static final String toString(AutoTierPolicyConstraint constraint) { return(new AutoTierPolicyConstraintHolder(constraint).toString()); } /** * Returns the policy name without any constraints in the string. The input string would be something like "(75% Gold, 25% Silver)". * This would return "". For example: "My Gold Policy(75% Gold, 25% Silver)" becomes "My Gold Policy". * * @param policyNameWithConstraints a string containing the policy name followed optionally by the constraints details. * * @return the policy name without any constraints in the string. */ public static final String getPolicyName(String policyNameWithConstraints) { return(StringUtils.substringBefore(policyNameWithConstraints, "(")); //$NON-NLS-1$ } /* * Class that is used to contain a DynamicPolicyConstraint. This class implements Comparable and provides a toString() * method so that DynamicPolicyConstraint objects can be formatted by our SwingCollectionPrinter class. */ private static class AutoTierPolicyConstraintHolder implements Comparable { private AutoTierPolicyConstraint constraint; public AutoTierPolicyConstraintHolder(AutoTierPolicyConstraint constraint) { this.constraint = constraint; } @Override public int compareTo(AutoTierPolicyConstraintHolder constraintHolder) { // Sort in natural language order. return Collator.getInstance().compare(constraint.getVendorTier(), constraintHolder.constraint.getVendorTier()); } @Override /** * Override used by SwingCollectionPrinter to construct values for a policy constraint. The format of the output string is: xx% . * For example, "75% Gold". */ public String toString() { StringBuilder str = new StringBuilder(); str.append(constraint.getLimitPercent()); str.append('%'); str.append(' '); str.append(constraint.getVendorTier()); return(str.toString()); } } }