From cf73a09b62b2a06b3654776aa514183fe63e758e Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Wed, 30 Oct 2013 17:42:47 +0200 Subject: [PATCH] guava functional work --- .../guava/GuavaCollectionsExamplesTest.java | 13 ++++++ .../guava/GuavaFunctionalExamplesTest.java | 46 ++++++++++--------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java index dc6bfe4c0b..2c137e6c80 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaCollectionsExamplesTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -173,4 +174,16 @@ public class GuavaCollectionsExamplesTest { final ImmutableMap imuttableMap = ImmutableMap. builder().putAll(muttableMap).build(); } + // unmodifiable + + @Test(expected = UnsupportedOperationException.class) + public final void givenUnmodifiableViewOverIterable_whenTryingToRemove_thenNotAllowed() { + final List numbers = Lists.newArrayList(1, 2, 3); + final Iterable unmodifiableIterable = Iterables.unmodifiableIterable(numbers); + final Iterator iterator = unmodifiableIterable.iterator(); + if (iterator.hasNext()) { + iterator.remove(); + } + } + } diff --git a/guava/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java b/guava/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java index f58139a974..08091639f1 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaFunctionalExamplesTest.java @@ -8,7 +8,6 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -35,14 +34,14 @@ public class GuavaFunctionalExamplesTest { @Test public final void whenFilteringNumbersAccordingToACondition_thenCorrectResults() { - final List randomNumbers = Lists.newArrayList(1, 2, 3, 6, 8, 10, 34, 57, 89); + final List numbers = Lists.newArrayList(1, 2, 3, 6, 8, 10, 34, 57, 89); final Predicate acceptEvenNumber = new Predicate() { @Override public final boolean apply(final Integer number) { return (number % 2) == 0; } }; - final List evenNumbers = Lists.newArrayList(Collections2.filter(randomNumbers, acceptEvenNumber)); + final List evenNumbers = Lists.newArrayList(Collections2.filter(numbers, acceptEvenNumber)); final Integer found = Collections.binarySearch(evenNumbers, 57); assertThat(found, lessThan(0)); @@ -50,10 +49,10 @@ public class GuavaFunctionalExamplesTest { @Test public final void givenCollectionContainsNulls_whenNullsAreFilteredOut_thenResultingCollectionsHasNoNulls() { - final List collectionOfStringsWithNulls = Lists.newArrayList("a", "bc", null, "def", null, null, "ghij"); - final Iterable collectionWithoutNulls = Iterables.filter(collectionOfStringsWithNulls, Predicates.notNull()); + final List withNulls = Lists.newArrayList("a", "bc", null, "def"); + final Iterable withoutNuls = Iterables.filter(withNulls, Predicates.notNull()); - assertTrue(Iterables.all(collectionWithoutNulls, Predicates.notNull())); + assertTrue(Iterables.all(withoutNuls, Predicates.notNull())); } // predicates - checking @@ -86,18 +85,6 @@ public class GuavaFunctionalExamplesTest { assertTrue(Iterables.all(evenNumbers, Predicates.not(acceptOddNumber))); } - // try - 1 - - @Test(expected = UnsupportedOperationException.class) - public final void givenUnmodifiableViewOverIterable_whenTryingToRemove_thenNotAllowed() { - final List numbers = Lists.newArrayList(1, 2, 3); - final Iterable unmodifiableIterable = Iterables.unmodifiableIterable(numbers); - final Iterator iterator = unmodifiableIterable.iterator(); - if (iterator.hasNext()) { - iterator.remove(); - } - } - // other predicates @Test @@ -118,10 +105,10 @@ public class GuavaFunctionalExamplesTest { public final void whenUsingAnIntermediaryFunctionToOrder_thenCorerctlyOrderedInAlphabeticalOrder() { final List numbersToSort = Arrays.asList(2, 1, 11, 100, 8, 14); final Ordering ordering = Ordering.natural().onResultOf(Functions.toStringFunction()); - final List alphabeticalOrderingOfNumbers = ordering.sortedCopy(numbersToSort); + final List inAlphabeticalOrder = ordering.sortedCopy(numbersToSort); - final List expectedAlphabeticalOrderingOfNumbers = Lists.newArrayList(1, 100, 11, 14, 2, 8); - assertThat(expectedAlphabeticalOrderingOfNumbers, equalTo(alphabeticalOrderingOfNumbers)); + final List correctAlphabeticalOrder = Lists.newArrayList(1, 100, 11, 14, 2, 8); + assertThat(correctAlphabeticalOrder, equalTo(inAlphabeticalOrder)); } @Test @@ -179,4 +166,21 @@ public class GuavaFunctionalExamplesTest { assertThat(numberToPowerOfTwoImuttable.get(2), equalTo(4)); } + // Predicate => Function + + @Test + public final void whenConvertingPredicateToFunction_thenCorrect() { + final List numbers = Lists.newArrayList(1, 2, 3, 6); + final Predicate acceptEvenNumber = new Predicate() { + @Override + public final boolean apply(final Integer number) { + return (number % 2) == 0; + } + }; + final Function isEventNumberFunction = Functions.forPredicate(acceptEvenNumber); + final List areNumbersEven = Lists.transform(numbers, isEventNumberFunction); + + assertThat(areNumbersEven, contains(false, true, false, true)); + } + }