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.event;
19  
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.concurrent.locks.ReadWriteLock;
24  import java.util.concurrent.locks.ReentrantReadWriteLock;
25  import at.ac.tuwien.infosys.sm4all.copal.api.util.Attribute;
26  
27  /**
28   * Representation of a list of {@link Attribute}s.
29   * 
30   * @author sanjin
31   */
32  public class Attributes {
33  
34      private final ReadWriteLock lock = new ReentrantReadWriteLock();
35      private final Map<String, Attribute> attributes = new HashMap<String, Attribute>();
36  
37      /**
38       * Adds {@link Attribute} with specified name to specified value. If the
39       * value is <code>null</code> the {@link Attribute} will be removed and
40       * consequent calls to {@link #contains(String)} with same name of
41       * {@link Attribute} will return <code>false</code>.
42       * 
43       * @param name the name of {@link Attribute}.
44       * @param value the value of {@link Attribute}.
45       * @throws NullPointerException if specified name of {@link Attribute} is
46       *         <code>null</code>.
47       * @throws IllegalArgumentException if specified name of {@link Attribute}
48       *         is an empty or blank string.
49       */
50      public void add(final String name, final String value) {
51          if (name == null)
52              throw new NullPointerException("Attribute name cannot be null.");
53          if (name.trim().isEmpty())
54              throw new IllegalArgumentException(
55                      "Attribute name cannot be an empty or blank string.");
56  
57          this.lock.writeLock().lock();
58          try {
59              if (value == null) {
60                  if (this.attributes.containsKey(name))
61                      this.attributes.remove(name);
62              } else
63                  this.attributes.put(name, new Attribute(name, value));
64          } finally {
65              this.lock.writeLock().unlock();
66          }
67      }
68  
69      /**
70       * Returns the value of {@link Attribute} with specified name. If this list
71       * does not have {@link Attribute} with specified name, <code>null</code>
72       * will be returned.
73       * 
74       * @param name the name of {@link Attribute}.
75       * @return the value of {@link Attribute} or <code>null</code> if there is
76       *         no such {@link Attribute}.
77       */
78      public String get(final String name) {
79          final String result;
80  
81          this.lock.readLock().lock();
82          try {
83              if (this.attributes.containsKey(name))
84                  result = this.attributes.get(name).getValue();
85              else
86                  result = null;
87          } finally {
88              this.lock.readLock().unlock();
89          }
90  
91          return result;
92      }
93  
94      /**
95       * Returns all currently added {@link Attribute}s.
96       * 
97       * @return all currently added {@link Attribute}s.
98       */
99      public Attribute[] getAll() {
100         final Collection<Attribute> result;
101 
102         this.lock.readLock().lock();
103         try {
104             result = this.attributes.values();
105         } finally {
106             this.lock.readLock().unlock();
107         }
108 
109         return result.toArray(new Attribute[result.size()]);
110     }
111 
112     /**
113      * Returns if an {@link Attribute} that has specified name is currently
114      * present in this list. If the {@link Attribute} was never added (or was
115      * added and consequently removed by adding it with <code>null</code>
116      * value), <code>false</code> will be returned; otherwise <code>true</code>
117      * will be returned.
118      * 
119      * @param name the name of {@link Attribute}.
120      * @return if an {@link Attribute} that has specified name is currently
121      *         present in this list.
122      */
123     public boolean contains(final String name) {
124         final boolean result;
125 
126         this.lock.readLock().lock();
127         try {
128             result = this.attributes.containsKey(name);
129         } finally {
130             this.lock.readLock().unlock();
131         }
132 
133         return result;
134     }
135 }