View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.util;
19  
20  import java.util.Collection;
21  
22  /**
23   * Implementation of a {@link Registry} for {@link Attribute}s.
24   * 
25   * @author sanjin
26   */
27  public class Attributes extends BaseRegistry<String, Attribute> {
28  
29      /**
30       * Create instance of empty {@link Attribute} {@link Registry}.
31       */
32      public Attributes() {
33          super();
34      }
35  
36      /**
37       * Create instance of {@link Attribute} {@link Registry} that has specified
38       * {@link Attribute}s registered.
39       * 
40       * @param attributes the {@link Attribute}s.
41       * @throws NullPointerException if specified array of {@link Attribute}s or
42       *         any {@link Attribute} in the array is <code>null</code>.
43       */
44      public Attributes(final Attribute... attributes) {
45          super();
46  
47          setAll(attributes);
48      }
49  
50      /**
51       * Register specified {@link Attribute}.
52       * 
53       * @param attribute the {@link Attribute}.
54       * @throws NullPointerException if specified {@link Attribute} is
55       *         <code>null</code>.
56       */
57      @Override
58      public void register(final Attribute attribute) {
59          if (null == attribute) {
60              throw new NullPointerException("Attribute cannot be null.");
61          }
62  
63          set(attribute);
64      }
65  
66      /**
67       * Unregister {@link Attribute} with specified name.
68       * 
69       * @param name the name of the {@link Attribute}.
70       */
71      @Override
72      public void unregister(final String name) {
73          if (null == name) {
74              throw new NullPointerException("Attribute name cannot be null.");
75          }
76  
77          try {
78              super.unregister(name);
79          } catch (final NotRegisteredException ignored) {
80              /* attribute is not registered; do nothing */
81          }
82      }
83  
84      /**
85       * Returns if an {@link Attribute} with specified name is currently
86       * registered.
87       * 
88       * @param name the name of the {@link Attribute}.
89       * @return if an {@link Attribute} with specified name is currently
90       *         registered.
91       */
92      @Override
93      public boolean isRegistered(final String name) {
94          if (null == name) {
95              throw new NullPointerException("Attribute name cannot be null.");
96          }
97  
98          return super.isRegistered(name);
99      }
100 
101     /**
102      * Returns {@link Attribute} with specified name.
103      * 
104      * @param name the name of the {@link Attribute}.
105      * @return the {@link Attribute} or <code>null</code> if there is no such
106      *         {@link Attribute} registered.
107      */
108     @Override
109     public Attribute get(final String name) {
110         if (null == name) {
111             throw new NullPointerException("Attribute name cannot be null.");
112         }
113 
114         return super.get(name);
115     }
116 
117     /**
118      * Returns all currently registered {@link Attribute}s.
119      * 
120      * @return all currently registered {@link Attribute}s.
121      */
122     @Override
123     public Attribute[] getAll() {
124         final Collection<Attribute> result = getValues();
125         return result.toArray(new Attribute[result.size()]);
126     }
127 
128     /**
129      * Register specified {@link Attribute}s and remove all previously
130      * registered {@link Attribute}s.
131      * 
132      * @param attributes the {@link Attribute}s.
133      * @throws NullPointerException if specified array of {@link Attribute}s or
134      *         any {@link Attribute} in the array is <code>null</code>.
135      */
136     public void setAll(final Attribute... attributes) {
137         if (null == attributes) {
138             throw new NullPointerException("Attributes cannot be null.");
139         }
140         for (final Attribute attribute : attributes) {
141             if (null == attribute) {
142                 throw new NullPointerException("Attribute cannot be null.");
143             }
144         }
145 
146         setValues(attributes);
147     }
148 
149     @Override
150     protected String getKey(final Attribute attribute) {
151         if (null == attribute) {
152             throw new NullPointerException("Attribute cannot be null.");
153         }
154 
155         return attribute.getName();
156     }
157 }