2424import java .util .List ;
2525import java .util .Map ;
2626
27- import static de .danielbechler .diff .inclusion .Inclusion .EXCLUDED ;
28- import static de .danielbechler .diff .inclusion .Inclusion .INCLUDED ;
29-
3027/**
3128 * Created by Daniel Bechler.
3229 */
33- public class ConfigNode
30+ public class ValueNode < V >
3431{
35- private final Map <ElementSelector , ConfigNode > children = new HashMap <ElementSelector , ConfigNode >();
36- private final ElementSelector elementSelector ;
37- private final ConfigNode parent ;
38- private Inclusion inclusion ;
32+ protected final Map <ElementSelector , ValueNode < V >> children = new HashMap <ElementSelector , ValueNode < V > >();
33+ protected final ValueNode < V > parent ;
34+ protected final ElementSelector elementSelector ;
35+ protected V value ;
3936
40- public ConfigNode ()
37+ public ValueNode ()
4138 {
4239 this (RootElementSelector .getInstance (), null );
4340 }
4441
45- private ConfigNode (final ElementSelector elementSelector , final ConfigNode parent )
42+ protected ValueNode (final ElementSelector elementSelector , final ValueNode < V > parent )
4643 {
4744 this .elementSelector = elementSelector ;
4845 this .parent = parent ;
4946 }
5047
51- public Inclusion getInclusion ()
52- {
53- return inclusion ;
54- }
55-
56- public void setInclusion (final Inclusion inclusion )
57- {
58- this .inclusion = inclusion ;
59- }
60-
6148 public ElementSelector getElementSelector ()
6249 {
6350 return elementSelector ;
6451 }
6552
66- public ConfigNode getParent ()
53+ public ValueNode < V > getParent ()
6754 {
6855 return parent ;
6956 }
7057
71- public ConfigNode getNodeForPath (final NodePath nodePath )
58+ public ValueNode < V > getNodeForPath (final NodePath nodePath )
7259 {
7360 if (parent == null )
7461 {
@@ -88,7 +75,7 @@ public ConfigNode getNodeForPath(final NodePath nodePath)
8875 }
8976 }
9077
91- public ConfigNode getChild (final ElementSelector childSelector )
78+ public ValueNode < V > getChild (final ElementSelector childSelector )
9279 {
9380 if (childSelector == RootElementSelector .getInstance ())
9481 {
@@ -100,13 +87,18 @@ public ConfigNode getChild(final ElementSelector childSelector)
10087 }
10188 else
10289 {
103- final ConfigNode childNode = new ConfigNode (childSelector , this );
90+ final ValueNode < V > childNode = newNode (childSelector );
10491 children .put (childSelector , childNode );
10592 return childNode ;
10693 }
10794 }
10895
109- private ConfigNode getChild (final List <ElementSelector > childSelectors )
96+ protected ValueNode <V > newNode (final ElementSelector childSelector )
97+ {
98+ return new ValueNode <V >(childSelector , this );
99+ }
100+
101+ private ValueNode <V > getChild (final List <ElementSelector > childSelectors )
110102 {
111103 assert !childSelectors .isEmpty ();
112104 if (childSelectors .contains (RootElementSelector .getInstance ()))
@@ -117,7 +109,7 @@ else if (childSelectors.size() == 1)
117109 {
118110 return getChild (childSelectors .get (0 ));
119111 }
120- final ConfigNode child = getChild (childSelectors .get (0 ));
112+ final ValueNode < V > child = getChild (childSelectors .get (0 ));
121113 return child .getChild (childSelectors .subList (1 , childSelectors .size ()));
122114 }
123115
@@ -126,89 +118,53 @@ public boolean hasChild(final ElementSelector childSelector)
126118 return children .get (childSelector ) != null ;
127119 }
128120
129- public boolean isIncluded ()
130- {
131- if (inclusion != EXCLUDED )
132- {
133- final ConfigNode parentWithInclusion = getClosestParentWithInclusion ();
134- if (parentWithInclusion != null )
135- {
136- return parentWithInclusion .getInclusion () != EXCLUDED ;
137- }
138- if (inclusion == INCLUDED )
139- {
140- return true ;
141- }
142- if (hasIncludedChildren ())
143- {
144- return true ;
145- }
146- }
147- return false ;
148- }
149-
150- public boolean isExcluded ()
151- {
152- if (inclusion == EXCLUDED )
153- {
154- return true ;
155- }
156- if (parent != null && parent .isExcluded ())
157- {
158- return true ;
159- }
160- return false ;
161- }
162-
163- public ConfigNode getClosestParentWithInclusion ()
121+ public ValueNode <V > getClosestParentWithValue ()
164122 {
165123 if (parent != null )
166124 {
167- if (parent .hasInclusion ())
125+ if (parent .hasValue ())
168126 {
169127 return parent ;
170128 }
171129 else
172130 {
173- return parent .getClosestParentWithInclusion ();
131+ return parent .getClosestParentWithValue ();
174132 }
175133 }
176134 return null ;
177135 }
178136
179- private boolean hasIncludedChildren ()
137+ public boolean hasValue ()
180138 {
181- for (final ConfigNode child : children .values ())
182- {
183- if (child .isIncluded ())
184- {
185- return true ;
186- }
187- }
188- return false ;
189- }
190-
191- public boolean hasInclusion ()
192- {
193- return inclusion != null ;
139+ return value != null ;
194140 }
195141
196- public boolean containsInclusion (final Inclusion inclusion )
142+ public boolean containsValue (final V value )
197143 {
198- if (this .inclusion == inclusion )
144+ if (this .value == value )
199145 {
200146 return true ;
201147 }
202148 else
203149 {
204- for (final ConfigNode child : children .values ())
150+ for (final ValueNode < V > child : children .values ())
205151 {
206- if (child .containsInclusion ( inclusion ))
152+ if (child .containsValue ( value ))
207153 {
208154 return true ;
209155 }
210156 }
211157 }
212158 return false ;
213159 }
160+
161+ public V getValue ()
162+ {
163+ return value ;
164+ }
165+
166+ public void setValue (final V value )
167+ {
168+ this .value = value ;
169+ }
214170}
0 commit comments