Skip to content

Commit c32b083

Browse files
committed
Merge pull request #149 from thaidlas/equals-filter
Added Collector EqualsFilter
2 parents ac92bd3 + 62a56e0 commit c32b083

File tree

5 files changed

+532
-2
lines changed

5 files changed

+532
-2
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2013 Deutsche Nationalbibliothek
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+
package org.culturegraph.mf.morph.collectors;
17+
18+
import java.util.HashMap;
19+
import java.util.HashSet;
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
import org.culturegraph.mf.morph.Metamorph;
24+
import org.culturegraph.mf.morph.NamedValueSource;
25+
import org.culturegraph.mf.util.StringUtil;
26+
27+
28+
29+
/**
30+
* Corresponds to the <code>&lt;equalsFilter-literal&gt;</code> tag. Emits data if values in variables are all equal.
31+
*
32+
* @author Thomas Haidlas
33+
*/
34+
public final class EqualsFilter extends AbstractCollect{
35+
private final Map<String, String> variables = new HashMap<String, String>();
36+
private final Set<NamedValueSource> sources = new HashSet<NamedValueSource>();
37+
private final Set<NamedValueSource> sourcesLeft = new HashSet<NamedValueSource>();
38+
private boolean isEqual = true;
39+
40+
public EqualsFilter(final Metamorph metamorph) {
41+
super(metamorph);
42+
setNamedValueReceiver(metamorph);
43+
this.isEqual = true;
44+
}
45+
46+
@Override
47+
protected void emit() {
48+
final String name = StringUtil.format(getName(), this.variables);
49+
final String value = StringUtil.format(getValue(), this.variables);
50+
if(this.isEqual){
51+
getNamedValueReceiver().receive(name, value, this, getRecordCount(), getEntityCount());
52+
}
53+
}
54+
55+
56+
@Override
57+
protected boolean isComplete() {
58+
return this.sourcesLeft.isEmpty();
59+
}
60+
61+
@Override
62+
protected void receive(final String name, final String value, final NamedValueSource source) {
63+
if(this.variables.size() > 0 && !this.variables.containsValue(value)){
64+
this.isEqual = false;
65+
}
66+
this.variables.put(name, value);
67+
this.sourcesLeft.remove(source);
68+
}
69+
70+
71+
@Override
72+
public void onNamedValueSourceAdded(final NamedValueSource namedValueSource) {
73+
this.sources.add(namedValueSource);
74+
this.sourcesLeft.add(namedValueSource);
75+
}
76+
77+
@Override
78+
protected void clear() {
79+
this.sourcesLeft.addAll(this.sources);
80+
this.variables.clear();
81+
this.isEqual = true;
82+
}
83+
84+
}

src/main/resources/morph-collectors.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ entity org.culturegraph.mf.morph.collectors.Entity
55
concat org.culturegraph.mf.morph.collectors.Concat
66
tuples org.culturegraph.mf.morph.collectors.Tuples
77
square org.culturegraph.mf.morph.collectors.Square
8-
range org.culturegraph.mf.morph.collectors.Range
8+
range org.culturegraph.mf.morph.collectors.Range
9+
equalsFilter org.culturegraph.mf.morph.collectors.EqualsFilter

src/main/resources/schemata/metamorph.xsd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@
228228
<attribute name="flushWith" type="string" use="optional" />
229229
</complexType>
230230
</element>
231+
232+
<element name="equalsFilter">
233+
<complexType>
234+
<sequence>
235+
<group ref="tns:literal-rule" minOccurs="1" maxOccurs="unbounded" />
236+
<element ref="tns:postprocess" minOccurs="0" maxOccurs="1" />
237+
</sequence>
238+
<attribute name="name" type="string" use="required" />
239+
<attribute name="value" type="string" use="required" />
240+
241+
<attribute name="reset" type="boolean" use="optional"
242+
default="false" />
243+
<attribute name="sameEntity" type="boolean" use="optional"
244+
default="false" />
245+
<attribute name="flushWith" type="string" use="optional" />
246+
</complexType>
247+
</element>
231248

232249
<element name="concat">
233250
<annotation>
@@ -544,6 +561,7 @@
544561
<element ref="tns:square" />
545562
<element ref="tns:tuples" />
546563
<element ref="tns:range" />
564+
<element ref="tns:equalsFilter" />
547565
</choice>
548566
</group>
549567

src/test/java/org/culturegraph/mf/morph/collectors/CollectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
*/
2626
@RunWith(TestSuite.class)
2727
@TestDefinitions({ "CombineTest.xml", "GroupTest.xml", "ChooseTest.xml", "EntityTest.xml", "ConcatTest.xml",
28-
"Nested.xml", "NestedEntity.xml", "TuplesTest.xml", "Misc.xml", "SquareTest.xml", "RangeTest.xml" })
28+
"Nested.xml", "NestedEntity.xml", "TuplesTest.xml", "Misc.xml", "SquareTest.xml", "RangeTest.xml", "EqualsFilterTest.xml" })
2929
public final class CollectorTest {/* bind to xml test */
3030
}

0 commit comments

Comments
 (0)