| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract class Options |
| 28 |
{ |
| 29 |
|
| 30 |
private int options = 0; |
| 31 |
|
| 32 |
private Map optionNames = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public Options() |
| 39 |
{ |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public Options(int options) throws XMPException |
| 51 |
{ |
| 52 |
assertOptionsValid(options); |
| 53 |
setOptions(options); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public void clear() |
| 61 |
{ |
| 62 |
options = 0; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public boolean isExactly(int optionBits) |
| 71 |
{ |
| 72 |
return getOptions() == optionBits; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public boolean containsAllOptions(int optionBits) |
| 81 |
{ |
| 82 |
return (getOptions() & optionBits) == optionBits; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public boolean containsOneOf(int optionBits) |
| 91 |
{ |
| 92 |
return ((getOptions()) & optionBits) != 0; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
protected boolean getOption(int optionBit) |
| 101 |
{ |
| 102 |
return (options & optionBit) != 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void setOption(int optionBits, boolean value) |
| 111 |
{ |
| 112 |
options = value ? options | optionBits : options & ~optionBits; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public int getOptions() |
| 121 |
{ |
| 122 |
return options; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public void setOptions(int options) throws XMPException |
| 131 |
{ |
| 132 |
assertOptionsValid(options); |
| 133 |
this.options = options; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public boolean equals(Object obj) |
| 141 |
{ |
| 142 |
return getOptions() == ((Options) obj).getOptions(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public int hashCode() |
| 150 |
{ |
| 151 |
return getOptions(); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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); |
| 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 |
|
| 190 |
|
| 191 |
public String toString() |
| 192 |
{ |
| 193 |
return "0x" + Integer.toHexString(options); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
protected abstract int getValidOptions(); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
protected abstract String defineOptionName(int option); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
protected void assertConsistency(int options) throws XMPException |
| 224 |
{ |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 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 |
|
| 284 |
|
| 285 |
private Map procureOptionNames() |
| 286 |
{ |
| 287 |
if (optionNames == null) |
| 288 |
{ |
| 289 |
optionNames = new HashMap(); |
| 290 |
} |
| 291 |
return optionNames; |
| 292 |
} |
| 293 |
} |