|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MapAttributed.java | 0% | 0% | 8,3% | 1,4% |
|
1 | /* | |
2 | * Copyright (c) 2001-2004, | |
3 | * RedVerst Group, ISP RAS http://www.ispras.ru | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, this | |
10 | * list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
13 | * this list of conditions and the following disclaimer in the documentation | |
14 | * and/or other materials provided with the distribution. | |
15 | * | |
16 | * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS" | |
17 | * may not be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
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 | 4 | 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 | 0 | public MapAttributed( Map map ) |
65 | { | |
66 | 0 | if( map == null ) |
67 | { | |
68 | 0 | throw new NullPointerException(); |
69 | } | |
70 | 0 | this.map = map; |
71 | } | |
72 | ||
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | 0 | public boolean hasAttribute( String name ) |
77 | { | |
78 | /* | |
79 | * If map allows null key we should throw NullPointerException. | |
80 | */ | |
81 | 0 | if( name == null ) |
82 | { | |
83 | 0 | throw new AttributeException( this, name ); |
84 | } | |
85 | 0 | if( map == null ) return false; |
86 | 0 | return map.containsKey( name ); |
87 | } | |
88 | ||
89 | /** | |
90 | * Always returns <code>true</code> - attributes of this class | |
91 | * may be created dynamically. | |
92 | */ | |
93 | 0 | public boolean isAttrCreatable() |
94 | { | |
95 | 0 | return true; |
96 | } | |
97 | ||
98 | /** | |
99 | * All existent attributes may be removed dynamically. | |
100 | */ | |
101 | 0 | public boolean isRemovable( String name ) |
102 | { | |
103 | 0 | if( !hasAttribute( name ) ) |
104 | { | |
105 | 0 | throw new AttributeException( this, name ); |
106 | } | |
107 | 0 | return true; |
108 | } | |
109 | ||
110 | /** | |
111 | * Always returns <code>true</code> - all attributes of this class | |
112 | * are writable. | |
113 | */ | |
114 | 0 | public boolean isWritable( String name ) |
115 | { | |
116 | 0 | if( !hasAttribute( name ) ) |
117 | { | |
118 | 0 | throw new AttributeException( this, name ); |
119 | } | |
120 | 0 | return true; |
121 | } | |
122 | ||
123 | //------------------------------------------------------------------------------ | |
124 | ||
125 | /** | |
126 | * {@inheritDoc} | |
127 | */ | |
128 | 0 | public void addAttribute( String name, Object value ) |
129 | { | |
130 | 0 | if( hasAttribute( name ) ) |
131 | { | |
132 | 0 | throw new AttributeException( this, name ); |
133 | } | |
134 | 0 | if( map == null ) createMap(); |
135 | 0 | map.put( name, value ); |
136 | } | |
137 | ||
138 | /** | |
139 | * {@inheritDoc} | |
140 | */ | |
141 | 0 | public void removeAttribute( String name ) |
142 | { | |
143 | /* | |
144 | * If map allows null key we should throw NullPointerException. | |
145 | */ | |
146 | 0 | if( name == null ) |
147 | { | |
148 | 0 | throw new AttributeException( this, null ); |
149 | } | |
150 | ||
151 | 0 | if( map == null ) return; |
152 | ||
153 | 0 | map.remove( name ); |
154 | } | |
155 | ||
156 | //------------------------------------------------------------------------------ | |
157 | ||
158 | /** | |
159 | * {@inheritDoc} | |
160 | */ | |
161 | 0 | public Object getAttribute( String name ) |
162 | { | |
163 | 0 | if( !hasAttribute( name ) ) |
164 | { | |
165 | 0 | throw new AttributeException( this, name ); |
166 | } | |
167 | // (map == null) => !hasAttribute( name ) | |
168 | 0 | return map.get( name ); |
169 | } | |
170 | ||
171 | /** | |
172 | * {@inheritDoc} | |
173 | */ | |
174 | 0 | public void setAttribute( String name, Object value ) |
175 | { | |
176 | 0 | if( !hasAttribute( name ) ) |
177 | { | |
178 | 0 | throw new AttributeException( this, name ); |
179 | } | |
180 | // (map == null) => !hasAttribute( name ) | |
181 | 0 | map.put( name, value ); |
182 | } | |
183 | ||
184 | //------------------------------------------------------------------------------ | |
185 | ||
186 | /** | |
187 | * {@inheritDoc} | |
188 | */ | |
189 | 0 | public Set/*String*/ getAttributeNames() |
190 | { | |
191 | 0 | if( map == null ) return new HashSet/*String*/(); |
192 | 0 | return map.keySet(); |
193 | } | |
194 | ||
195 | //------------------------------------------------------------------------------ | |
196 | // implementation | |
197 | ||
198 | /** | |
199 | * Creates default map instance. | |
200 | */ | |
201 | 0 | protected void createMap() |
202 | { | |
203 | 0 | map = new HashMap/*String,Object*/(); |
204 | } | |
205 | ||
206 | private Map/*String,Object*/ map = null; | |
207 | } |
|