|
| 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><equalsFilter-literal></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 | +} |
0 commit comments