Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.oracle.webservices.internal.api.message; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | |
aoqi@0 | 31 | import java.lang.reflect.Field; |
aoqi@0 | 32 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 33 | import java.lang.reflect.Method; |
aoqi@0 | 34 | import java.security.AccessController; |
aoqi@0 | 35 | import java.security.PrivilegedAction; |
aoqi@0 | 36 | import java.util.AbstractMap; |
aoqi@0 | 37 | import java.util.HashMap; |
aoqi@0 | 38 | import java.util.HashSet; |
aoqi@0 | 39 | import java.util.Map; |
aoqi@0 | 40 | import java.util.Map.Entry; |
aoqi@0 | 41 | import java.util.Set; |
aoqi@0 | 42 | |
aoqi@0 | 43 | |
aoqi@0 | 44 | /** |
aoqi@0 | 45 | * A set of "properties" that can be accessed via strongly-typed fields |
aoqi@0 | 46 | * as well as reflexibly through the property name. |
aoqi@0 | 47 | * |
aoqi@0 | 48 | * @author Kohsuke Kawaguchi |
aoqi@0 | 49 | */ |
aoqi@0 | 50 | @SuppressWarnings("SuspiciousMethodCalls") |
aoqi@0 | 51 | public abstract class BasePropertySet implements PropertySet { |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** |
aoqi@0 | 54 | * Creates a new instance of TypedMap. |
aoqi@0 | 55 | */ |
aoqi@0 | 56 | protected BasePropertySet() { |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | private Map<String,Object> mapView; |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Represents the list of strongly-typed known properties |
aoqi@0 | 63 | * (keyed by property names.) |
aoqi@0 | 64 | * |
aoqi@0 | 65 | * <p> |
aoqi@0 | 66 | * Just giving it an alias to make the use of this class more fool-proof. |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | protected static class PropertyMap extends HashMap<String,Accessor> { |
aoqi@0 | 69 | |
aoqi@0 | 70 | // the entries are often being iterated through so performance can be improved |
aoqi@0 | 71 | // by their caching instead of iterating through the original (immutable) map each time |
aoqi@0 | 72 | transient PropertyMapEntry[] cachedEntries = null; |
aoqi@0 | 73 | |
aoqi@0 | 74 | PropertyMapEntry[] getPropertyMapEntries() { |
aoqi@0 | 75 | if (cachedEntries == null) { |
aoqi@0 | 76 | cachedEntries = createPropertyMapEntries(); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | return cachedEntries; |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | private PropertyMapEntry[] createPropertyMapEntries() { |
aoqi@0 | 82 | final PropertyMapEntry[] modelEntries = new PropertyMapEntry[size()]; |
aoqi@0 | 83 | int i = 0; |
aoqi@0 | 84 | for (final Entry<String, Accessor> e : entrySet()) { |
aoqi@0 | 85 | modelEntries[i++] = new PropertyMapEntry(e.getKey(), e.getValue()); |
aoqi@0 | 86 | } |
aoqi@0 | 87 | return modelEntries; |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | /** |
aoqi@0 | 93 | * PropertyMapEntry represents a Map.Entry in the PropertyMap with more efficient access. |
aoqi@0 | 94 | */ |
aoqi@0 | 95 | static public class PropertyMapEntry { |
aoqi@0 | 96 | public PropertyMapEntry(String k, Accessor v) { |
aoqi@0 | 97 | key = k; value = v; |
aoqi@0 | 98 | } |
aoqi@0 | 99 | String key; |
aoqi@0 | 100 | Accessor value; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | /** |
aoqi@0 | 104 | * Map representing the Fields and Methods annotated with {@link PropertySet.Property}. |
aoqi@0 | 105 | * Model of {@link PropertySet} class. |
aoqi@0 | 106 | * |
aoqi@0 | 107 | * <p> |
aoqi@0 | 108 | * At the end of the derivation chain this method just needs to be implemented |
aoqi@0 | 109 | * as: |
aoqi@0 | 110 | * |
aoqi@0 | 111 | * <pre> |
aoqi@0 | 112 | * private static final PropertyMap model; |
aoqi@0 | 113 | * static { |
aoqi@0 | 114 | * model = parse(MyDerivedClass.class); |
aoqi@0 | 115 | * } |
aoqi@0 | 116 | * protected PropertyMap getPropertyMap() { |
aoqi@0 | 117 | * return model; |
aoqi@0 | 118 | * } |
aoqi@0 | 119 | * </pre> |
aoqi@0 | 120 | */ |
aoqi@0 | 121 | protected abstract PropertyMap getPropertyMap(); |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * This method parses a class for fields and methods with {@link PropertySet.Property}. |
aoqi@0 | 125 | */ |
aoqi@0 | 126 | protected static PropertyMap parse(final Class clazz) { |
aoqi@0 | 127 | // make all relevant fields and methods accessible. |
aoqi@0 | 128 | // this allows runtime to skip the security check, so they runs faster. |
aoqi@0 | 129 | return AccessController.doPrivileged(new PrivilegedAction<PropertyMap>() { |
aoqi@0 | 130 | @Override |
aoqi@0 | 131 | public PropertyMap run() { |
aoqi@0 | 132 | PropertyMap props = new PropertyMap(); |
aoqi@0 | 133 | for (Class c=clazz; c!=null; c=c.getSuperclass()) { |
aoqi@0 | 134 | for (Field f : c.getDeclaredFields()) { |
aoqi@0 | 135 | Property cp = f.getAnnotation(Property.class); |
aoqi@0 | 136 | if(cp!=null) { |
aoqi@0 | 137 | for(String value : cp.value()) { |
aoqi@0 | 138 | props.put(value, new FieldAccessor(f, value)); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |
aoqi@0 | 142 | for (Method m : c.getDeclaredMethods()) { |
aoqi@0 | 143 | Property cp = m.getAnnotation(Property.class); |
aoqi@0 | 144 | if(cp!=null) { |
aoqi@0 | 145 | String name = m.getName(); |
aoqi@0 | 146 | assert name.startsWith("get") || name.startsWith("is"); |
aoqi@0 | 147 | |
aoqi@0 | 148 | String setName = name.startsWith("is") ? "set"+name.substring(2) : // isFoo -> setFoo |
aoqi@0 | 149 | 's' +name.substring(1); // getFoo -> setFoo |
aoqi@0 | 150 | Method setter; |
aoqi@0 | 151 | try { |
aoqi@0 | 152 | setter = clazz.getMethod(setName,m.getReturnType()); |
aoqi@0 | 153 | } catch (NoSuchMethodException e) { |
aoqi@0 | 154 | setter = null; // no setter |
aoqi@0 | 155 | } |
aoqi@0 | 156 | for(String value : cp.value()) { |
aoqi@0 | 157 | props.put(value, new MethodAccessor(m, setter, value)); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | } |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | return props; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | }); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | /** |
aoqi@0 | 169 | * Represents a typed property defined on a {@link PropertySet}. |
aoqi@0 | 170 | */ |
aoqi@0 | 171 | protected interface Accessor { |
aoqi@0 | 172 | String getName(); |
aoqi@0 | 173 | boolean hasValue(PropertySet props); |
aoqi@0 | 174 | Object get(PropertySet props); |
aoqi@0 | 175 | void set(PropertySet props, Object value); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | static final class FieldAccessor implements Accessor { |
aoqi@0 | 179 | /** |
aoqi@0 | 180 | * Field with the annotation. |
aoqi@0 | 181 | */ |
aoqi@0 | 182 | private final Field f; |
aoqi@0 | 183 | |
aoqi@0 | 184 | /** |
aoqi@0 | 185 | * One of the values in {@link Property} annotation on {@link #f}. |
aoqi@0 | 186 | */ |
aoqi@0 | 187 | private final String name; |
aoqi@0 | 188 | |
aoqi@0 | 189 | protected FieldAccessor(Field f, String name) { |
aoqi@0 | 190 | this.f = f; |
aoqi@0 | 191 | f.setAccessible(true); |
aoqi@0 | 192 | this.name = name; |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | @Override |
aoqi@0 | 196 | public String getName() { |
aoqi@0 | 197 | return name; |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | @Override |
aoqi@0 | 201 | public boolean hasValue(PropertySet props) { |
aoqi@0 | 202 | return get(props)!=null; |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | @Override |
aoqi@0 | 206 | public Object get(PropertySet props) { |
aoqi@0 | 207 | try { |
aoqi@0 | 208 | return f.get(props); |
aoqi@0 | 209 | } catch (IllegalAccessException e) { |
aoqi@0 | 210 | throw new AssertionError(); |
aoqi@0 | 211 | } |
aoqi@0 | 212 | } |
aoqi@0 | 213 | |
aoqi@0 | 214 | @Override |
aoqi@0 | 215 | public void set(PropertySet props, Object value) { |
aoqi@0 | 216 | try { |
aoqi@0 | 217 | f.set(props,value); |
aoqi@0 | 218 | } catch (IllegalAccessException e) { |
aoqi@0 | 219 | throw new AssertionError(); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | } |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | static final class MethodAccessor implements Accessor { |
aoqi@0 | 225 | /** |
aoqi@0 | 226 | * Getter method. |
aoqi@0 | 227 | */ |
aoqi@0 | 228 | private final @NotNull Method getter; |
aoqi@0 | 229 | /** |
aoqi@0 | 230 | * Setter method. |
aoqi@0 | 231 | * Some property is read-only. |
aoqi@0 | 232 | */ |
aoqi@0 | 233 | private final @Nullable Method setter; |
aoqi@0 | 234 | |
aoqi@0 | 235 | /** |
aoqi@0 | 236 | * One of the values in {@link Property} annotation on {@link #getter}. |
aoqi@0 | 237 | */ |
aoqi@0 | 238 | private final String name; |
aoqi@0 | 239 | |
aoqi@0 | 240 | protected MethodAccessor(Method getter, Method setter, String value) { |
aoqi@0 | 241 | this.getter = getter; |
aoqi@0 | 242 | this.setter = setter; |
aoqi@0 | 243 | this.name = value; |
aoqi@0 | 244 | getter.setAccessible(true); |
aoqi@0 | 245 | if (setter!=null) { |
aoqi@0 | 246 | setter.setAccessible(true); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | @Override |
aoqi@0 | 251 | public String getName() { |
aoqi@0 | 252 | return name; |
aoqi@0 | 253 | } |
aoqi@0 | 254 | |
aoqi@0 | 255 | @Override |
aoqi@0 | 256 | public boolean hasValue(PropertySet props) { |
aoqi@0 | 257 | return get(props)!=null; |
aoqi@0 | 258 | } |
aoqi@0 | 259 | |
aoqi@0 | 260 | @Override |
aoqi@0 | 261 | public Object get(PropertySet props) { |
aoqi@0 | 262 | try { |
aoqi@0 | 263 | return getter.invoke(props); |
aoqi@0 | 264 | } catch (IllegalAccessException e) { |
aoqi@0 | 265 | throw new AssertionError(); |
aoqi@0 | 266 | } catch (InvocationTargetException e) { |
aoqi@0 | 267 | handle(e); |
aoqi@0 | 268 | return 0; // never reach here |
aoqi@0 | 269 | } |
aoqi@0 | 270 | } |
aoqi@0 | 271 | |
aoqi@0 | 272 | @Override |
aoqi@0 | 273 | public void set(PropertySet props, Object value) { |
aoqi@0 | 274 | if(setter==null) { |
aoqi@0 | 275 | throw new ReadOnlyPropertyException(getName()); |
aoqi@0 | 276 | } |
aoqi@0 | 277 | try { |
aoqi@0 | 278 | setter.invoke(props,value); |
aoqi@0 | 279 | } catch (IllegalAccessException e) { |
aoqi@0 | 280 | throw new AssertionError(); |
aoqi@0 | 281 | } catch (InvocationTargetException e) { |
aoqi@0 | 282 | handle(e); |
aoqi@0 | 283 | } |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | /** |
aoqi@0 | 287 | * Since we don't expect the getter/setter to throw a checked exception, |
aoqi@0 | 288 | * it should be possible to make the exception propagation transparent. |
aoqi@0 | 289 | * That's what we are trying to do here. |
aoqi@0 | 290 | */ |
aoqi@0 | 291 | private Exception handle(InvocationTargetException e) { |
aoqi@0 | 292 | Throwable t = e.getTargetException(); |
aoqi@0 | 293 | if (t instanceof Error) { |
aoqi@0 | 294 | throw (Error)t; |
aoqi@0 | 295 | } |
aoqi@0 | 296 | if (t instanceof RuntimeException) { |
aoqi@0 | 297 | throw (RuntimeException)t; |
aoqi@0 | 298 | } |
aoqi@0 | 299 | throw new Error(e); |
aoqi@0 | 300 | } |
aoqi@0 | 301 | } |
aoqi@0 | 302 | |
aoqi@0 | 303 | |
aoqi@0 | 304 | /** |
aoqi@0 | 305 | * Class allowing to work with PropertySet object as with a Map; it doesn't only allow to read properties from |
aoqi@0 | 306 | * the map but also to modify the map in a way it is in sync with original strongly typed fields. It also allows |
aoqi@0 | 307 | * (if necessary) to store additional properties those can't be found in strongly typed fields. |
aoqi@0 | 308 | * |
aoqi@0 | 309 | * @see com.sun.xml.internal.ws.api.PropertySet#asMap() method |
aoqi@0 | 310 | */ |
aoqi@0 | 311 | final class MapView extends HashMap<String, Object> { |
aoqi@0 | 312 | |
aoqi@0 | 313 | // flag if it should allow store also different properties |
aoqi@0 | 314 | // than the from strongly typed fields |
aoqi@0 | 315 | boolean extensible; |
aoqi@0 | 316 | |
aoqi@0 | 317 | MapView(boolean extensible) { |
aoqi@0 | 318 | super(getPropertyMap().getPropertyMapEntries().length); |
aoqi@0 | 319 | this.extensible = extensible; |
aoqi@0 | 320 | initialize(); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | |
aoqi@0 | 323 | public void initialize() { |
aoqi@0 | 324 | // iterate (cached) array instead of map to speed things up ... |
aoqi@0 | 325 | PropertyMapEntry[] entries = getPropertyMap().getPropertyMapEntries(); |
aoqi@0 | 326 | for (PropertyMapEntry entry : entries) { |
aoqi@0 | 327 | super.put(entry.key, entry.value); |
aoqi@0 | 328 | } |
aoqi@0 | 329 | } |
aoqi@0 | 330 | |
aoqi@0 | 331 | @Override |
aoqi@0 | 332 | public Object get(Object key) { |
aoqi@0 | 333 | Object o = super.get(key); |
aoqi@0 | 334 | if (o instanceof Accessor) { |
aoqi@0 | 335 | return ((Accessor) o).get(BasePropertySet.this); |
aoqi@0 | 336 | } else { |
aoqi@0 | 337 | return o; |
aoqi@0 | 338 | } |
aoqi@0 | 339 | } |
aoqi@0 | 340 | |
aoqi@0 | 341 | @Override |
aoqi@0 | 342 | public Set<Entry<String, Object>> entrySet() { |
aoqi@0 | 343 | Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>(); |
aoqi@0 | 344 | for (String key : keySet()) { |
aoqi@0 | 345 | entries.add(new SimpleImmutableEntry<String, Object>(key, get(key))); |
aoqi@0 | 346 | } |
aoqi@0 | 347 | return entries; |
aoqi@0 | 348 | } |
aoqi@0 | 349 | |
aoqi@0 | 350 | @Override |
aoqi@0 | 351 | public Object put(String key, Object value) { |
aoqi@0 | 352 | |
aoqi@0 | 353 | Object o = super.get(key); |
aoqi@0 | 354 | if (o != null && o instanceof Accessor) { |
aoqi@0 | 355 | |
aoqi@0 | 356 | Object oldValue = ((Accessor) o).get(BasePropertySet.this); |
aoqi@0 | 357 | ((Accessor) o).set(BasePropertySet.this, value); |
aoqi@0 | 358 | return oldValue; |
aoqi@0 | 359 | |
aoqi@0 | 360 | } else { |
aoqi@0 | 361 | |
aoqi@0 | 362 | if (extensible) { |
aoqi@0 | 363 | return super.put(key, value); |
aoqi@0 | 364 | } else { |
aoqi@0 | 365 | throw new IllegalStateException("Unknown property [" + key + "] for PropertySet [" + |
aoqi@0 | 366 | BasePropertySet.this.getClass().getName() + "]"); |
aoqi@0 | 367 | } |
aoqi@0 | 368 | } |
aoqi@0 | 369 | } |
aoqi@0 | 370 | |
aoqi@0 | 371 | @Override |
aoqi@0 | 372 | public void clear() { |
aoqi@0 | 373 | for (String key : keySet()) { |
aoqi@0 | 374 | remove(key); |
aoqi@0 | 375 | } |
aoqi@0 | 376 | } |
aoqi@0 | 377 | |
aoqi@0 | 378 | @Override |
aoqi@0 | 379 | public Object remove(Object key) { |
aoqi@0 | 380 | Object o; |
aoqi@0 | 381 | o = super.get(key); |
aoqi@0 | 382 | if (o instanceof Accessor) { |
aoqi@0 | 383 | ((Accessor)o).set(BasePropertySet.this, null); |
aoqi@0 | 384 | } |
aoqi@0 | 385 | return super.remove(key); |
aoqi@0 | 386 | } |
aoqi@0 | 387 | } |
aoqi@0 | 388 | |
aoqi@0 | 389 | @Override |
aoqi@0 | 390 | public boolean containsKey(Object key) { |
aoqi@0 | 391 | Accessor sp = getPropertyMap().get(key); |
aoqi@0 | 392 | if (sp != null) { |
aoqi@0 | 393 | return sp.get(this) != null; |
aoqi@0 | 394 | } |
aoqi@0 | 395 | return false; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | /** |
aoqi@0 | 399 | * Gets the name of the property. |
aoqi@0 | 400 | * |
aoqi@0 | 401 | * @param key |
aoqi@0 | 402 | * This field is typed as {@link Object} to follow the {@link Map#get(Object)} |
aoqi@0 | 403 | * convention, but if anything but {@link String} is passed, this method |
aoqi@0 | 404 | * just returns null. |
aoqi@0 | 405 | */ |
aoqi@0 | 406 | @Override |
aoqi@0 | 407 | public Object get(Object key) { |
aoqi@0 | 408 | Accessor sp = getPropertyMap().get(key); |
aoqi@0 | 409 | if (sp != null) { |
aoqi@0 | 410 | return sp.get(this); |
aoqi@0 | 411 | } |
aoqi@0 | 412 | throw new IllegalArgumentException("Undefined property "+key); |
aoqi@0 | 413 | } |
aoqi@0 | 414 | |
aoqi@0 | 415 | /** |
aoqi@0 | 416 | * Sets a property. |
aoqi@0 | 417 | * |
aoqi@0 | 418 | * <h3>Implementation Note</h3> |
aoqi@0 | 419 | * This method is slow. Code inside JAX-WS should define strongly-typed |
aoqi@0 | 420 | * fields in this class and access them directly, instead of using this. |
aoqi@0 | 421 | * |
aoqi@0 | 422 | * @throws ReadOnlyPropertyException |
aoqi@0 | 423 | * if the given key is an alias of a strongly-typed field, |
aoqi@0 | 424 | * and if the name object given is not assignable to the field. |
aoqi@0 | 425 | * |
aoqi@0 | 426 | * @see Property |
aoqi@0 | 427 | */ |
aoqi@0 | 428 | @Override |
aoqi@0 | 429 | public Object put(String key, Object value) { |
aoqi@0 | 430 | Accessor sp = getPropertyMap().get(key); |
aoqi@0 | 431 | if(sp!=null) { |
aoqi@0 | 432 | Object old = sp.get(this); |
aoqi@0 | 433 | sp.set(this,value); |
aoqi@0 | 434 | return old; |
aoqi@0 | 435 | } else { |
aoqi@0 | 436 | throw new IllegalArgumentException("Undefined property "+key); |
aoqi@0 | 437 | } |
aoqi@0 | 438 | } |
aoqi@0 | 439 | |
aoqi@0 | 440 | /** |
aoqi@0 | 441 | * Checks if this {@link PropertySet} supports a property of the given name. |
aoqi@0 | 442 | */ |
aoqi@0 | 443 | @Override |
aoqi@0 | 444 | public boolean supports(Object key) { |
aoqi@0 | 445 | return getPropertyMap().containsKey(key); |
aoqi@0 | 446 | } |
aoqi@0 | 447 | |
aoqi@0 | 448 | @Override |
aoqi@0 | 449 | public Object remove(Object key) { |
aoqi@0 | 450 | Accessor sp = getPropertyMap().get(key); |
aoqi@0 | 451 | if(sp!=null) { |
aoqi@0 | 452 | Object old = sp.get(this); |
aoqi@0 | 453 | sp.set(this,null); |
aoqi@0 | 454 | return old; |
aoqi@0 | 455 | } else { |
aoqi@0 | 456 | throw new IllegalArgumentException("Undefined property "+key); |
aoqi@0 | 457 | } |
aoqi@0 | 458 | } |
aoqi@0 | 459 | |
aoqi@0 | 460 | /** |
aoqi@0 | 461 | * Creates a {@link Map} view of this {@link PropertySet}. |
aoqi@0 | 462 | * |
aoqi@0 | 463 | * <p> |
aoqi@0 | 464 | * This map is partially live, in the sense that values you set to it |
aoqi@0 | 465 | * will be reflected to {@link PropertySet}. |
aoqi@0 | 466 | * |
aoqi@0 | 467 | * <p> |
aoqi@0 | 468 | * However, this map may not pick up changes made |
aoqi@0 | 469 | * to {@link PropertySet} after the view is created. |
aoqi@0 | 470 | * |
aoqi@0 | 471 | * @deprecated use newer implementation {@link PropertySet#asMap()} which produces |
aoqi@0 | 472 | * readwrite {@link Map} |
aoqi@0 | 473 | * |
aoqi@0 | 474 | * @return |
aoqi@0 | 475 | * always non-null valid instance. |
aoqi@0 | 476 | */ |
aoqi@0 | 477 | @Deprecated |
aoqi@0 | 478 | @Override |
aoqi@0 | 479 | public final Map<String,Object> createMapView() { |
aoqi@0 | 480 | final Set<Entry<String,Object>> core = new HashSet<Entry<String,Object>>(); |
aoqi@0 | 481 | createEntrySet(core); |
aoqi@0 | 482 | |
aoqi@0 | 483 | return new AbstractMap<String, Object>() { |
aoqi@0 | 484 | @Override |
aoqi@0 | 485 | public Set<Entry<String,Object>> entrySet() { |
aoqi@0 | 486 | return core; |
aoqi@0 | 487 | } |
aoqi@0 | 488 | }; |
aoqi@0 | 489 | } |
aoqi@0 | 490 | |
aoqi@0 | 491 | /** |
aoqi@0 | 492 | * Creates a modifiable {@link Map} view of this {@link PropertySet}. |
aoqi@0 | 493 | * <p/> |
aoqi@0 | 494 | * Changes done on this {@link Map} or on {@link PropertySet} object work in both directions - values made to |
aoqi@0 | 495 | * {@link Map} are reflected to {@link PropertySet} and changes done using getters/setters on {@link PropertySet} |
aoqi@0 | 496 | * object are automatically reflected in this {@link Map}. |
aoqi@0 | 497 | * <p/> |
aoqi@0 | 498 | * If necessary, it also can hold other values (not present on {@link PropertySet}) - |
aoqi@0 | 499 | * {@see PropertySet#mapAllowsAdditionalProperties} |
aoqi@0 | 500 | * |
aoqi@0 | 501 | * @return always non-null valid instance. |
aoqi@0 | 502 | */ |
aoqi@0 | 503 | @Override |
aoqi@0 | 504 | public Map<String, Object> asMap() { |
aoqi@0 | 505 | if (mapView == null) { |
aoqi@0 | 506 | mapView = createView(); |
aoqi@0 | 507 | } |
aoqi@0 | 508 | return mapView; |
aoqi@0 | 509 | } |
aoqi@0 | 510 | |
aoqi@0 | 511 | protected Map<String, Object> createView() { |
aoqi@0 | 512 | return new MapView(mapAllowsAdditionalProperties()); |
aoqi@0 | 513 | } |
aoqi@0 | 514 | |
aoqi@0 | 515 | /** |
aoqi@0 | 516 | * Used when constructing the {@link MapView} for this object - it controls if the {@link MapView} servers only to |
aoqi@0 | 517 | * access strongly typed values or allows also different values |
aoqi@0 | 518 | * |
aoqi@0 | 519 | * @return true if {@link Map} should allow also properties not defined as strongly typed fields |
aoqi@0 | 520 | */ |
aoqi@0 | 521 | protected boolean mapAllowsAdditionalProperties() { |
aoqi@0 | 522 | return false; |
aoqi@0 | 523 | } |
aoqi@0 | 524 | |
aoqi@0 | 525 | protected void createEntrySet(Set<Entry<String,Object>> core) { |
aoqi@0 | 526 | for (final Entry<String, Accessor> e : getPropertyMap().entrySet()) { |
aoqi@0 | 527 | core.add(new Entry<String, Object>() { |
aoqi@0 | 528 | @Override |
aoqi@0 | 529 | public String getKey() { |
aoqi@0 | 530 | return e.getKey(); |
aoqi@0 | 531 | } |
aoqi@0 | 532 | |
aoqi@0 | 533 | @Override |
aoqi@0 | 534 | public Object getValue() { |
aoqi@0 | 535 | return e.getValue().get(BasePropertySet.this); |
aoqi@0 | 536 | } |
aoqi@0 | 537 | |
aoqi@0 | 538 | @Override |
aoqi@0 | 539 | public Object setValue(Object value) { |
aoqi@0 | 540 | Accessor acc = e.getValue(); |
aoqi@0 | 541 | Object old = acc.get(BasePropertySet.this); |
aoqi@0 | 542 | acc.set(BasePropertySet.this,value); |
aoqi@0 | 543 | return old; |
aoqi@0 | 544 | } |
aoqi@0 | 545 | }); |
aoqi@0 | 546 | } |
aoqi@0 | 547 | } |
aoqi@0 | 548 | } |