1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.dynattrs;
33
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.Map;
37 import java.util.Set;
38
39 /***
40 * The implementation of {@link Attributed} interface that stores attributes
41 * in {@link Map}.
42 *
43 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
44 * @version $Id: MapAttributed.java,v 1.2 2005/08/02 06:04:29 all-x Exp $
45 */
46 public class MapAttributed extends IndexedAttributedImpl
47 {
48 /***
49 * Class constructor with the default map created by {@link #createMap()}.
50 */
51 public MapAttributed()
52 {
53 }
54
55 /***
56 * Class constructor with the specified map.
57 *
58 * @param map The map to store attributes.
59 * This map should support optional operations:
60 * {@link Map#put(Object,Object)}, {@link Map#remove(Object)}.
61 * @throws NullPointerException
62 * If <code>map</code> is <code>null</code>.
63 */
64 public MapAttributed( Map map )
65 {
66 if( map == null )
67 {
68 throw new NullPointerException();
69 }
70 this.map = map;
71 }
72
73 /***
74 * {@inheritDoc}
75 */
76 public boolean hasAttribute( String name )
77 {
78
79
80
81 if( name == null )
82 {
83 throw new AttributeException( this, name );
84 }
85 if( map == null ) return false;
86 return map.containsKey( name );
87 }
88
89 /***
90 * Always returns <code>true</code> - attributes of this class
91 * may be created dynamically.
92 */
93 public boolean isAttrCreatable()
94 {
95 return true;
96 }
97
98 /***
99 * All existent attributes may be removed dynamically.
100 */
101 public boolean isRemovable( String name )
102 {
103 if( !hasAttribute( name ) )
104 {
105 throw new AttributeException( this, name );
106 }
107 return true;
108 }
109
110 /***
111 * Always returns <code>true</code> - all attributes of this class
112 * are writable.
113 */
114 public boolean isWritable( String name )
115 {
116 if( !hasAttribute( name ) )
117 {
118 throw new AttributeException( this, name );
119 }
120 return true;
121 }
122
123
124
125 /***
126 * {@inheritDoc}
127 */
128 public void addAttribute( String name, Object value )
129 {
130 if( hasAttribute( name ) )
131 {
132 throw new AttributeException( this, name );
133 }
134 if( map == null ) createMap();
135 map.put( name, value );
136 }
137
138 /***
139 * {@inheritDoc}
140 */
141 public void removeAttribute( String name )
142 {
143
144
145
146 if( name == null )
147 {
148 throw new AttributeException( this, null );
149 }
150
151 if( map == null ) return;
152
153 map.remove( name );
154 }
155
156
157
158 /***
159 * {@inheritDoc}
160 */
161 public Object getAttribute( String name )
162 {
163 if( !hasAttribute( name ) )
164 {
165 throw new AttributeException( this, name );
166 }
167
168 return map.get( name );
169 }
170
171 /***
172 * {@inheritDoc}
173 */
174 public void setAttribute( String name, Object value )
175 {
176 if( !hasAttribute( name ) )
177 {
178 throw new AttributeException( this, name );
179 }
180
181 map.put( name, value );
182 }
183
184
185
186 /***
187 * {@inheritDoc}
188 */
189 public Set
190 {
191 if( map == null ) return new HashSet
192 return map.keySet();
193 }
194
195
196
197
198 /***
199 * Creates default map instance.
200 */
201 protected void createMap()
202 {
203 map = new HashMap
204 }
205
206 private Map
207 }