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.MapData;
017: import com.versant.core.jdo.VersantStateManager;
018:
019: import javax.jdo.spi.PersistenceCapable;
020: import java.io.Serializable;
021: import java.util.Map;
022: import java.util.TreeMap;
023:
024: /**
025: * Detached SCO for TreeMap.
026: */
027: public class DetachSCOTreeMap extends TreeMap implements Serializable,
028: VersantSimpleSCO {
029:
030: private PersistenceCapable owner;
031: private VersantStateManager stateManager;
032: private int fieldNo;
033:
034: private DetachSCOTreeMap(PersistenceCapable owner,
035: VersantFieldMetaData fmd) {
036: super (fmd.getComparator());
037: this .owner = owner;
038: this .fieldNo = fmd.getManagedFieldNo();
039: }
040:
041: public DetachSCOTreeMap(PersistenceCapable owner,
042: VersantStateManager stateManager, VersantFieldMetaData fmd,
043: Map beforeMap) {
044: this (owner, fmd);
045: putAll(beforeMap);
046: this .stateManager = stateManager;
047: }
048:
049: public DetachSCOTreeMap(PersistenceCapable owner,
050: VersantStateManager stateManager, VersantFieldMetaData fmd,
051: MapData mapData) {
052: this (owner, fmd);
053: int n = mapData.entryCount;
054: Object[] keys = mapData.keys;
055: Object[] values = mapData.values;
056: for (int i = 0; i < n; i++) {
057: put(keys[i], values[i]);
058: }
059: this .stateManager = stateManager;
060: }
061:
062: public Class getJavaType() {
063: return TreeMap.class;
064: }
065:
066: public Object remove(Object key) {
067: return checkModified(super .remove(key));
068: }
069:
070: public Object put(Object key, Object value) {
071: makeDirty();
072: return super .put(key, value);
073: }
074:
075: public void putAll(Map t) {
076: makeDirty();
077: super .putAll(t);
078: }
079:
080: public void clear() {
081: final int size = size();
082: super .clear();
083: if (size != 0)
084: makeDirty();
085: }
086:
087: public Object getOwner() {
088: return owner;
089: }
090:
091: public void makeTransient() {
092: owner = null;
093: stateManager = null;
094: }
095:
096: public void makeDirty() {
097: if (stateManager != null && owner != null) {
098: stateManager.makeDirty(owner, fieldNo);
099: }
100: }
101:
102: private Object checkModified(Object obj) {
103: if (obj != null) {
104: makeDirty();
105: }
106: return obj;
107: }
108: }
|