001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: NodeList.java$
021: * $FileID: 4340$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:23 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.project;
032:
033: /**
034: * Title: SourceJammer v 0.1.0
035: * Description:
036: * Copyright: Copyright (c) 2001
037: * Company:
038: * @author Robert MacGrogan
039: * @version $Revision: 1.3 $
040: */
041:
042: import java.util.Vector;
043: import java.util.Hashtable;
044: import java.util.Enumeration;
045:
046: import org.sourcejammer.project.view.NodeInfo;
047:
048: /**
049: * A linked and indexed colletion of Nodes. Each node within
050: * a NodeList must have a unique name.
051: */
052: public class NodeList {
053:
054: private Vector mvecNodes;
055: private Hashtable mhshIndex;
056: private Hashtable mhshNames;
057:
058: public NodeList() {
059: mvecNodes = new Vector();
060: mhshIndex = new Hashtable();
061: mhshNames = new Hashtable();
062: }
063:
064: /**
065: * Provided to dump contents of NodeList to an array of
066: * Nodes. This array can be passed more easily using SOAP.
067: */
068: public Node[] getContentsAsArray() {
069: int iSize = mvecNodes.size();
070: Node[] ndArray = new Node[iSize];
071: mvecNodes.toArray(ndArray);
072: return ndArray;
073: }
074:
075: /**
076: * Provided to set this NodeList using an array of Nodes. Note that
077: * calling this method will empty out any previously set nodes from the NodeList.
078: *
079: * @exception NodeExistsException if the array contains more than one node with
080: * the same nodename.
081: */
082: public void setContentsFromArray(Node[] nodes)
083: throws NodeExistsException {
084: mvecNodes = new Vector();
085: mhshIndex = new Hashtable();
086: mhshNames = new Hashtable();
087: for (int i = 0; i < nodes.length; i++) {
088: addNode(nodes[i]);
089: }
090: }
091:
092: /**
093: * Adds a node to the list.
094: *
095: * @param node -- the Node to be added.
096: *
097: * @exception NodeExistsException if a Node with the same name
098: * is already in the NodeList.
099: */
100: public void addNode(Node node) throws NodeExistsException {
101: if (mhshIndex.get(node.getNodeName()) == null) {
102: Integer intIndex = new Integer(mvecNodes.size());
103: //Index by name
104: mhshIndex.put(node.getNodeName(), intIndex);
105: //Name by index
106: mhshNames.put(intIndex, node.getNodeName());
107: //Store Node
108: mvecNodes.add(node);
109: } else {
110: throw new NodeExistsException(
111: "The NodeList already contains a Node with the name: "
112: + node.getNodeName() + ".");
113: }
114: }
115:
116: /**
117: * Returns a Node based on it's index value.
118: */
119: public Node getNode(int index) throws NodeDoesNotExistException {
120: Node ndReturn = (Node) mvecNodes.get(index);
121: if (ndReturn == null) {
122: throw new NodeDoesNotExistException(
123: "The NodeList does not contain a Node at index: "
124: + index + ".");
125: }
126: return ndReturn;
127: }
128:
129: /**
130: * Returns a Node based on it's unique name.
131: */
132: public Node getNode(String nodeName)
133: throws NodeDoesNotExistException {
134: Integer intIndex = (Integer) mhshIndex.get(nodeName);
135: Node ndReturn = null;
136: if (intIndex != null) {
137: ndReturn = (Node) mvecNodes.get(intIndex.intValue());
138: } else {
139: throw new NodeDoesNotExistException(
140: "The NodeList does not contain a Node with the name: "
141: + nodeName + ".");
142: }
143: return ndReturn;
144: }
145:
146: /**
147: * Removes a Node from the NodeList based on it's index value.
148: *
149: * @param index -- index of the Node to be removed.
150: * @return the Node that is being removed.
151: * @exception NodeDoesNotExistException -- if there is no node
152: * at the specified index.
153: */
154: public Node removeNode(int index) throws NodeDoesNotExistException {
155: Node ndRemoved = null;
156: Integer intIndex = new Integer(index);
157: String name = (String) mhshNames.get(intIndex);
158: if (name != null) {
159: //Remove name
160: mhshNames.remove(intIndex);
161: //Remove index
162: mhshIndex.remove(name);
163: //Remove Node
164: ndRemoved = (Node) mvecNodes.remove(intIndex.intValue());
165: reindex();
166: } else {
167: throw new NodeDoesNotExistException(
168: "The NodeList does not contain a Node at index: "
169: + index + ".");
170: }
171: return ndRemoved;
172: }
173:
174: /**
175: * Removes a Node from the NodeList based on it's unique name.
176: *
177: * @param name -- name of the Node to be removed.
178: * @return the Node that is being removed.
179: * @exception NodeDoesNotExistException -- if there is no node
180: * with the specified name.
181: */
182: public Node removeNode(String nodeName)
183: throws NodeDoesNotExistException {
184: Node ndRemoved = null;
185: Integer intIndex = (Integer) mhshIndex.get(nodeName);
186: if (intIndex != null) {
187: ndRemoved = removeNode(intIndex.intValue());
188: } else {
189: throw new NodeDoesNotExistException(
190: "The NodeList does not contain a Node with the name: "
191: + nodeName + ".");
192: }
193: return ndRemoved;
194:
195: }
196:
197: private void reindex() {
198: Enumeration enmNodes = mvecNodes.elements();
199: mhshIndex = new Hashtable();
200: mhshNames = new Hashtable();
201: int iCounter = 0;
202: while (enmNodes.hasMoreElements()) {
203: Node nd = (Node) enmNodes.nextElement();
204: Integer intIndex = new Integer(iCounter);
205: String sName = nd.getNodeName();
206: mhshNames.put(intIndex, sName);
207: mhshIndex.put(sName, intIndex);
208: iCounter++;
209: }
210: }
211:
212: /**
213: * Returns a NodeIterator for iterating through the contents of this
214: * NodeList.
215: */
216: public NodeIterator getIterator() {
217: NodeIterator oIterator = new NodeListIterator(mvecNodes);
218: return oIterator;
219: }
220:
221: /**
222: * Returns the number of nodes in the ilst.
223: */
224: public int size() {
225: return mvecNodes.size();
226: }
227:
228: /**
229: * Returns the names of nodes in the list.
230: */
231: public String[] getListNames() {
232: String[] names = new String[mvecNodes.size()];
233: for (int i = 0; i < mvecNodes.size(); i++) {
234: Node node = (Node) mvecNodes.get(i);
235: names[i] = node.getNodeName();
236: }
237: return names;
238:
239: }
240:
241: /*
242: public static void main(String args[]){
243: try {
244: System.out.println("Let's build an array and call setContents.");
245:
246: NodeInfo oInfo1 = new NodeInfo();
247: oInfo1.setNodeName("One");
248:
249: NodeInfo oInfo2 = new NodeInfo();
250: oInfo2.setNodeName("Two");
251:
252: NodeInfo oInfo3 = new NodeInfo();
253: oInfo3.setNodeName("Three");
254:
255: NodeInfo[] ndArray = new NodeInfo[3];
256:
257: ndArray[0] = oInfo1;
258: ndArray[1] = oInfo2;
259: ndArray[2] = oInfo3;
260:
261: NodeList oList = new NodeList();
262: oList.setContentsFromArray(ndArray);
263:
264: System.out.println("Okay, now to get the array.");
265:
266: Node[] nodes = oList.getContentsAsArray();
267: NodeInfo[] ndArray2 = new NodeInfo[nodes.length];
268: for (int i = 0; i < nodes.length; i++){
269: System.out.println(i);
270: ndArray[i] = (NodeInfo)nodes[i];
271: }
272: String[] names= oList.getListNames();
273: for(int i=0;i<names.length;i++)
274: System.out.println("Names are :"+names[i]);
275: System.out.println("Got it.");
276:
277:
278: }
279: catch (Exception ex){
280: ex.printStackTrace();
281: }
282:
283: }
284: */
285: }
|