View Javadoc

1   /*
2    * ====================
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4    * 
5    * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.     
6    * 
7    * The contents of this file are subject to the terms of the Common Development 
8    * and Distribution License("CDDL") (the "License").  You may not use this file 
9    * except in compliance with the License.
10   * 
11   * You can obtain a copy of the License at 
12   * http://IdentityConnectors.dev.java.net/legal/license.txt
13   * See the License for the specific language governing permissions and limitations 
14   * under the License. 
15   * 
16   * When distributing the Covered Code, include this CDDL Header Notice in each file
17   * and include the License file at identityconnectors/legal/license.txt.
18   * If applicable, add the following below this CDDL Header, with the fields 
19   * enclosed by brackets [] replaced by your own identifying information: 
20   * "Portions Copyrighted [year] [name of copyright owner]"
21   * ====================
22   */
23  package org.identityconnectors.framework.impl.api;
24  
25  import java.text.MessageFormat;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Comparator;
30  import java.util.HashSet;
31  import java.util.LinkedHashMap;
32  import java.util.List;
33  
34  import org.identityconnectors.common.CollectionUtil;
35  import org.identityconnectors.framework.api.ConfigurationProperties;
36  import org.identityconnectors.framework.api.ConfigurationProperty;
37  
38  
39  public class ConfigurationPropertiesImpl implements ConfigurationProperties {
40  
41      // =======================================================================
42      // Fields
43      // =======================================================================
44      /**
45       * Properties, listed in order by their "order" attribute
46       */
47      LinkedHashMap<String, ConfigurationPropertyImpl> _properties;
48  
49      /**
50       * The container. Not serialized in this object. Set when this
51       * property is added to parent
52       */
53      private transient APIConfigurationImpl _parent;
54      
55      // =======================================================================
56      // Internal Methods
57      // =======================================================================
58          
59      public APIConfigurationImpl getParent() {
60          return _parent;
61      }
62      
63      public void setParent(APIConfigurationImpl parent) {
64          _parent = parent;
65      }
66      
67      public void setProperties(Collection<ConfigurationPropertyImpl> in) {
68          List<ConfigurationPropertyImpl> properties =
69             new ArrayList<ConfigurationPropertyImpl>(in);
70          Collections.sort(properties, new Comparator<ConfigurationPropertyImpl>() {
71              public int compare(ConfigurationPropertyImpl o1,
72                      ConfigurationPropertyImpl o2) {
73                  int or1 = o1.getOrder();
74                  int or2 = o2.getOrder();
75                  return or1 < or2 ? -1 : or1 > or2 ? 1 : 0;
76              }
77          });
78          LinkedHashMap<String, ConfigurationPropertyImpl> temp =
79              new LinkedHashMap<String, ConfigurationPropertyImpl>();
80          for (ConfigurationPropertyImpl property : properties) {
81              temp.put(property.getName(), property);
82              property.setParent(this);
83          }
84          _properties = temp;
85      }
86      
87      public Collection<ConfigurationPropertyImpl> getProperties() {
88          return _properties.values();
89      }
90      
91          
92      // =======================================================================
93      // Interface Methods
94      // =======================================================================
95      /**
96       * {@inheritDoc}
97       */
98      public ConfigurationProperty getProperty(String name) {
99          return _properties.get(name);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     public List<String> getPropertyNames() {
106         List<String> names = new ArrayList<String>(_properties.keySet());
107         return CollectionUtil.newReadOnlyList(names);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public void setPropertyValue(String name, Object value) {
114         ConfigurationPropertyImpl property = _properties.get(name);
115         if (property == null) {
116             final String TMPL = "Property ''{0}'' does not exist.";
117             final String MSG = MessageFormat.format(TMPL, name);
118             throw new IllegalArgumentException(MSG);
119         }
120         property.setValue(value);
121     }
122     
123     public boolean equals(Object o) {
124         if ( o instanceof ConfigurationPropertiesImpl ) {
125             ConfigurationPropertiesImpl other = (ConfigurationPropertiesImpl)o;
126             HashSet<ConfigurationPropertyImpl> set1 = 
127                 new HashSet<ConfigurationPropertyImpl>(
128                         _properties.values());
129             HashSet<ConfigurationPropertyImpl> set2 = 
130                 new HashSet<ConfigurationPropertyImpl>(
131                         other._properties.values());
132             return set1.equals(set2);
133         }
134         return false;
135     }
136     
137     public int hashCode() {
138         HashSet<ConfigurationPropertyImpl> set1 = 
139             new HashSet<ConfigurationPropertyImpl>(
140                     _properties.values());
141         return set1.hashCode();
142     }
143 }