001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2005-2007 New York University
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019:
020: package xtc.xform;
021:
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: /**
027: * A tree item contained in a sequence.
028: *
029: * @author Joe Pamer
030: * @version $Revision: 1.18 $
031: */
032: public class Item {
033: /** The tree item object. */
034: Object object;
035:
036: /** The tree item's parent. */
037: Item parent;
038:
039: /** The item's child index in the parent item. */
040: int index;
041:
042: /** The children of this item */
043: List<Item> children;
044:
045: /**
046: * Create a new item.
047: *
048: */
049: public Item() {
050: object = null;
051: parent = null;
052: children = null;
053: index = 0;
054: }
055:
056: /**
057: * Create a new item.
058: *
059: * @param object The item object.
060: * @param parent The item's parent.
061: * @param index The item's child index.
062: */
063: public Item(Object object, Item parent, int index) {
064: this .object = object;
065: this .parent = parent;
066: this .index = index;
067: this .children = null;
068: }
069:
070: /**
071: * Create a new item.
072: *
073: * @param object The item object.
074: * @param parent The item's parent.
075: * @param index The item's child index.
076: * @param children The item's child items.
077: */
078: public Item(Object object, Item parent, int index,
079: List<Item> children) {
080: this .object = object;
081: this .parent = parent;
082: this .index = index;
083: addChildren(children);
084: }
085:
086: /**
087: * Create a new that is a copy of an existing item.
088: *
089: * @param i The item object.
090: */
091: public Item(Item i) {
092: this .object = i.object;
093: this .parent = i.parent;
094: this .index = i.index;
095: addChildren(i.children);
096: }
097:
098: /**
099: * Get the item's object.
100: *
101: * @return The item's object.
102: */
103: public Object getObject() {
104: return this .object;
105: }
106:
107: /**
108: * Get the item's parent.
109: *
110: * @return The item's parent item.
111: */
112: public Item getParent() {
113: return this .parent;
114: }
115:
116: /**
117: * Get the item's index.
118: *
119: * @return The item's index.
120: */
121: public int getIndex() {
122: return this .index;
123: }
124:
125: /**
126: * Get the item's children.
127: *
128: * @return The item's children.
129: */
130: public List<Item> getChildren() {
131: return this .children;
132: }
133:
134: /**
135: * Test for equality.
136: *
137: * @param o The object to test for equality.
138: * @return True if o is equal, false otherwise.
139: */
140: public boolean equals(Object o) {
141: if (o instanceof Item) {
142: Item other = (Item) o;
143: if ((this .object == other.object)
144: && (this .parent == other.parent)
145: && (this .index == other.index)) {
146: return true;
147: } else {
148: return false;
149: }
150: } else {
151: return false;
152: }
153: }
154:
155: /**
156: * Add a new child to the item.
157: *
158: * @param child The new child item.
159: */
160: public void addChild(Item child) {
161: if (null == this .children) {
162: this .children = new LinkedList<Item>();
163: }
164:
165: child.parent = this ;
166: child.index = this .children.size();
167:
168: this .children.add(child);
169: }
170:
171: /**
172: * Add a new child at the specified index.
173: *
174: * @param child The new child item.
175: * @param index The index.
176: */
177: public void addChild(Item child, int index) {
178: if (null == this .children) {
179: if (0 == index) {
180: this .children = new LinkedList<Item>();
181: } else {
182: throw new RuntimeException(
183: "Error: Attempted to add first child at "
184: + "index greater than 0.");
185: }
186: }
187: child.parent = this ;
188: child.index = index;
189:
190: this .children.add(index, child);
191:
192: // adjust the other children's indices
193: for (int newidx = index + 1; newidx < this .children.size(); newidx++) {
194: (this .children.get(newidx)).index = newidx;
195: }
196: }
197:
198: /**
199: * Add a collection of new children to an item.
200: *
201: * @param children The new children.
202: */
203: public void addChildren(List<Item> children) {
204: if (null == children) {
205: return;
206: } else if (null == this .children) {
207: this .children = new LinkedList<Item>();
208: }
209:
210: int index = this .children.size();
211:
212: for (Iterator<Item> child_iterator = children.iterator(); child_iterator
213: .hasNext(); index++) {
214: Item child_item = child_iterator.next();
215: child_item.index = index;
216: child_item.parent = this ;
217: this .children.add(child_item);
218: }
219: }
220:
221: /**
222: * Insert a collection of new children at the specified index.
223: *
224: * @param children The children to insert.
225: * @param index The index to insert at.
226: */
227: public void addChildren(List<Item> children, int index) {
228: if (null == this .children) {
229: if (0 == index) {
230: this .children = new LinkedList<Item>();
231: } else {
232: throw new RuntimeException(
233: "Error: Attempted to add first children at"
234: + " an index greater than 0.");
235: }
236: }
237:
238: int insert_index = index;
239:
240: for (Iterator<Item> child_iterator = children.iterator(); child_iterator
241: .hasNext(); index++) {
242:
243: Item child_item = child_iterator.next();
244: child_item.parent = this ;
245: child_item.index = index;
246: }
247:
248: this .children.addAll(insert_index, children);
249:
250: // adjust old children's indices
251: while (index < this .children.size()) {
252: (this .children.get(index)).index = index;
253: index++;
254: }
255: }
256:
257: /**
258: * Remove the child at the specified index.
259: *
260: * @param index The index of the child to eliminate.
261: */
262: public void removeChild(int index) {
263: this .children.remove(index);
264:
265: // adjust the indices of the remaining children
266: while (index < this .children.size()) {
267: (this .children.get(index)).index = index;
268: index++;
269: }
270: }
271:
272: /**
273: * Replace the item at the specified index with the given item.
274: *
275: * @param index The index of the child to be replaced.
276: * @param item The item to make the replacement with.
277: */
278: public void replaceChild(int index, Item item) {
279: item.parent = this ;
280: item.index = index;
281:
282: this .children.set(index, item);
283: }
284:
285: /**
286: * Replace the item at the specified index with the given list of items.
287: *
288: * @param index The index of the child to be replaced.
289: * @param items The items to make the replacement with.
290: */
291: public void replaceChild(int index, List<Item> items) {
292: removeChild(index);
293: addChildren(items, index);
294: }
295:
296: /**
297: * Add to a list (only if it's not already a member of the list).
298: *
299: * @param list The list to be added to.
300: * @return The potentially modified list.
301: */
302: public List<Object> addToList(List<Object> list) {
303: if (!list.contains(this )) {
304: list.add(this );
305: }
306: return list;
307: }
308:
309: /**
310: * Return the item as a string value.
311: *
312: * @return The item as a string.
313: */
314: public String toString() {
315: if (null == this .object) {
316: return "null";
317: } else if (this .object instanceof String) {
318: return (String) this .object;
319: } else {
320: return this .object.toString();
321: }
322: }
323:
324: } // end class Item
|