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  
23  import org.opensaml.xml.util.DatatypeHelper;
24  
25  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
26  import edu.internet2.middleware.shibboleth.common.attribute.encoding.AttributeEncoder;
27  
28  /**
29   * An attribute implementation that operates on simple value types.
30   * 
31   * @param <ValueType> value type
32   */
33  public class BasicAttribute<ValueType> extends BaseAttribute<ValueType> implements Cloneable {
34  
35      /** ID of this attribute. */
36      private String id;
37  
38      /** Map of attribute encoders for this attribute, keyed off of category. */
39      private ArrayList<AttributeEncoder> encoders;
40  
41      /** Set of values for this attribute. */
42      private Collection<ValueType> values;
43  
44      /** Comparator for this attribute. */
45      private Comparator<ValueType> comparator;
46  
47      /** Constructor. */
48      public BasicAttribute() {
49          encoders = new ArrayList<AttributeEncoder>();
50          values = new ArrayList<ValueType>();
51      }
52  
53      /**
54       * Constructor.
55       * 
56       * @param attributeId the ID of this attribute
57       */
58      public BasicAttribute(String attributeId) {
59          id = DatatypeHelper.safeTrimOrNullString(attributeId);
60          encoders = new ArrayList<AttributeEncoder>();
61          values = new ArrayList<ValueType>();
62      }
63  
64      /** {@inheritDoc} */
65      public ArrayList<AttributeEncoder> getEncoders() {
66          return encoders;
67      }
68  
69      /** {@inheritDoc} */
70      public String getId() {
71          return id;
72      }
73  
74      /**
75       * Set id of this attribute.
76       * 
77       * @param newID new ID
78       */
79      public void setId(String newID) {
80          id = newID;
81      }
82  
83      /** {@inheritDoc} */
84      public Comparator<ValueType> getValueComparator() {
85          return comparator;
86      }
87  
88      /**
89       * Set value comparator for this attribute.
90       * 
91       * @param newComparator new value comparator
92       */
93      public void setValueComparator(Comparator<ValueType> newComparator) {
94          comparator = newComparator;
95      }
96  
97      /** {@inheritDoc} */
98      public Collection<ValueType> getValues() {
99          return values;
100     }
101 
102     /**
103      * Replace the current set of values with the given set.
104      * 
105      * @param newValues new values to replace existing ones
106      */
107     public void setValues(Collection<ValueType> newValues) {
108         values = newValues;
109     }
110 
111     /** {@inheritDoc} */
112     public BasicAttribute<ValueType> clone() {
113         BasicAttribute<ValueType> newAttribute = new BasicAttribute<ValueType>();
114 
115         newAttribute.setId(getId());
116 
117         newAttribute.setValueComparator(this.getValueComparator());
118 
119         newAttribute.getValues().addAll(getValues());
120 
121         newAttribute.getEncoders().addAll(getEncoders());
122 
123         return newAttribute;
124     }
125 }