KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > parser > JdoCollection


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metadata.parser;
13
14 import com.versant.core.metadata.MDStaticUtils;
15 import com.versant.core.common.Debug;
16 import com.versant.core.common.BindingSupportImpl;
17
18 import java.io.PrintStream JavaDoc;
19 import java.util.Collections JavaDoc;
20
21 /**
22  * Collection element from a .jdo file.
23  */

24 public final class JdoCollection extends JdoElement {
25
26     public String JavaDoc elementType;
27     public int embeddedElement;
28     public JdoExtension[] extensions;
29     public JdoField parent;
30
31     public JdoElement getParent() { return parent; }
32
33     /**
34      * Get information for this element to be used in building up a
35      * context string.
36      * @see #getContext
37      */

38     public String JavaDoc getSubContext() {
39         return "collection";
40     }
41
42     public String JavaDoc toString() {
43         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
44         s.append("collection elementType=");
45         s.append(elementType);
46         s.append(" embeddedElement=");
47         s.append(MDStaticUtils.toTriStateString(embeddedElement));
48         return s.toString();
49     }
50
51     public void dump() {
52         dump(Debug.OUT, "");
53     }
54
55     public void dump(PrintStream JavaDoc out, String JavaDoc indent) {
56         out.println(indent + this);
57         if (extensions != null) {
58             for (int i = 0; i < extensions.length; i++) {
59                 extensions[i].dump(out, indent + " ");
60             }
61         }
62     }
63
64     /**
65      * Get the fully qualified name of our element type (or null if none).
66      */

67     public String JavaDoc getElementTypeQName() {
68         if (elementType == null) return null;
69         int i = elementType.indexOf('.');
70         if (i >= 0) return elementType;
71         String JavaDoc packageName = parent.parent.parent.name;
72         if (packageName.length() == 0) return elementType;
73         return packageName + '.' + elementType;
74     }
75
76     public JdoCollection createCopy(JdoField parent) {
77         JdoCollection tmp = new JdoCollection();
78         tmp.parent = parent;
79         tmp.elementType = elementType;
80         tmp.embeddedElement = embeddedElement;
81
82         if (extensions != null) {
83             tmp.extensions = new JdoExtension[extensions.length];
84             for (int i = 0; i < extensions.length; i++) {
85                 tmp.extensions[i] = extensions[i].createCopy(tmp);
86             }
87         }
88         return tmp;
89     }
90
91     /**
92      *
93      */

94     public void synchronizeForHorizontal(JdoCollection col) {
95         if (!elementType.equals(col.elementType)) {
96             throw BindingSupportImpl.getInstance().invalidOperation(
97                     "May not override the element type of collection. " +
98                     "\nAttemptin to change 'element-type' from "
99                     + col.elementType + " to " + elementType);
100         }
101
102         if (col.extensions != null) {
103             JdoExtension[] copy = new JdoExtension[col.extensions.length];
104             for (int i = 0; i < col.extensions.length; i++) {
105                 copy[i] = col.extensions[i].createCopy(this);
106             }
107             if (extensions != null) {
108                 JdoExtension.synchronize3(extensions, copy, Collections.EMPTY_SET, false);
109             } else {
110                 extensions = copy;
111             }
112         }
113     }
114
115     /**
116      * Create the extension if it does not exist, else update it if overwrite is true
117      * @param key
118      * @param value
119      * @param overwrite
120      */

121     public JdoExtension findCreate(int key, String JavaDoc value, boolean overwrite) {
122         if (extensions == null) {
123             extensions = new JdoExtension[] {createChild(key, value, this)};
124             return extensions[0];
125         }
126
127         JdoExtension ext = JdoExtension.find(key, extensions);
128         if (ext == null) {
129             extensions = addExtension(extensions, (ext = createChild(key, value, this)));
130         } else if (overwrite) {
131             ext.value = value;
132         }
133         return ext;
134     }
135 }
136
137
Popular Tags