/** * Method to remove an element from the ArrayList. * * @param index The element position. * @return The object that was removed */ public synchronized Object remove(int index) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); Object delegateObject = (useCache ? delegate.remove(index) : null); Object backingObject = null; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingObject = delegateObject; ownerOP .getExecutionContext() .addOperationToQueue(new ListRemoveAtOperation(ownerOP, backingStore, index)); } else { try { backingObject = backingStore.remove(ownerOP, index, size); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( Localiser.msg("023013", "remove", ownerMmd.getName(), dse)); backingObject = null; } } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingObject : delegateObject); }
/** * Wrapper addition that allows turning off of the dependent-field checks when doing the position * setting. This means that we can prevent the deletion of the object that was previously in that * position. This particular feature is used when attaching a list field and where some elements * have changed positions. * * @param index The position * @param element The new element * @return The element previously at that position */ public Object set(int index, Object element, boolean allowDependentField) { // Reject inappropriate elements if (!allowNulls && element == null) { throw new NullPointerException( "Nulls not allowed for collection at field " + ownerMmd.getName() + " but element is null"); } makeDirty(); if (useCache) { loadFromStore(); } Object delegateReturn = delegate.set(index, element); if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue( new ListSetOperation(ownerOP, backingStore, index, element, allowDependentField)); } else { backingStore.set(ownerOP, index, element, allowDependentField); } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return delegateReturn; }
/** * Accessor for the subList of elements between from and to of the List * * @param from Start index (inclusive) * @param to End index (exclusive) * @return The subList */ public synchronized java.util.List subList(int from, int to) { if (useCache) { loadFromStore(); } else if (backingStore != null) { return backingStore.subList(ownerOP, from, to); } return delegate.subList(from, to); }
/** * Method to retrieve the last position of the element. * * @param element The element * @return The last position of this element in the List. */ public int lastIndexOf(Object element) { if (useCache) { loadFromStore(); } else if (backingStore != null) { return backingStore.lastIndexOf(ownerOP, element); } return delegate.lastIndexOf(element); }
/** * Method to retrieve an element no. * * @param index The item to retrieve * @return The element at that position. */ public Object get(int index) { if (useCache) { loadFromStore(); } else if (backingStore != null) { return backingStore.get(ownerOP, index); } return delegate.get(index); }
/** * Accessor for the size of the ArrayList. * * @return The size. */ public int size() { if (useCache && isCacheLoaded) { // If the "delegate" is already loaded, use it return delegate.size(); } else if (backingStore != null) { return backingStore.size(ownerOP); } return delegate.size(); }
/** * Method to return if the list contains this element. * * @param element The element * @return Whether it is contained */ public boolean contains(Object element) { if (useCache && isCacheLoaded) { // If the "delegate" is already loaded, use it return delegate.contains(element); } else if (backingStore != null) { return backingStore.contains(ownerOP, element); } return delegate.contains(element); }
/** * Method to remove a collection of elements from the List. * * @param elements Collection of elements to remove * @return Whether it was successful. */ public boolean removeAll(Collection elements) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); Collection contained = null; if (backingStore != null && SCOUtils.useQueuedUpdate(queued, ownerOP)) { // Check which are contained before updating the delegate contained = new java.util.HashSet(); for (Object elem : elements) { if (contains(elem)) { contained.add(elem); } } } boolean delegateSuccess = delegate.removeAll(elements); if (backingStore != null) { boolean backingSuccess = true; if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingSuccess = false; for (Object element : contained) { backingSuccess = true; ownerOP .getExecutionContext() .addOperationToQueue( new CollectionRemoveOperation(ownerOP, backingStore, element, true)); } } else { try { backingSuccess = backingStore.removeAll(ownerOP, elements, size); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( Localiser.msg("023013", "removeAll", ownerMmd.getName(), dse)); backingSuccess = false; } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return backingSuccess; } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return delegateSuccess; }
/** Method to load all elements from the "backing store" where appropriate. */ protected void loadFromStore() { if (backingStore != null && !isCacheLoaded) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg("023006", ownerOP.getObjectAsPrintable(), ownerMmd.getName())); } delegate.clear(); Iterator iter = backingStore.iterator(ownerOP); while (iter.hasNext()) { delegate.add(iter.next()); } isCacheLoaded = true; } }
/** Method to clear the ArrayList. */ public synchronized void clear() { makeDirty(); delegate.clear(); if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore)); } else { backingStore.clear(ownerOP); } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } }
/** * Method to add an element to the ArrayList. * * @param element The new element * @return Whether it was added ok. */ public boolean add(Object element) { // Reject inappropriate elements if (!allowNulls && element == null) { throw new NullPointerException( "Nulls not allowed for collection at field " + ownerMmd.getName() + " but element is null"); } if (useCache) { loadFromStore(); } boolean backingSuccess = true; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } else { try { backingSuccess = backingStore.add(ownerOP, element, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { throw new IllegalArgumentException( Localiser.msg("023013", "add", ownerMmd.getName(), dse), dse); } } } // Only make it dirty after adding the element(s) to the datastore so we give it time // to be inserted - otherwise jdoPreStore on this object would have been called before // completing the addition makeDirty(); boolean delegateSuccess = delegate.add(element); if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** * Method to remove an element from the collection, and observe the flag for whether to allow * cascade delete. * * @param element The element * @param allowCascadeDelete Whether to allow cascade delete */ public synchronized boolean remove(Object element, boolean allowCascadeDelete) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); boolean contained = delegate.contains(element); boolean delegateSuccess = delegate.remove(element); boolean backingSuccess = true; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingSuccess = contained; if (backingSuccess) { ownerOP .getExecutionContext() .addOperationToQueue( new CollectionRemoveOperation( ownerOP, backingStore, element, allowCascadeDelete)); } } else { try { backingSuccess = backingStore.remove(ownerOP, element, size, allowCascadeDelete); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( Localiser.msg("023013", "remove", ownerMmd.getName(), dse)); backingSuccess = false; } } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** * Method to add a Collection to a position in the ArrayList. * * @param index Position to insert the collection. * @param elements The collection * @return Whether it was added ok. */ public boolean addAll(int index, Collection elements) { if (useCache) { loadFromStore(); } boolean backingSuccess = true; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { int pos = index; for (Object element : elements) { ownerOP .getExecutionContext() .addOperationToQueue(new ListAddAtOperation(ownerOP, backingStore, pos++, element)); } } else { try { backingSuccess = backingStore.addAll(ownerOP, elements, index, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { throw new IllegalArgumentException( Localiser.msg("023013", "addAll", ownerMmd.getName(), dse), dse); } } } // Only make it dirty after adding the element(s) to the datastore so we give it time // to be inserted - otherwise jdoPreStore on this object would have been called before // completing the addition makeDirty(); boolean delegateSuccess = delegate.addAll(index, elements); if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** * Method to update an embedded element in this list. * * @param element The element * @param fieldNumber Number of field in the element * @param value New value for this field */ public void updateEmbeddedElement(Object element, int fieldNumber, Object value) { if (backingStore != null) { backingStore.updateEmbeddedElement(ownerOP, element, fieldNumber, value); } }
/** * Method to initialise the SCO from an existing value. * * @param c The object to set from * @param forInsert Whether the object needs inserting in the datastore with this value * @param forUpdate Whether to update the SCO in the datastore with this value */ public void initialise(java.util.ArrayList c, boolean forInsert, boolean forUpdate) { if (c != null) { // Check for the case of serialised PC elements, and assign ObjectProviders to the elements // without if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) { ExecutionContext ec = ownerOP.getExecutionContext(); Iterator iter = c.iterator(); while (iter.hasNext()) { Object pc = iter.next(); ObjectProvider objOP = ec.findObjectProvider(pc); if (objOP == null) { objOP = ec.getNucleusContext() .getObjectProviderFactory() .newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber()); } } } if (backingStore != null && useCache && !isCacheLoaded) { // Mark as loaded so we just use the value isCacheLoaded = true; } if (forInsert) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } if (useCache) { loadFromStore(); } if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { for (Object element : c) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } } else { try { backingStore.addAll(ownerOP, c, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( Localiser.msg("023013", "addAll", ownerMmd.getName(), dse)); } } } makeDirty(); delegate.addAll(c); } else if (forUpdate) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } // TODO This does clear+addAll : Improve this and work out which elements are added and // which deleted if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore)); } else { backingStore.clear(ownerOP); } } if (useCache) { loadFromStore(); } if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { for (Object element : c) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } } else { try { backingStore.addAll(ownerOP, c, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( Localiser.msg("023013", "addAll", ownerMmd.getName(), dse)); } } } delegate.addAll(c); makeDirty(); } else { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } delegate.clear(); delegate.addAll(c); } } }