001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.masterindex.plugin.datamodel;
042:
043: import java.util.ArrayList;
044:
045: /**
046: *
047: * this represent the ObjectDefinition or metaData for a given DataObject
048: *
049: *
050: * @author Sujit Biswas
051: *
052: */
053: public class ObjectDefinition {
054:
055: private String name;
056:
057: /**
058: * list of fields
059: */
060: ArrayList<Field> fields = new ArrayList<Field>();
061:
062: /**
063: * list of children
064: */
065: ArrayList<ObjectDefinition> children = new ArrayList<ObjectDefinition>();
066:
067: /**
068: * @param name
069: */
070: public ObjectDefinition(String name) {
071: super ();
072: this .name = name;
073: }
074:
075: /**
076: * @param name
077: * @param fields
078: * @param children
079: */
080: public ObjectDefinition(String name, ArrayList<Field> fields,
081: ArrayList<ObjectDefinition> children) {
082: super ();
083: this .name = name;
084: this .fields = fields;
085: this .children = children;
086: }
087:
088: /**
089: * add field
090: *
091: * @param o
092: * @return
093: */
094: public boolean addField(Field o) {
095: return fields.add(o);
096: }
097:
098: /**
099: * add field at a specified location, shift the elements from the current
100: * position to the right
101: *
102: * @param index
103: * @param element
104: */
105: public void addField(int index, Field element) {
106: fields.add(index, element);
107: }
108:
109: private void ensureFieldCapacity(int minCapacity) {
110: int size = minCapacity + 1;
111:
112: if (fields.size() < size) {
113: for (int i = fields.size(); i < size; i++) {
114: fields.add(i, null);
115: }
116: }
117:
118: }
119:
120: /**
121: * get field for the given index
122: *
123: * @param index
124: * @return
125: */
126: public Field getField(int index) {
127: return fields.get(index);
128: }
129:
130: /**
131: * set the field at the given index
132: *
133: * @param index
134: * @param element
135: * @return
136: */
137: public Field setField(int index, Field element) {
138: ensureFieldCapacity(index);
139: return fields.set(index, element);
140: }
141:
142: /**
143: * add child at the given index
144: *
145: * @param index
146: * @param element
147: */
148: public void addchild(int index, ObjectDefinition element) {
149: children.add(index, element);
150: }
151:
152: /**
153: * add child
154: *
155: * @param o
156: * @return
157: */
158: public boolean addchild(ObjectDefinition o) {
159: return children.add(o);
160: }
161:
162: /**
163: * get child at the given index
164: *
165: * @param index
166: * @return
167: */
168: public ObjectDefinition getchild(int index) {
169: return children.get(index);
170: }
171:
172: /**
173: * set child at the given index
174: *
175: * @param index
176: * @param element
177: * @return
178: */
179: public ObjectDefinition setchild(int index, ObjectDefinition element) {
180: ensureChildCapacity(index);
181: return children.set(index, element);
182: }
183:
184: private void ensureChildCapacity(int minCapacity) {
185: int size = minCapacity + 1;
186:
187: if (children.size() < size) {
188: for (int i = children.size(); i < size; i++) {
189: children.add(i, null);
190: }
191: }
192:
193: }
194:
195: /**
196: * @return the children
197: */
198: public ArrayList<ObjectDefinition> getChildren() {
199: return children;
200: }
201:
202: /**
203: * @param children
204: * the children to set
205: */
206: public void setChildren(ArrayList<ObjectDefinition> children) {
207: this .children = children;
208: }
209:
210: /**
211: * @return the fields
212: */
213: public ArrayList<Field> getFields() {
214: return fields;
215: }
216:
217: /**
218: * @param fields
219: * the fields to set
220: */
221: public void setFields(ArrayList<Field> fields) {
222: this .fields = fields;
223: }
224:
225: /**
226: * @return the name
227: */
228: public String getName() {
229: return name;
230: }
231:
232: /**
233: * @param name
234: * the name to set
235: */
236: public void setName(String name) {
237: this .name = name;
238: }
239:
240: /*
241: * (non-Javadoc)
242: *
243: * @see java.lang.Object#toString()
244: */
245: @Override
246: public String toString() {
247: StringBuilder sb = new StringBuilder();
248:
249: sb.append("name: " + name + "\n");
250:
251: sb.append(" Fields: \n");
252: sb.append(fields.toString());
253:
254: if (!children.isEmpty())
255: sb.append("\nChildren: ");
256:
257: for (ObjectDefinition c : children) {
258:
259: sb.append("\n child ");
260: if (c != null)
261: sb.append(c.toString());
262: }
263:
264: return sb.toString();
265: }
266:
267: }
|