Debian Codegraph @

src/com/adobe/internal/xmp/options/Options.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2006 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10 package com.adobe.internal.xmp.options;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import com.adobe.internal.xmp.XMPError;
16 import com.adobe.internal.xmp.XMPException;
17
18 /**
19 * The base class for a collection of 32 flag bits. Individual flags are defined as enum value bit
20 * masks. Inheriting classes add convenience accessor methods.
21 * FfF: In Java 1.5 this class can replace by an enum set.
22 *
23 * @author Stefan Makswit
24 * @version $Revision$
25 * @since 24.01.2006
26 */
27 public abstract class Options
28 {
29 /** the internal int containing all options */
30 private int options = 0;
31 /** a map containing the bit names */
32 private Map optionNames = null;
33
34
35 /**
36 * The default constructor.
37 */
38 public Options()
39 {
40 // EMTPY
41 }
42
43
44 /**
45 * Constructor with the options bit mask.
46 *
47 * @param options the options bit mask
48 * @throws XMPException If the options are not correct
49 */
50 public Options(int options) throws XMPException
51 {
52 assertOptionsValid(options);
53 setOptions(options);
54 }
55
56
57 /**
58 * Resets the options.
59 */
60 public void clear()
61 {
62 options = 0;
63 }
64
65
66 /**
67 * @param optionBits an option bitmask
68 * @return Returns true, if this object is equal to the given options.
69 */
70 public boolean isExactly(int optionBits)
71 {
72 return getOptions() == optionBits;
73 }
74
75
76 /**
77 * @param optionBits an option bitmask
78 * @return Returns true, if this object contains all given options.
79 */
80 public boolean containsAllOptions(int optionBits)
81 {
82 return (getOptions() & optionBits) == optionBits;
83 }
84
85
86 /**
87 * @param optionBits an option bitmask
88 * @return Returns true, if this object contain at least one of the given options.
89 */
90 public boolean containsOneOf(int optionBits)
91 {
92 return ((getOptions()) & optionBits) != 0;
93 }
94
95
96 /**
97 * @param optionBit the binary bit or bits that are requested
98 * @return Returns if <em>all</em> of the requested bits are set or not.
99 */
100 protected boolean getOption(int optionBit)
101 {
102 return (options & optionBit) != 0;
103 }
104
105
106 /**
107 * @param optionBits the binary bit or bits that shall be set to the given value
108 * @param value the boolean value to set
109 */
110 public void setOption(int optionBits, boolean value)
111 {
112 options = value ? options | optionBits : options & ~optionBits;
113 }
114
115
116 /**
117 * Is friendly to access it during the tests.
118 * @return Returns the options.
119 */
120 public int getOptions()
121 {
122 return options;
123 }
124
125
126 /**
127 * @param options The options to set.
128 * @throws XMPException If the options are not correct
129 */
130 public void setOptions(int options) throws XMPException
131 {
132 assertOptionsValid(options);
133 this.options = options;
134 }
135
136
137 /**
138 * @see Object#equals(Object)
139 */
140 public boolean equals(Object obj)
141 {
142 return getOptions() == ((Options) obj).getOptions();
143 }
144
145
146 /**
147 * @see java.lang.Object#hashCode()
148 */
149 public int hashCode()
150 {
151 return getOptions();
152 }
153
154
155 /**
156 * Creates a human readable string from the set options. <em>Note:</em> This method is quite
157 * expensive and should only be used within tests or as
158 * @return Returns a String listing all options that are set to <code>true</code> by their name,
159 * like &quot;option1 | option4&quot;.
160 */
161 public String getOptionsString()
162 {
163 if (options != 0)
164 {
165 StringBuffer sb = new StringBuffer();
166 int theBits = options;
167 while (theBits != 0)
168 {
169 int oneLessBit = theBits & (theBits - 1); // clear rightmost one bit
170 int singleBit = theBits ^ oneLessBit;
171 String bitName = getOptionName(singleBit);
172 sb.append(bitName);
173 if (oneLessBit != 0)
174 {
175 sb.append(" | ");
176 }
177 theBits = oneLessBit;
178 }
179 return sb.toString();
180 }
181 else
182 {
183 return "<none>";
184 }
185 }
186
187
188 /**
189 * @return Returns the options as hex bitmask.
190 */
191 public String toString()
192 {
193 return "0x" + Integer.toHexString(options);
194 }
195
196
197 /**
198 * To be implemeted by inheritants.
199 * @return Returns a bit mask where all valid option bits are set.
200 */
201 protected abstract int getValidOptions();
202
203
204 /**
205 * To be implemeted by inheritants.
206 * @param option a single, valid option bit.
207 * @return Returns a human readable name for an option bit.
208 */
209 protected abstract String defineOptionName(int option);
210
211
212 /**
213 * The inheriting option class can do additional checks on the options.
214 * <em>Note:</em> For performance reasons this method is only called
215 * when setting bitmasks directly.
216 * When get- and set-methods are used, this method must be called manually,
217 * normally only when the Options-object has been created from a client
218 * (it has to be made public therefore).
219 *
220 * @param options the bitmask to check.
221 * @throws XMPException Thrown if the options are not consistent.
222 */
223 protected void assertConsistency(int options) throws XMPException
224 {
225 // empty, no checks
226 }
227
228
229 /**
230 * Checks options before they are set.
231 * First it is checked if only defined options are used,
232 * second the additional {@link Options#assertConsistency(int)}-method is called.
233 *
234 * @param options the options to check
235 * @throws XMPException Thrown if the options are invalid.
236 */
237 private void assertOptionsValid(int options) throws XMPException
238 {
239 int invalidOptions = options & ~getValidOptions();
240 if (invalidOptions == 0)
241 {
242 assertConsistency(options);
243 }
244 else
245 {
246 throw new XMPException("The option bit(s) 0x" + Integer.toHexString(invalidOptions)
247 + " are invalid!", XMPError.BADOPTIONS);
248 }
249 }
250
251
252
253 /**
254 * Looks up or asks the inherited class for the name of an option bit.
255 * Its save that there is only one valid option handed into the method.
256 * @param option a single option bit
257 * @return Returns the option name or undefined.
258 */
259 private String getOptionName(int option)
260 {
261 Map optionsNames = procureOptionNames();
262
263 Integer key = new Integer(option);
264 String result = (String) optionsNames.get(key);
265 if (result == null)
266 {
267 result = defineOptionName(option);
268 if (result != null)
269 {
270 optionsNames.put(key, result);
271 }
272 else
273 {
274 result = "<option name not defined>";
275 }
276 }
277
278 return result;
279 }
280
281
282 /**
283 * @return Returns the optionNames map and creates it if required.
284 */
285 private Map procureOptionNames()
286 {
287 if (optionNames == null)
288 {
289 optionNames = new HashMap();
290 }
291 return optionNames;
292 }
293 }