001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022: package net.sf.jmoney.model2;
023:
024: import java.lang.reflect.Field;
025:
026: /**
027: * This class is the base class from which all classes that implement
028: * extension property sets should be derived.
029:
030: * @author Nigel Westbury
031: */
032: public abstract class ExtensionObject {
033: protected ExtendableObject baseObject;
034: protected PropertySet propertySet;
035:
036: public ExtensionObject(ExtendableObject extendedObject) {
037: this .baseObject = extendedObject;
038: }
039:
040: void setPropertySet(PropertySet propertySet) {
041: this .propertySet = propertySet;
042: }
043:
044: public IObjectKey getObjectKey() {
045: return baseObject.getObjectKey();
046: }
047:
048: public Session getSession() {
049: return baseObject.getSession();
050: }
051:
052: public DataManager getDataManager() {
053: return baseObject.getDataManager();
054: }
055:
056: /**
057: * Two or more instantiated objects may represent the same object
058: * in the datastore. Such objects should be considered
059: * the same. Therefore this method overrides the default
060: * implementation that is based on Java identity.
061: * <P>
062: * This method also considers two objects to be the same if either
063: * or both of the objects are extension objects and the underlying
064: * objects are the same.
065: * <P>
066: * @return true if the two objects represent the same object
067: * in the datastore, false otherwise.
068: */
069: // TODO: Is this method really correct?
070: // If so, we need a hashcode also.
071: @Override
072: public boolean equals(Object object) {
073: return baseObject.equals(object);
074: }
075:
076: public <E extends ExtensionObject> E getExtension(
077: ExtensionPropertySet<E> propertySet,
078: boolean alwaysReturnNonNull) {
079: return baseObject
080: .getExtension(propertySet, alwaysReturnNonNull);
081: }
082:
083: public <V> V getPropertyValue(
084: ScalarPropertyAccessor<V> propertyAccessor) {
085: return baseObject.getPropertyValue(propertyAccessor);
086: }
087:
088: public <E extends ExtendableObject> ObjectCollection<E> getListPropertyValue(
089: ListPropertyAccessor<E> propertyAccessor) {
090: return baseObject.getListPropertyValue(propertyAccessor);
091: }
092:
093: public <V> void setPropertyValue(
094: ScalarPropertyAccessor<V> propertyAccessor, V value) {
095: baseObject.setPropertyValue(propertyAccessor, value);
096: }
097:
098: protected <V> void processPropertyChange(
099: ScalarPropertyAccessor<V> propertyAccessor, V oldValue,
100: V newValue) {
101: baseObject.processPropertyChange(propertyAccessor, oldValue,
102: newValue);
103: }
104:
105: /**
106: * This method is used to enable other classes in the package to
107: * access protected fields in the extension objects.
108: *
109: * @param theObjectKeyField
110: * @return
111: */
112: Object getProtectedFieldValue(Field theObjectKeyField) {
113: try {
114: return theObjectKeyField.get(this );
115: } catch (IllegalArgumentException e) {
116: e.printStackTrace();
117: throw new RuntimeException("internal error");
118: } catch (IllegalAccessException e) {
119: e.printStackTrace();
120: // TODO: check the protection earlier and raise MalformedPlugin
121: throw new RuntimeException(
122: "internal error - field protection problem");
123: }
124: }
125: }
|