001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency;
010:
011: import com.completex.objective.components.persistency.core.impl.LinkIterator;
012:
013: import java.io.IOException;
014: import java.io.ObjectInput;
015: import java.io.ObjectOutput;
016:
017: /**
018: * Allows for creation of Compound persistent objects "on fly". Can be used as return type
019: * as well as in "queries by example".
020: *
021: * @see AdHocPersistentObject
022: * @author Gennady Krizhevsky
023: */
024: public class CompoundPersistentObject extends AbstractPersistentObject
025: implements Compound {
026:
027: protected CompoundPersistentObject() {
028: }
029:
030: //
031: // Compound begin:
032: //
033: public boolean compound() {
034: return true;
035: }
036:
037: public CompoundPersistentObject(PersistentObject[] persistentObjects) {
038: this ();
039: this .persistentObjects = persistentObjects;
040: }
041:
042: public void assemble() {
043: persistentObjects[0].assemble();
044: }
045:
046: public void addChild(Link link) {
047: persistentObjects[0].addChild(link);
048: }
049:
050: public Link toLink() {
051: return persistentObjects[0].record().toLink();
052: }
053:
054: public LinkIterator linkIterator() {
055: return persistentObjects == null
056: || persistentObjects.length == 0 ? null
057: : persistentObjects[0].linkIterator();
058: }
059:
060: public Link child(String name) {
061: return persistentObjects == null
062: || persistentObjects.length == 0 ? null
063: : persistentObjects[0].child(name);
064: }
065:
066: public boolean hasChildren() {
067: return !(persistentObjects == null || persistentObjects.length == 0)
068: && persistentObjects[0].hasChildren();
069: }
070:
071: public Object getChildObject(String name) {
072: return persistentObjects == null
073: || persistentObjects.length == 0 ? null
074: : persistentObjects[0].getChildObject(name);
075: }
076:
077: public void setChildObject(String name, Object value) {
078: if (persistentObjects != null && persistentObjects.length > 0) {
079: persistentObjects[0].setChildObject(name, value);
080: }
081: }
082:
083: public Link[] inlineLinks() {
084: return persistentObjects == null
085: || persistentObjects.length == 0 ? null
086: : persistentObjects[0].inlineLinks();
087: }
088:
089: public Parameters toParameters(int[] parentIndeces) {
090: return persistentObjects == null
091: || persistentObjects.length == 0 ? null
092: : persistentObjects[0].record().toParameters(
093: parentIndeces);
094: }
095:
096: public AbstractPersistentObject newPersistentInstance() {
097: try {
098: PersistentObject[] persistentObjects = new PersistentObject[this .persistentObjects.length];
099: for (int i = 0; i < persistentObjects.length; i++) {
100: persistentObjects[i] = this .persistentObjects[i]
101: .newPersistentObject();
102: }
103: CompoundPersistentObject compoundPersistentObject = (CompoundPersistentObject) this
104: .getClass().newInstance();
105: compoundPersistentObject
106: .setPersistentObjects(persistentObjects);
107: return compoundPersistentObject;
108: } catch (Exception e) {
109: throw new RuntimeException(
110: "Cannot instantiate CompoundPersistentObject by class name ["
111: + this .getClass().getName() + "]", e);
112: }
113: }
114:
115: /**
116: * Returns copy of this object
117: *
118: * @return copy of this object
119: */
120: public Object clone() {
121: return newPersistentInstance();
122: }
123:
124: protected void setPersistentObjects(
125: PersistentObject[] persistentObjects) {
126: this .persistentObjects = persistentObjects;
127: }
128:
129: /**
130: * @deprecated use Compound.compoundEntries() instead
131: * @return PersistentObject[]
132: */
133: public PersistentObject[] getPersistentObjects() {
134: return compoundEntries();
135: }
136:
137: /**
138: * @deprecated use Compound.compoundEntry(i) instead
139: * @return PersistentObject
140: */
141: public PersistentObject get(int i) {
142: return compoundEntry(i);
143: }
144:
145: /**
146: * Empty
147: */
148: protected void selfReference() {
149: }
150:
151: /**
152: * Returns false
153: *
154: * @return false
155: */
156: public boolean selfReferencing() {
157: return false;
158: }
159:
160: //
161: // For external serialization:
162: //
163: /**
164: * Implementation of Externalizable interface
165: *
166: * @see java.io.Externalizable
167: * @param out ObjectOutput
168: * @throws IOException
169: */
170: public void writeExternal(ObjectOutput out) throws IOException {
171: out.writeObject(persistentObjects);
172: }
173:
174: /**
175: * Implementation of Externalizable interface
176: *
177: * @see java.io.Externalizable
178: * @param in ObjectInput
179: * @throws IOException
180: * @throws ClassNotFoundException
181: */
182: public void readExternal(ObjectInput in) throws IOException,
183: ClassNotFoundException {
184: persistentObjects = (PersistentObject[]) in.readObject();
185: }
186:
187: protected void flatten0() {
188: }
189:
190: protected void unflatten0() {
191: }
192:
193: public boolean emptyEntry(int index) {
194: if (index > compoundSize()) {
195: throw new IndexOutOfBoundsException("isEmptyEntry: index "
196: + index + " > compoundSize " + compoundSize());
197: }
198: return isEmptyEntry(compoundEntry(index));
199: }
200:
201: //
202: //
203: //
204: public String toString() {
205: StringBuffer buffer = new StringBuffer(super .toString());
206: if (persistentObjects != null) {
207: buffer.append(": ");
208: for (int i = 0; i < persistentObjects.length; i++) {
209: String comma = i == 0 ? "" : ", ";
210: buffer.append(comma).append(persistentObjects[i]);
211: }
212: }
213: return buffer.toString();
214: }
215:
216: }
|