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   * <strong>NOTE:</strong> many plugins will use the {@link Object#toString()} method on an attribute's 
33   * values.  Therefore any value should return something reasonable for that method and what is returned 
34   * should be very stable across versions.
35   * 
36   * @param <ValueType> value type
37   */
38  public class BasicAttribute<ValueType> extends BaseAttribute<ValueType> implements Cloneable {
39  
40      /** ID of this attribute. */
41      private String id;
42  
43      /** Map of attribute encoders for this attribute, keyed off of category. */
44      private ArrayList<AttributeEncoder> encoders;
45  
46      /** Set of values for this attribute. */
47      private Collection<ValueType> values;
48  
49      /** Comparator for this attribute. */
50      private Comparator<ValueType> comparator;
51  
52      /** Constructor. */
53      public BasicAttribute() {
54          encoders = new ArrayList<AttributeEncoder>(3);
55          values = new ArrayList<ValueType>(5);
56      }
57  
58      /**
59       * Constructor.
60       * 
61       * @param attributeId the ID of this attribute
62       */
63      public BasicAttribute(String attributeId) {
64          id = DatatypeHelper.safeTrimOrNullString(attributeId);
65          encoders = new ArrayList<AttributeEncoder>();
66          values = new ArrayList<ValueType>(5);
67      }
68  
69      /** {@inheritDoc} */
70      public List<AttributeEncoder> getEncoders() {
71          return encoders;
72      }
73  
74      /** {@inheritDoc} */
75      public String getId() {
76          return id;
77      }
78  
79      /**
80       * Set id of this attribute.
81       * 
82       * @param newID new ID
83       */
84      public void setId(String newID) {
85          id = newID;
86      }
87  
88      /** {@inheritDoc} */
89      public Comparator<ValueType> getValueComparator() {
90          return comparator;
91      }
92  
93      /**
94       * Set value comparator for this attribute.
95       * 
96       * @param newComparator new value comparator
97       */
98      public void setValueComparator(Comparator<ValueType> newComparator) {
99          comparator = newComparator;
100     }
101 
102     /** {@inheritDoc} */
103     public Collection<ValueType> getValues() {
104         return values;
105     }
106 
107     /**
108      * Replace the current set of values with the given set.
109      * 
110      * @param newValues new values to replace existing ones
111      */
112     public void setValues(Collection<ValueType> newValues) {
113         values = newValues;
114     }
115 
116     /** {@inheritDoc} */
117     public BasicAttribute<ValueType> clone() {
118         BasicAttribute<ValueType> newAttribute = new BasicAttribute<ValueType>();
119 
120         newAttribute.setId(getId());
121 
122         newAttribute.setValueComparator(this.getValueComparator());
123 
124         newAttribute.getValues().addAll(getValues());
125 
126         newAttribute.getEncoders().addAll(getEncoders());
127 
128         return newAttribute;
129     }
130 }