5
5
6
6
import java .util .Collection ;
7
7
import java .util .List ;
8
+ import java .util .Map ;
8
9
import java .util .NoSuchElementException ;
9
10
import java .util .Set ;
10
11
11
12
import org .junit .Test ;
12
13
13
14
import com .google .common .base .Function ;
14
15
import com .google .common .base .Predicate ;
16
+ import com .google .common .base .Predicates ;
17
+ import com .google .common .collect .ImmutableList ;
18
+ import com .google .common .collect .ImmutableMap ;
19
+ import com .google .common .collect .ImmutableSet ;
15
20
import com .google .common .collect .Iterables ;
16
21
import com .google .common .collect .Lists ;
22
+ import com .google .common .collect .Maps ;
17
23
import com .google .common .collect .Sets ;
18
24
25
+ @ SuppressWarnings ("unused" )
19
26
public class GuavaCollectionsExamplesTest {
20
27
21
28
// tests
@@ -96,7 +103,7 @@ public final boolean apply(final String input) {
96
103
//
97
104
98
105
@ Test (expected = NoSuchElementException .class )
99
- public final void givenNoSearchResult_whenFindingElementInIterable_thenNoException () {
106
+ public final void givenNoSearchResult_whenFindingElementInIterable_thenException () {
100
107
final Iterable <String > theCollection = Sets .newHashSet ("abcd" , "efgh" , "ijkl" );
101
108
102
109
final String found = Iterables .find (theCollection , new Predicate <String >() {
@@ -109,4 +116,61 @@ public final boolean apply(final String input) {
109
116
assertNull (found );
110
117
}
111
118
119
+ @ Test
120
+ public final void givenNoSearchResult_whenFindingElementInIterableWithSpecifiedReturn_thenNoException () {
121
+ final Iterable <String > theCollection = Sets .newHashSet ("abcd" , "efgh" , "ijkl" );
122
+
123
+ final Predicate <String > inputOfLengthOne = new Predicate <String >() {
124
+ @ Override
125
+ public final boolean apply (final String input ) {
126
+ return input .length () == 1 ;
127
+ }
128
+ };
129
+ final String found = Iterables .find (theCollection , inputOfLengthOne , null );
130
+
131
+ assertNull (found );
132
+ }
133
+
134
+ // purge of nulls
135
+
136
+ @ Test
137
+ public final void givenListContainsNulls_whenPurgedOfNulls_thenNoLongerContainsNulls () {
138
+ final List <String > values = Lists .newArrayList ("a" , null , "b" , "c" );
139
+ final Iterable <String > withoutNulls = Iterables .filter (values , Predicates .notNull ());
140
+ System .out .println (withoutNulls );
141
+ }
142
+
143
+ // immutable collections
144
+
145
+ @ Test
146
+ public final void whenCreatingImuutableCollections_thenNoExceptions () {
147
+ final ImmutableList <String > immutableList = ImmutableList .of ("a" , "b" , "c" );
148
+ final ImmutableSet <String > immutableSet = ImmutableSet .of ("a" , "b" , "c" );
149
+ final ImmutableMap <String , String > imuttableMap = ImmutableMap .of ("k1" , "v1" , "k2" , "v2" , "k3" , "v3" );
150
+ }
151
+
152
+ @ Test
153
+ public final void whenTransformingCollectionsToImmutable_thenNoExceptions () {
154
+ final List <String > muttableList = Lists .newArrayList ();
155
+ final ImmutableList <String > immutableList = ImmutableList .copyOf (muttableList );
156
+
157
+ final Set <String > muttableSet = Sets .newHashSet ();
158
+ final ImmutableSet <String > immutableSet = ImmutableSet .copyOf (muttableSet );
159
+
160
+ final Map <String , String > muttableMap = Maps .newHashMap ();
161
+ final ImmutableMap <String , String > imuttableMap = ImmutableMap .copyOf (muttableMap );
162
+ }
163
+
164
+ @ Test
165
+ public final void whenTransformingCollectionsToImmutableViaBuilders_thenNoExceptions () {
166
+ final List <String > muttableList = Lists .newArrayList ();
167
+ final ImmutableList <String > immutableList = ImmutableList .<String > builder ().addAll (muttableList ).build ();
168
+
169
+ final Set <String > muttableSet = Sets .newHashSet ();
170
+ final ImmutableSet <String > immutableSet = ImmutableSet .<String > builder ().addAll (muttableSet ).build ();
171
+
172
+ final Map <String , String > muttableMap = Maps .newHashMap ();
173
+ final ImmutableMap <String , String > imuttableMap = ImmutableMap .<String , String > builder ().putAll (muttableMap ).build ();
174
+ }
175
+
112
176
}
0 commit comments