KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > type > Node


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of JEGG nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.type;
30
31 import java.util.Collection JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  *
40  */

41 public class Node
42 {
43     private static final Log LOG = LogFactory.getLog(Node.class);
44     
45     private Class JavaDoc type;
46     private Object JavaDoc cookie;
47     private Collection JavaDoc branches = new HashSet JavaDoc();
48     
49     public Node(Class JavaDoc t)
50     {
51         super();
52         type = t;
53     }
54     public Node(Class JavaDoc t, Object JavaDoc c)
55     {
56         this(t);
57         cookie = c;
58     }
59     public Class JavaDoc getType()
60     {
61         return type;
62     }
63     public Object JavaDoc getCookie()
64     {
65         return cookie;
66     }
67     
68     public void setCookie(Object JavaDoc o)
69     {
70         cookie = o;
71     }
72     
73     public boolean insert(Node nd)
74     {
75         Class JavaDoc nd_type = nd.getType();
76         Class JavaDoc this_type = getType();
77         
78         if (this_type.isAssignableFrom(nd_type))
79         {
80             if (!this_type.equals(nd_type))
81             {
82                 for (Iterator JavaDoc it = branches.iterator(); it.hasNext(); )
83                 {
84                     Node cn = (Node) it.next();
85                     Class JavaDoc cn_type = cn.getType();
86                     if (cn_type.isAssignableFrom(nd_type))
87                     {
88                         if (LOG.isDebugEnabled())
89                             LOG.debug("Inserting "+nd_type.getName()+" under node "+cn_type.getName());
90                         if (cn.insert(nd))
91                         {
92                             return true;
93                         }
94                     }
95                     else
96                     if (nd_type.isAssignableFrom(cn_type))
97                     {
98                         if (LOG.isDebugEnabled())
99                             LOG.debug("Moving node "+cn_type.getName()+" from node "+this_type.getName()+" to node "+nd_type.getName());
100                         if (nd.insert(cn))
101                         {
102                             it.remove();
103                             //removeChild(cn);
104
}
105                     }
106                 }
107                 addChild(nd);
108             }
109             else
110             {
111                 setCookie(nd.getCookie());
112             }
113             return true;
114         }
115         return false;
116     }
117     
118     protected Collection JavaDoc getBranches()
119     {
120         return branches;
121     }
122     
123     protected void addChild(Node nd)
124     {
125         //System.out.println("Adding "+nd.getType().getName()+" to "+getType().getName());
126
branches.add(nd);
127     }
128     
129     protected void removeChild(Node nd)
130     {
131         branches.remove(nd);
132     }
133     
134     public Node findNearest(Class JavaDoc c)
135     {
136         Class JavaDoc this_type = getType();
137         if (this_type.equals(c))
138         {
139             return this;
140         }
141         if (getType().isAssignableFrom(c))
142         {
143             for (Iterator JavaDoc it = branches.iterator(); it.hasNext(); )
144             {
145                 Node cn = (Node) it.next();
146                 Node fn = cn.findNearest(c);
147                 if (null != fn)
148                 {
149                     return fn;
150                 }
151             }
152             return this;
153         }
154         return null;
155     }
156 }
157
Popular Tags