View Javadoc

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