Question: I wanna find help with my filter codes. This filter class is to : Here is a motivating query. Let's suppose we have a list
I wanna find help with my filter codes. This filter class is to :Here is a motivating query. Let's suppose we have a list of numbers [10,50,1,400]. This list will be our input data. The query is "return the elements greater than 40". The output for this example would be the elements 50, 400. Here are codes I need to make adjustment: package iterators; import java.util.Iterator; import java.util.NoSuchElementException; // Iterator that uses a Predicate to filter out elements from the input // UNCOMMENT THE implements Iterator before starting! public class Filter implements Iterator { private final Predicate p; private final Iterator input; private T result; public Filter(Predicate p, Iterator input) { this.p = p; this.input = input; } public boolean hasNext() { return input.hasNext(); } public T next() { if (hasNext()) { if (p.check(input.next())) { T result = input.next(); } } else { throw new NoSuchElementException(){ } return result; } } Here are the fllterfunction:
package iterators; import java.util.List; // Interface for classes to define a function from InT to Listpublic interface FlatApplyFunction { public List apply(InT x); }
Here are the test code:Filter test and the end is Predicate's interface(You may just have a look at the end and then run the test):
package iterators; import java.util.Arrays; import java.util.List; import org.junit.Test; import static org.junit.Assert.*; public class FilterTest { public FilterTest() { } @Test public void emptyTest() { Integer[] inputValues = {}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new IsNotZero(), input.iterator()); assertEquals(false, op.hasNext()); assertEquals(false, op.hasNext()); } @Test public void oneRemovedTest() { Integer[] inputValues = {0}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new IsNotZero(), input.iterator()); assertEquals(false, op.hasNext()); assertEquals(false, op.hasNext()); } @Test public void oneKeptTest() { Integer[] inputValues = {200}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new IsNotZero(), input.iterator()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals(200, (int)op.next()); assertEquals(false, op.hasNext()); assertEquals(false, op.hasNext()); } @Test public void moreTest() { Integer[] inputValues = {0,50,1,0,400,0,0}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new IsNotZero(), input.iterator()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals(50, (int)op.next()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals(1, (int)op.next()); assertEquals(400, (int)op.next()); assertEquals(false, op.hasNext()); } @Test public void emptyTest2() { String[] inputValues = {}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new OneCharacter(), input.iterator()); assertEquals(false, op.hasNext()); assertEquals(false, op.hasNext()); } @Test public void oneRemovedTest2() { String[] inputValues = {"AB"}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new OneCharacter(), input.iterator()); assertEquals(false, op.hasNext()); assertEquals(false, op.hasNext()); } @Test public void oneKeptTest2() { String[] inputValues = {"A"}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new OneCharacter(), input.iterator()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals("A", op.next()); assertEquals(false, op.hasNext()); assertEquals(false, op.hasNext()); } @Test public void moreTest2() { String[] inputValues = {"A","AB","ABC","C","D","E","CDE","DE"}; List input = Arrays.asList(inputValues); Filter op = new Filter<>(new OneCharacter(), input.iterator()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals("A", op.next()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals(true, op.hasNext()); assertEquals("C", op.next()); assertEquals(true, op.hasNext()); assertEquals("D", op.next()); assertEquals(true, op.hasNext()); assertEquals("E", op.next()); assertEquals(false, op.hasNext()); } public static class IsNotZero implements Predicate { /* Return true if the input isn't 0 */ @Override public boolean check(Integer data) { return data!=0; } } public static class OneCharacter implements Predicate { /* Return true if the input is 1 character */ @Override public boolean check(String data) { return data.length() == 1; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
