001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo.sco.detached;
012:
013: import com.versant.core.jdo.VersantStateManager;
014: import com.versant.core.common.VersantFieldMetaData;
015: import com.versant.core.jdo.sco.VersantSimpleSCO;
016: import com.versant.core.jdo.sco.SCOIterator;
017: import com.versant.core.jdo.VersantStateManager;
018:
019: import javax.jdo.spi.PersistenceCapable;
020: import java.io.Serializable;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024:
025: import com.versant.core.common.BindingSupportImpl;
026:
027: /**
028: * Detached SCO for a HashSet.
029: */
030: public class DetachSCOHashSet extends HashSet implements Serializable,
031: VersantSimpleSCO {
032:
033: private PersistenceCapable owner;
034: private VersantStateManager stateManager;
035: private int fieldNo;
036:
037: public DetachSCOHashSet(PersistenceCapable owner,
038: VersantStateManager stateManager, VersantFieldMetaData fmd,
039: Object[] originalData) {
040: this .owner = owner;
041: this .stateManager = stateManager;
042: this .fieldNo = fmd.getManagedFieldNo();
043: int n = originalData == null ? 0 : originalData.length;
044: for (int i = 0; i < n; i++) {
045: Object o = originalData[i];
046: if (o == null)
047: throw createNPE();
048: super .add(o);
049: }
050: }
051:
052: private RuntimeException createNPE() {
053: return BindingSupportImpl.getInstance().nullElement(
054: "Null element not allowed.");
055: }
056:
057: public boolean add(Object o) {
058: if (o == null)
059: throw createNPE();
060: boolean result = super .add(o);
061: if (result)
062: makeDirty();
063: return result;
064: }
065:
066: public boolean remove(Object o) {
067: boolean result = super .remove(o);
068: if (result) {
069: makeDirty();
070: }
071: return result;
072: }
073:
074: public void clear() {
075: super .clear();
076: makeDirty();
077: }
078:
079: public boolean retainAll(Collection c) {
080: if (super .retainAll(c)) {
081: makeDirty();
082: return true;
083: }
084: return false;
085: }
086:
087: public boolean removeAll(Collection c) {
088: if (super .removeAll(c)) {
089: makeDirty();
090: return true;
091: }
092: return false;
093: }
094:
095: public boolean addAll(Collection c) {
096: if (super .addAll(c)) {
097: makeDirty();
098: return true;
099: }
100: return false;
101:
102: }
103:
104: public Iterator iterator() {
105: return new SCOIterator(super .iterator(), stateManager, owner,
106: fieldNo);
107: }
108:
109: private void makeDirty() {
110: if (stateManager != null && owner != null) {
111: stateManager.makeDirty(owner, fieldNo);
112: }
113: }
114:
115: public void makeTransient() {
116: owner = null;
117: stateManager = null;
118: }
119: }
|