View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.attribute.provider;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  import org.opensaml.xml.util.DatatypeHelper;
25  
26  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
27  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
28  
29  /**
30   * An attribute implementation that operates on simple value types.
31   * 
32   * @param <ValueType> value type
33   */
34  public class BasicAttribute<ValueType> extends BaseAttribute<ValueType> implements Cloneable {
35  
36      /** ID of this attribute. */
37      private String id;
38  
39      /** Map of attribute encoders for this attribute, keyed off of category. */
40      private ArrayList<AttributeEncoder> encoders;
41  
42      /** Set of values for this attribute. */
43      private Collection<ValueType> values;
44  
45      /** Comparator for this attribute. */
46      private Comparator<ValueType> comparator;
47  
48      /** Constructor. */
49      public BasicAttribute() {
50          encoders = new ArrayList<AttributeEncoder>(3);
51          values = new ArrayList<ValueType>(5);
52      }
53  
54      /**
55       * Constructor.
56       * 
57       * @param attributeId the ID of this attribute
58       */
59      public BasicAttribute(String attributeId) {
60          id = DatatypeHelper.safeTrimOrNullString(attributeId);
61          encoders = new ArrayList<AttributeEncoder>();
62          values = new ArrayList<ValueType>(5);
63      }
64  
65      /** {@inheritDoc} */
66      public List<AttributeEncoder> getEncoders() {
67          return encoders;
68      }
69  
70      /** {@inheritDoc} */
71      public String getId() {
72          return id;
73      }
74  
75      /**
76       * Set id of this attribute.
77       * 
78       * @param newID new ID
79       */
80      public void setId(String newID) {
81          id = newID;
82      }
83  
84      /** {@inheritDoc} */
85      public Comparator<ValueType> getValueComparator() {
86          return comparator;
87      }
88  
89      /**
90       * Set value comparator for this attribute.
91       * 
92       * @param newComparator new value comparator
93       */
94      public void setValueComparator(Comparator<ValueType> newComparator) {
95          comparator = newComparator;
96      }
97  
98      /** {@inheritDoc} */
99      public Collection<ValueType> getValues() {
100         return values;
101     }
102 
103     /**
104      * Replace the current set of values with the given set.
105      * 
106      * @param newValues new values to replace existing ones
107      */
108     public void setValues(Collection<ValueType> newValues) {
109         values = newValues;
110     }
111 
112     /** {@inheritDoc} */
113     public BasicAttribute<ValueType> clone() {
114         BasicAttribute<ValueType> newAttribute = new BasicAttribute<ValueType>();
115 
116         newAttribute.setId(getId());
117 
118         newAttribute.setValueComparator(this.getValueComparator());
119 
120         newAttribute.getValues().addAll(getValues());
121 
122         newAttribute.getEncoders().addAll(getEncoders());
123 
124         return newAttribute;
125     }
126 }