Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding EqualsValue filter #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/core/cascading/operation/filter/EqualsValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cascading.operation.filter;

import cascading.flow.FlowProcess;
import cascading.operation.BaseOperation;
import cascading.operation.Filter;
import cascading.operation.FilterCall;
import cascading.tuple.TupleEntry;

/**
* Class EqualsValue verifies that the first n values in the argument values {@link cascading.tuple.Tuple} equals the n
* values specified by the constructor. If a non-equal value is encountered, or if the tuple does not have enough
* values, the current Tuple will be filtered out.
*/
public class EqualsValue extends BaseOperation<Void> implements Filter<Void> {

private static final long serialVersionUID = 1L;

private Object[] values;

public EqualsValue(Object value, Object... values) {
super(1);
this.values = new Object[values.length + 1];
this.values[0] = value;
System.arraycopy(values, 0, this.values, 1, values.length);
}

@Override
@SuppressWarnings("rawtypes")
public boolean isRemove(FlowProcess flowProcess, FilterCall<Void> filterCall) {
TupleEntry arguments = filterCall.getArguments();
if (arguments.size() < values.length) {
return true;
}
for (int i = 0; i < values.length; i++) {
Object arg = arguments.getObject(i);
if (values[i] == null || arg == null) {
if (values[i] != null || arg != null) {
return true;
}
}
if (!values[i].equals(arg)) {
return true;
}
}
return false;
}
}
38 changes: 38 additions & 0 deletions src/test/cascading/operation/filter/EqualsValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cascading.operation.filter;

import cascading.tuple.Tuple;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static cascading.CascadingTestCase.invokeFilter;

public class EqualsValueTest {
@Test
public void testEqualsValue() {
EqualsValue e1 = new EqualsValue("aaa");
Tuple[] args1 = new Tuple[] {
new Tuple("aaa"),
new Tuple("bbb"),
new Tuple(1),
new Tuple(null)
};
boolean[] actual1 = invokeFilter(e1, args1);
assertEquals(false, actual1[0]);
assertEquals(true, actual1[1]);
assertEquals(true, actual1[2]);
assertEquals(true, actual1[3]);

EqualsValue e2 = new EqualsValue(1, "x");
Tuple[] args2 = new Tuple[] {
new Tuple(1, "x"),
new Tuple(2, "x"),
new Tuple(1, "y"),
new Tuple(1)
};
boolean[] actual2 = invokeFilter(e2, args2);
assertEquals(false, actual2[0]);
assertEquals(true, actual2[1]);
assertEquals(true, actual2[2]);
assertEquals(true, actual2[3]);
}
}