1
1
package org .baeldung .guava ;
2
2
3
+ import static org .hamcrest .Matchers .contains ;
3
4
import static org .hamcrest .Matchers .equalTo ;
4
5
import static org .hamcrest .Matchers .lessThan ;
5
6
import static org .junit .Assert .assertThat ;
6
7
import static org .junit .Assert .assertTrue ;
7
8
9
+ import java .util .Arrays ;
8
10
import java .util .Collections ;
11
+ import java .util .Iterator ;
9
12
import java .util .List ;
10
13
import java .util .Map ;
11
14
import java .util .Set ;
12
15
13
16
import org .junit .Test ;
14
17
15
18
import com .google .common .base .Function ;
19
+ import com .google .common .base .Functions ;
16
20
import com .google .common .base .Predicate ;
21
+ import com .google .common .base .Predicates ;
17
22
import com .google .common .collect .Collections2 ;
18
23
import com .google .common .collect .Iterables ;
19
24
import com .google .common .collect .Lists ;
20
25
import com .google .common .collect .Maps ;
26
+ import com .google .common .collect .Ordering ;
21
27
import com .google .common .collect .Sets ;
22
28
23
29
public class GuavaFunctionalExamplesTest {
24
30
25
31
// tests
26
32
27
- // predicates
33
+ // predicates - filtering
28
34
29
35
@ Test
30
36
public final void whenFilteringNumbersAccordingToACondition_thenCorrectResults () {
@@ -41,17 +47,73 @@ public final boolean apply(final Integer number) {
41
47
assertThat (found , lessThan (0 ));
42
48
}
43
49
50
+ @ Test
51
+ public final void givenCollectionContainsNulls_whenNullsAreFilteredOut_thenResultingCollectionsHasNoNulls () {
52
+ final List <String > collectionOfStringsWithNulls = Lists .newArrayList ("a" , "bc" , null , "def" , null , null , "ghij" );
53
+ final Iterable <String > collectionWithoutNulls = Iterables .filter (collectionOfStringsWithNulls , Predicates .notNull ());
54
+
55
+ assertTrue (Iterables .all (collectionWithoutNulls , Predicates .notNull ()));
56
+ }
57
+
58
+ // predicates - checking
59
+
44
60
@ Test
45
61
public final void givenEvenNumbers_whenCheckingIfAllSatisfyTheEvenPredicate_thenYes () {
46
- final List <Integer > eventNumbers = Lists .newArrayList (2 , 6 , 8 , 10 , 34 , 90 );
62
+ final List <Integer > evenNumbers = Lists .newArrayList (2 , 6 , 8 , 10 , 34 , 90 );
47
63
final Predicate <Integer > acceptEvenNumber = new Predicate <Integer >() {
48
64
@ Override
49
65
public final boolean apply (final Integer number ) {
50
66
return (number % 2 ) == 0 ;
51
67
}
52
68
};
53
69
54
- assertTrue (Iterables .all (eventNumbers , acceptEvenNumber ));
70
+ assertTrue (Iterables .all (evenNumbers , acceptEvenNumber ));
71
+ }
72
+
73
+ // negating a predicate
74
+
75
+ @ Test
76
+ public final void givenCollectionOfEvenNumbers_whenCheckingThatCollectionContainsNoOddNumber_thenTrue () {
77
+ final List <Integer > evenNumbers = Lists .newArrayList (2 , 6 , 8 , 10 , 34 , 90 );
78
+ final Predicate <Integer > acceptEvenNumber = new Predicate <Integer >() {
79
+ @ Override
80
+ public final boolean apply (final Integer number ) {
81
+ return (number % 2 ) != 0 ;
82
+ }
83
+ };
84
+
85
+ assertTrue (Iterables .all (evenNumbers , Predicates .not (acceptEvenNumber )));
86
+ }
87
+
88
+ // try - 1
89
+
90
+ @ Test (expected = UnsupportedOperationException .class )
91
+ public final void givenUnmodifiableViewOverIterable_whenTryingToRemove_thenNotAllowed () {
92
+ final List <Integer > numbers = Lists .newArrayList (1 , 2 , 3 );
93
+ final Iterable <Integer > unmodifiableIterable = Iterables .unmodifiableIterable (numbers );
94
+ final Iterator <Integer > iterator = unmodifiableIterable .iterator ();
95
+ if (iterator .hasNext ()) {
96
+ iterator .remove ();
97
+ }
98
+ }
99
+
100
+ // functions
101
+
102
+ @ Test
103
+ public final void whenApplyingSimpleFunctionToInputs_thenCorrectlyTransformed () {
104
+ final List <Integer > numbers = Lists .newArrayList (1 , 2 , 3 );
105
+ final List <String > numbersAsStrings = Lists .transform (numbers , Functions .toStringFunction ());
106
+ assertThat (numbersAsStrings , contains ("1" , "2" , "3" ));
107
+ }
108
+
109
+ @ Test
110
+ public final void whenUsingAnIntermediaryFunctionToOrder_thenCorerctlyOrderedInAlphabeticalOrder () {
111
+ final List <Integer > numbersToSort = Arrays .asList (2 , 1 , 11 , 100 , 8 , 14 );
112
+ final Ordering <Object > ordering = Ordering .natural ().onResultOf (Functions .toStringFunction ());
113
+ final List <Integer > alphabeticalOrderingOfNumbers = ordering .sortedCopy (numbersToSort );
114
+
115
+ final List <Integer > expectedAlphabeticalOrderingOfNumbers = Lists .newArrayList (1 , 100 , 11 , 14 , 2 , 8 );
116
+ assertThat (expectedAlphabeticalOrderingOfNumbers , equalTo (alphabeticalOrderingOfNumbers ));
55
117
}
56
118
57
119
// Set+Function => Map
0 commit comments