View Javadoc

1   package org.sf.jlaunchpad.util;
2   
3   import java.util.HashMap;
4   import java.text.MessageFormat;
5   
6   /**
7    * Helper class for the format attribute.
8    *
9    * @since 1.7
10   */
11  public class FormatElement {
12    /**
13     * The selected value in this enumeration.
14     */
15    protected String value;
16  
17    private static HashMap<String, MessageFormat> formatMap = new HashMap<String, MessageFormat>();
18    private static final String CHECKSUM = "CHECKSUM";
19    private static final String MD5SUM = "MD5SUM";
20    private static final String SVF = "SVF";
21  
22    static {
23      formatMap.put(CHECKSUM, new MessageFormat("{0}"));
24      formatMap.put(MD5SUM, new MessageFormat("{0} *{1}"));
25      formatMap.put(SVF, new MessageFormat("MD5 ({1}) = {0}"));
26    }
27  
28    /**
29     * Constructor for FormatElement
30     */
31    public FormatElement() {
32      super();
33    }
34  
35    /**
36     * @param value the <code>String</code> value of the attribute
37     */
38    public final void setValue(String value) {
39    //  int idx = indexOfValue(value);
40  /*        if (idx == -1) {
41              throw new BuildException(value + " is not a legal value for this attribute");
42          }
43  */
44      //int index = idx;
45      this.value = value;
46    }
47  
48    /**
49     * get the index of a value in this enumeration.
50     *
51     * @param value the string value to look for.
52     * @return the index of the value in the array of strings
53     *         or -1 if it cannot be found.
54     * @see #getValues()
55     */
56    public final int indexOfValue(String value) {
57      String[] values = getValues();
58      if (values == null || value == null) {
59        return -1;
60      }
61      for (int i = 0; i < values.length; i++) {
62        if (value.equals(values[i])) {
63          return i;
64        }
65      }
66      return -1;
67    }
68  
69    /**
70     * @return the selected value.
71     */
72    public final String getValue() {
73      return value;
74    }
75  
76    /**
77     * Get the default value - CHECKSUM.
78     *
79     * @return the defaul value.
80     */
81    public static FormatElement getDefault() {
82      FormatElement e = new FormatElement();
83      e.setValue(CHECKSUM);
84      return e;
85    }
86  
87    /**
88     * Convert this enumerated type to a <code>MessageFormat</code>.
89     *
90     * @return a <code>MessageFormat</code> object.
91     */
92    public MessageFormat getFormat() {
93      return formatMap.get(getValue());
94    }
95  
96    /**
97     * Get the valid values.
98     *
99     * @return an array of values.
100    */
101   public String[] getValues() {
102     return new String[]{CHECKSUM, MD5SUM, SVF};
103   }
104 
105 }