View Javadoc

1   /*
2    * $Id: PropertiesRepository.java,v 1.3 2008/09/09 12:50:47 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.component;
5   
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   /**
10   * 
11   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
12   * @version $Revision: 1.3 $ $Date: 2008/09/09 12:50:47 $
13   */
14  public class PropertiesRepository extends Properties {
15      private static final String REVISION = "$Revision: 1.3 $";
16  
17      private static final float MAP_LOAD_FACTOR = 0.9f;
18  
19      private static Map propertyToKey;
20  
21      private static Map keyToProperty;
22  
23      public static void declareProperties(String properties[]) {
24  
25          propertyToKey = new HashMap(properties.length, MAP_LOAD_FACTOR);
26          keyToProperty = new HashMap(properties.length, MAP_LOAD_FACTOR);
27  
28          for (int i = 0; i < properties.length; i++) {
29              String property = properties[i];
30  
31              Integer previousKey = (Integer) propertyToKey.get(property);
32  
33              Integer key = (Integer) computeKey(property);
34  
35              if (previousKey != null) {
36                  if (previousKey.equals(key)) {
37                      continue;
38                  }
39  
40                  throw new IllegalStateException(
41                          "Same property, different key '" + property + "'='"
42                                  + previousKey + "'/'" + key + "'");
43              }
44  
45              for (;;) {
46                  Integer old = (Integer) keyToProperty.put(key, property);
47                  if (old == null || old.equals(key)) {
48                      break;
49                  }
50  
51                  // key = new Integer(key.intValue() + 1);
52                  throw new IllegalStateException("Duplicate key for property '"
53                          + property + "'.");
54              }
55              propertyToKey.put(property, key);
56          }
57      }
58  
59      private static Object computeKey(String property) {
60          return new Integer(property.hashCode());
61      }
62  
63      public static Object getKey(String property) {
64          return propertyToKey.get(property);
65      }
66  
67      public static String getPropertyFromKey(Object key) {
68          return (String) keyToProperty.get(key);
69      }
70  }