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:
018: import javax.jdo.spi.PersistenceCapable;
019: import java.io.Serializable;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.TreeSet;
023:
024: import com.versant.core.common.BindingSupportImpl;
025:
026: /**
027: * Detached TreeSet SCO.
028: */
029: public class DetachSCOTreeSet extends TreeSet implements Serializable,
030: VersantSimpleSCO {
031:
032: private PersistenceCapable owner;
033: private VersantStateManager stateManager;
034: private int fieldNo;
035:
036: public DetachSCOTreeSet(PersistenceCapable owner,
037: VersantStateManager stateManager, VersantFieldMetaData fmd,
038: Object[] originalData) {
039: super (fmd.getComparator());
040: this .owner = owner;
041: this .stateManager = stateManager;
042: this .fieldNo = fmd.getManagedFieldNo();
043: int n = originalData == null ? 0 : originalData.length;
044: if (!owner.jdoIsNew()) {
045: for (int i = 0; i < n; i++) {
046: Object o = originalData[i];
047: if (o == null)
048: break;
049: super .add(o);
050: }
051: } else {
052: for (int i = 0; i < n; i++) {
053: Object o = originalData[i];
054: if (o == null)
055: throw createNPE();
056: super .add(o);
057: }
058: }
059: }
060:
061: private RuntimeException createNPE() {
062: return BindingSupportImpl.getInstance().nullElement(
063: "Null element not allowed.");
064: }
065:
066: public boolean add(Object o) {
067: if (o == null)
068: throw createNPE();
069: boolean result = super .add(o);
070: if (result)
071: makeDirty();
072: return result;
073: }
074:
075: public boolean remove(Object o) {
076: boolean result = super .remove(o);
077: if (result) {
078: makeDirty();
079: }
080: return result;
081: }
082:
083: public void clear() {
084: super .clear();
085: makeDirty();
086: }
087:
088: public boolean retainAll(Collection c) {
089: if (super .retainAll(c)) {
090: makeDirty();
091: return true;
092: }
093: return false;
094: }
095:
096: public boolean removeAll(Collection c) {
097: if (super .removeAll(c)) {
098: makeDirty();
099: return true;
100: }
101: return false;
102: }
103:
104: public boolean addAll(Collection c) {
105: if (super .addAll(c)) {
106: makeDirty();
107: return true;
108: }
109: return false;
110:
111: }
112:
113: public Iterator iterator() {
114: return new SCOIterator(super .iterator(), stateManager, owner,
115: fieldNo);
116: }
117:
118: private void makeDirty() {
119: if (stateManager != null && owner != null) {
120: stateManager.makeDirty(owner, fieldNo);
121: }
122: }
123:
124: public void makeTransient() {
125: owner = null;
126: stateManager = null;
127: }
128: }
|