/** * Raise the add change event and relationship maintainence. */ protected void raiseAddChangeEvent(Object key, Object value) { if (hasTrackedPropertyChangeListener()) { _persistence_getPropertyChangeListener().propertyChange(new MapChangeEvent(this, getTrackedAttributeName(), this, key, value, CollectionChangeEvent.ADD, true)); } // this is where relationship maintenance would go }
/** * INTERNAL: * Check whether the contents have been read from the database. * If they have not, read them and set the delegate. * This method used to be synchronized, which caused deadlock. */ protected Hashtable getDelegate() { if (delegate == null) { synchronized(this){ if (delegate == null) { delegate = this.buildDelegate(); } } } return delegate; }
@Override public void clear(){ IndirectMap.this.clear(); }
@Override public boolean retainAll(Collection<?> c){ boolean result = false; Iterator objects = delegateSet.iterator(); while (objects.hasNext()) { Object object = objects.next(); if (!c.contains(object)) { objects.remove(); IndirectMap.this.raiseRemoveChangeEvent(object, IndirectMap.this.getDelegate().get(object)); result = true; } } return result; }
/** * @see java.util.Hashtable#put(java.lang.Object, java.lang.Object) */ @Override public synchronized V put(K key, V value) { V oldValue = this.getDelegate().put(key, value); if (oldValue != null){ raiseRemoveChangeEvent(key, oldValue); } raiseAddChangeEvent(key, value); return oldValue; }
@Override public synchronized V replace(K key, V value) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { Map<K, V> del = getDelegate(); if (del.containsKey(key)) { return put(key, value); } return null; } return getDelegate().replace(key, value); }
@Override public synchronized V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { V oldValue = get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if (newValue == null) { remove(key); } else { put(key, newValue); } return newValue; } return getDelegate().merge(key, value, remappingFunction); }
/** * @see java.util.Hashtable#clear() */ public synchronized void clear() { if (hasTrackedPropertyChangeListener()) { Iterator objects = this.keySet().iterator(); while (objects.hasNext()) { Object o = objects.next(); objects.remove(); this.raiseRemoveChangeEvent(o, this.get(o)); } } else { this.getDelegate().clear(); } }
/** * INTERNAL: * Return the real collection object. * This will force instantiation. */ @Override public Object getDelegateObject() { return getDelegate(); }
@Override public synchronized void replaceAll(BiFunction<? super K,? super V,? extends V> function) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { getDelegate().entrySet().stream().forEach((entry) -> { K key = entry.getKey(); V oldValue = entry.getValue(); entry.setValue(function.apply(key, entry.getValue())); raiseRemoveChangeEvent(key, oldValue); raiseAddChangeEvent(key, entry.getValue()); }); return; } getDelegate().replaceAll(function); }
@Override public synchronized V putIfAbsent(K key, V value) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { V current = getDelegate().get(key); if (current == null) { V v = getDelegate().put(key, value); raiseAddChangeEvent(key, value); return v; } return current; } return getDelegate().putIfAbsent(key, value); }
@Override public synchronized boolean remove(Object key, Object value) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { Map<K, V> del = getDelegate(); if (del.containsKey(key) && Objects.equals(del.get(key), value)) { del.remove(key); raiseRemoveChangeEvent(key, value); return true; } return false; } return getDelegate().remove(key, value); }
/** * @see java.util.Hashtable#clear() */ @Override public synchronized void clear() { if (hasTrackedPropertyChangeListener()) { Iterator<K> objects = this.keySet().iterator(); while (objects.hasNext()) { K o = objects.next(); objects.remove(); } } else { this.getDelegate().clear(); } }
/** * PUBLIC: * Use the Hashtable.toString(); but wrap it with braces to indicate * there is a bit of indirection. * Don't allow this method to trigger a database read. * @see java.util.Hashtable#toString() */ public String toString() { if (ValueHolderInterface.shouldToStringInstantiate) { return this.getDelegate().toString(); } if (this.isInstantiated()) { return "{" + this.getDelegate().toString() + "}"; } else { return "{" + org.eclipse.persistence.internal.helper.Helper.getShortClassName(this.getClass()) + ": not instantiated}"; } }
/** * Return the freshly-built delegate. */ protected Hashtable<K, V> buildDelegate() { Hashtable<K, V> value = (Hashtable<K, V>)getValueHolder().getValue(); if (value == null) { value = new Hashtable<>(this.initialCapacity, this.loadFactor); } return value; }
/** * PUBLIC: * Construct a new IndirectMap with the same mappings as the given Map. * The IndirectMap is created with a capacity of twice the number of entries * in the given Map or 11 (whichever is greater), and a default load factor, which is 0.75. * @param m a map containing the mappings to use */ public IndirectMap(Map m) { super(0); this.initialize(m); }
@Override public synchronized V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { if (get(key) != null) { V oldValue = get(key); V newValue = remappingFunction.apply(key, oldValue); if (newValue != null) { put(key, newValue); return newValue; } remove(key); } return null; } return getDelegate().computeIfPresent(key, remappingFunction); }
/** * @see java.util.Hashtable#clear() */ @Override public synchronized void clear() { if (hasTrackedPropertyChangeListener()) { Iterator<K> objects = this.keySet().iterator(); while (objects.hasNext()) { K o = objects.next(); objects.remove(); // should remove this to not fire same event twice this.raiseRemoveChangeEvent(o, this.get(o)); } } else { this.getDelegate().clear(); } }
@Override public synchronized boolean replace(K key, V oldValue, V newValue) { // Must trigger add events if tracked or uow. if (hasTrackedPropertyChangeListener()) { Map<K, V> del = getDelegate(); if (del.containsKey(key) && Objects.equals(del.get(key), oldValue)) { put(key, newValue); return true; } return false; } return getDelegate().replace(key, oldValue, newValue); }
/** * INTERNAL: * Return the real collection object. * This will force instantiation. */ public Object getDelegateObject() { return getDelegate(); }