Skip to content

Commit 288e5d8

Browse files
committed
Made ConfigNode more flexible
1 parent 6767ee0 commit 288e5d8

File tree

5 files changed

+162
-104
lines changed

5 files changed

+162
-104
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2014 Daniel Bechler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.danielbechler.diff.inclusion;
18+
19+
import de.danielbechler.diff.path.NodePath;
20+
import de.danielbechler.diff.selector.ElementSelector;
21+
22+
import static de.danielbechler.diff.inclusion.Inclusion.EXCLUDED;
23+
import static de.danielbechler.diff.inclusion.Inclusion.INCLUDED;
24+
25+
/**
26+
* Created by Daniel Bechler.
27+
*/
28+
class InclusionNode extends ValueNode<Inclusion>
29+
{
30+
InclusionNode()
31+
{
32+
}
33+
34+
private InclusionNode(final ElementSelector elementSelector, final InclusionNode parent)
35+
{
36+
super(elementSelector, parent);
37+
}
38+
39+
public boolean isIncluded()
40+
{
41+
if (value != EXCLUDED)
42+
{
43+
final ValueNode<Inclusion> parentWithInclusion = getClosestParentWithValue();
44+
if (parentWithInclusion != null)
45+
{
46+
return parentWithInclusion.getValue() != EXCLUDED;
47+
}
48+
if (value == INCLUDED)
49+
{
50+
return true;
51+
}
52+
if (containsValue(INCLUDED))
53+
{
54+
return true;
55+
}
56+
}
57+
return false;
58+
}
59+
60+
public boolean isExcluded()
61+
{
62+
if (value == EXCLUDED)
63+
{
64+
return true;
65+
}
66+
if (parent != null && ((InclusionNode) parent).isExcluded())
67+
{
68+
return true;
69+
}
70+
return false;
71+
}
72+
73+
@Override
74+
public InclusionNode getParent()
75+
{
76+
return (InclusionNode) super.getParent();
77+
}
78+
79+
@Override
80+
public InclusionNode getNodeForPath(final NodePath nodePath)
81+
{
82+
return (InclusionNode) super.getNodeForPath(nodePath);
83+
}
84+
85+
@Override
86+
public InclusionNode getChild(final ElementSelector childSelector)
87+
{
88+
return (InclusionNode) super.getChild(childSelector);
89+
}
90+
91+
@Override
92+
protected InclusionNode newNode(final ElementSelector childSelector)
93+
{
94+
return new InclusionNode(childSelector, this);
95+
}
96+
97+
@Override
98+
public InclusionNode getClosestParentWithValue()
99+
{
100+
return (InclusionNode) super.getClosestParentWithValue();
101+
}
102+
}

src/main/java/de/danielbechler/diff/inclusion/InclusionService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class InclusionService<T> implements InclusionConfiguration<T>, IsIgnored
3939
{
4040
private final CategoryResolver categoryResolver;
4141
private final T rootConfiguration;
42-
private final ConfigNode nodeInclusions = new ConfigNode();
42+
private final InclusionNode nodeInclusions = new InclusionNode();
4343
private final Map<Class<?>, Inclusion> typeInclusions = new HashMap<Class<?>, Inclusion>();
4444
private final Map<String, Inclusion> categoryInclusions = new HashMap<String, Inclusion>();
4545
private final Map<String, Inclusion> propertyNameInclusions = new HashMap<String, Inclusion>();
@@ -118,7 +118,7 @@ else if (isExcludedByPropertyName(node))
118118

119119
private boolean hasInclusions(final Inclusion inclusion)
120120
{
121-
if (nodeInclusions.containsInclusion(inclusion))
121+
if (nodeInclusions.containsValue(inclusion))
122122
{
123123
return true;
124124
}
@@ -139,7 +139,7 @@ private boolean hasInclusions(final Inclusion inclusion)
139139

140140
private boolean isIncludedByPath(final DiffNode node)
141141
{
142-
return nodeInclusions.getNodeForPath(node.getPath()).isIncluded();
142+
return ((InclusionNode) nodeInclusions.getNodeForPath(node.getPath())).isIncluded();
143143
}
144144

145145
private boolean isIncludedByCategory(final DiffNode node)
@@ -189,8 +189,8 @@ else if (isIncludedByParentPropertyName(node))
189189

190190
private boolean isExcludedByPath(final DiffNode node)
191191
{
192-
final ConfigNode configNode = nodeInclusions.getNodeForPath(node.getPath());
193-
if (configNode.isExcluded() && !configNode.containsInclusion(INCLUDED))
192+
final InclusionNode valueNode = (InclusionNode) nodeInclusions.getNodeForPath(node.getPath());
193+
if (valueNode.isExcluded() && !valueNode.containsValue(INCLUDED))
194194
{
195195
return true;
196196
}
@@ -292,7 +292,7 @@ public ToExcludeAndReturn<T> type(final Class<?> type)
292292

293293
public ToExcludeAndReturn<T> node(final NodePath nodePath)
294294
{
295-
nodeInclusions.getNodeForPath(nodePath).setInclusion(EXCLUDED);
295+
nodeInclusions.getNodeForPath(nodePath).setValue(EXCLUDED);
296296
return this;
297297
}
298298

@@ -329,7 +329,7 @@ public ToIncludeAndReturn<T> type(final Class<?> type)
329329

330330
public ToIncludeAndReturn<T> node(final NodePath nodePath)
331331
{
332-
nodeInclusions.getNodeForPath(nodePath).setInclusion(INCLUDED);
332+
nodeInclusions.getNodeForPath(nodePath).setValue(INCLUDED);
333333
return this;
334334
}
335335

src/main/java/de/danielbechler/diff/inclusion/ConfigNode.java renamed to src/main/java/de/danielbechler/diff/inclusion/ValueNode.java

Lines changed: 37 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,38 @@
2424
import java.util.List;
2525
import 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
}

src/main/java/de/danielbechler/diff/path/NodePathValueHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/**
3131
* @author Daniel Bechler
32-
* @see de.danielbechler.diff.inclusion.ConfigNode
32+
* @see de.danielbechler.diff.inclusion.ValueNode
3333
* @deprecated The ConfigNode provides a much more powerful way to store values for NodePaths.
3434
*/
3535
@Deprecated

0 commit comments

Comments
 (0)