55
66import java .util .Collection ;
77import java .util .List ;
8+ import java .util .Map ;
89import java .util .NoSuchElementException ;
910import java .util .Set ;
1011
1112import org .junit .Test ;
1213
1314import com .google .common .base .Function ;
1415import 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 ;
1520import com .google .common .collect .Iterables ;
1621import com .google .common .collect .Lists ;
22+ import com .google .common .collect .Maps ;
1723import com .google .common .collect .Sets ;
1824
25+ @ SuppressWarnings ("unused" )
1926public class GuavaCollectionsExamplesTest {
2027
2128 // tests
@@ -96,7 +103,7 @@ public final boolean apply(final String input) {
96103 //
97104
98105 @ Test (expected = NoSuchElementException .class )
99- public final void givenNoSearchResult_whenFindingElementInIterable_thenNoException () {
106+ public final void givenNoSearchResult_whenFindingElementInIterable_thenException () {
100107 final Iterable <String > theCollection = Sets .newHashSet ("abcd" , "efgh" , "ijkl" );
101108
102109 final String found = Iterables .find (theCollection , new Predicate <String >() {
@@ -109,4 +116,61 @@ public final boolean apply(final String input) {
109116 assertNull (found );
110117 }
111118
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+
112176}
0 commit comments