8
8
9
9
import java .util .Arrays ;
10
10
import java .util .Collections ;
11
- import java .util .Iterator ;
12
11
import java .util .List ;
13
12
import java .util .Map ;
14
13
import java .util .Set ;
@@ -35,25 +34,25 @@ public class GuavaFunctionalExamplesTest {
35
34
36
35
@ Test
37
36
public final void whenFilteringNumbersAccordingToACondition_thenCorrectResults () {
38
- final List <Integer > randomNumbers = Lists .newArrayList (1 , 2 , 3 , 6 , 8 , 10 , 34 , 57 , 89 );
37
+ final List <Integer > numbers = Lists .newArrayList (1 , 2 , 3 , 6 , 8 , 10 , 34 , 57 , 89 );
39
38
final Predicate <Integer > acceptEvenNumber = new Predicate <Integer >() {
40
39
@ Override
41
40
public final boolean apply (final Integer number ) {
42
41
return (number % 2 ) == 0 ;
43
42
}
44
43
};
45
- final List <Integer > evenNumbers = Lists .newArrayList (Collections2 .filter (randomNumbers , acceptEvenNumber ));
44
+ final List <Integer > evenNumbers = Lists .newArrayList (Collections2 .filter (numbers , acceptEvenNumber ));
46
45
47
46
final Integer found = Collections .binarySearch (evenNumbers , 57 );
48
47
assertThat (found , lessThan (0 ));
49
48
}
50
49
51
50
@ Test
52
51
public final void givenCollectionContainsNulls_whenNullsAreFilteredOut_thenResultingCollectionsHasNoNulls () {
53
- final List <String > collectionOfStringsWithNulls = Lists .newArrayList ("a" , "bc" , null , "def" , null , null , "ghij " );
54
- final Iterable <String > collectionWithoutNulls = Iterables .filter (collectionOfStringsWithNulls , Predicates .notNull ());
52
+ final List <String > withNulls = Lists .newArrayList ("a" , "bc" , null , "def" );
53
+ final Iterable <String > withoutNuls = Iterables .filter (withNulls , Predicates .notNull ());
55
54
56
- assertTrue (Iterables .all (collectionWithoutNulls , Predicates .notNull ()));
55
+ assertTrue (Iterables .all (withoutNuls , Predicates .notNull ()));
57
56
}
58
57
59
58
// predicates - checking
@@ -86,18 +85,6 @@ public final boolean apply(final Integer number) {
86
85
assertTrue (Iterables .all (evenNumbers , Predicates .not (acceptOddNumber )));
87
86
}
88
87
89
- // try - 1
90
-
91
- @ Test (expected = UnsupportedOperationException .class )
92
- public final void givenUnmodifiableViewOverIterable_whenTryingToRemove_thenNotAllowed () {
93
- final List <Integer > numbers = Lists .newArrayList (1 , 2 , 3 );
94
- final Iterable <Integer > unmodifiableIterable = Iterables .unmodifiableIterable (numbers );
95
- final Iterator <Integer > iterator = unmodifiableIterable .iterator ();
96
- if (iterator .hasNext ()) {
97
- iterator .remove ();
98
- }
99
- }
100
-
101
88
// other predicates
102
89
103
90
@ Test
@@ -118,10 +105,10 @@ public final void whenApplyingSimpleFunctionToInputs_thenCorrectlyTransformed()
118
105
public final void whenUsingAnIntermediaryFunctionToOrder_thenCorerctlyOrderedInAlphabeticalOrder () {
119
106
final List <Integer > numbersToSort = Arrays .asList (2 , 1 , 11 , 100 , 8 , 14 );
120
107
final Ordering <Object > ordering = Ordering .natural ().onResultOf (Functions .toStringFunction ());
121
- final List <Integer > alphabeticalOrderingOfNumbers = ordering .sortedCopy (numbersToSort );
108
+ final List <Integer > inAlphabeticalOrder = ordering .sortedCopy (numbersToSort );
122
109
123
- final List <Integer > expectedAlphabeticalOrderingOfNumbers = Lists .newArrayList (1 , 100 , 11 , 14 , 2 , 8 );
124
- assertThat (expectedAlphabeticalOrderingOfNumbers , equalTo (alphabeticalOrderingOfNumbers ));
110
+ final List <Integer > correctAlphabeticalOrder = Lists .newArrayList (1 , 100 , 11 , 14 , 2 , 8 );
111
+ assertThat (correctAlphabeticalOrder , equalTo (inAlphabeticalOrder ));
125
112
}
126
113
127
114
@ Test
@@ -179,4 +166,21 @@ public final Integer apply(final Integer input) {
179
166
assertThat (numberToPowerOfTwoImuttable .get (2 ), equalTo (4 ));
180
167
}
181
168
169
+ // Predicate => Function
170
+
171
+ @ Test
172
+ public final void whenConvertingPredicateToFunction_thenCorrect () {
173
+ final List <Integer > numbers = Lists .newArrayList (1 , 2 , 3 , 6 );
174
+ final Predicate <Integer > acceptEvenNumber = new Predicate <Integer >() {
175
+ @ Override
176
+ public final boolean apply (final Integer number ) {
177
+ return (number % 2 ) == 0 ;
178
+ }
179
+ };
180
+ final Function <Integer , Boolean > isEventNumberFunction = Functions .forPredicate (acceptEvenNumber );
181
+ final List <Boolean > areNumbersEven = Lists .transform (numbers , isEventNumberFunction );
182
+
183
+ assertThat (areNumbersEven , contains (false , true , false , true ));
184
+ }
185
+
182
186
}
0 commit comments