### Question:
FirstValidRule implements Filter<T> { @Override public Set<T> apply( Set<T> input ) { Set<T> output = input; for ( Filter<T> chain : rules ) { output = chain.apply( input ); if ( !output.isEmpty() ) { break; } } return output; } FirstValidRule( List<FilterChain<T>> rules ); @Override Set<T> apply( Set<T> input ); @Override boolean equals( Object o ); @Override int hashCode(); @Override String toString();  }### Answer:
@Test public void shouldUseResultOfFirstNonEmpty() { Filter<Integer> removeValuesOfFive = data -> data.stream().filter( value -> value != 5 ).collect( Collectors.toSet() ); Filter<Integer> countMoreThanFour = data -> data.size() > 4 ? data : Collections.emptySet(); Filter<Integer> countMoreThanThree = data -> data.size() > 3 ? data : Collections.emptySet(); FilterChain<Integer> ruleA = new FilterChain<>( asList( removeValuesOfFive, countMoreThanFour ) ); FilterChain<Integer> ruleB = new FilterChain<>( asList( removeValuesOfFive, countMoreThanThree ) ); FilterChain<Integer> ruleC = new FilterChain<>( singletonList( countMoreThanFour ) ); FirstValidRule<Integer> firstValidRule = new FirstValidRule<>( asList( ruleA, ruleB, ruleC ) ); Set<Integer> data = asSet( 5, 1, 5, 2, 5, 3, 5, 4 ); data = firstValidRule.apply( data ); assertEquals( asSet( 1, 2, 3, 4 ), data ); }