├── settings.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── main │ └── java │ │ └── com │ │ └── technologyconversations │ │ └── java8exercises │ │ └── streams │ │ ├── Sum.java │ │ ├── Person.java │ │ ├── OldestPerson.java │ │ ├── Kids.java │ │ ├── PeopleStats.java │ │ ├── ToUpperCase.java │ │ ├── Partitioning.java │ │ ├── Stats.java │ │ ├── Joining.java │ │ ├── FlatCollection.java │ │ ├── FilterCollection.java │ │ └── Grouping.java └── test │ └── java │ └── com │ └── technologyconversations │ └── java8exercises │ └── streams │ ├── SumSpec.java │ ├── FilterCollectionSpec.java │ ├── ToUpperCaseSpec.java │ ├── FlatCollectionSpec.java │ ├── OldestPersonSpec.java │ ├── JoiningSpec.java │ ├── KidsSpec.java │ ├── PartitioningSpec.java │ ├── GroupingSpec.java │ └── PeopleStatsSpec.java ├── README.md ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'java-8-exercises' 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | .gradle 4 | build 5 | out 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vfarcic/java-8-exercises/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 27 11:50:21 CET 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-all.zip 7 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Sum.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.List; 4 | 5 | public class Sum { 6 | 7 | private Sum() { 8 | } 9 | 10 | public static int calculate7(List numbers) { 11 | int total = 0; 12 | for (int number : numbers) { 13 | total += number; 14 | } 15 | return total; 16 | } 17 | 18 | public static int calculate(List people) { 19 | return people.stream() // Convert collection to Stream 20 | .reduce(0, (total, number) -> total + number); // Sum elements with 0 as starting value 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/SumSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.Sum.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | /* 12 | Sum all elements of a collection 13 | */ 14 | public class SumSpec { 15 | 16 | @Test 17 | public void calculateShouldReturnSumOfAllIntegersInCollection() { 18 | List numbers = asList(1, 2, 3, 4, 5); 19 | assertThat(calculate(numbers)).isEqualTo(1 + 2 + 3 + 4 + 5); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/FilterCollectionSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.FilterCollection.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class FilterCollectionSpec { 12 | 13 | @Test 14 | public void transformKeepStringsShorterThant4Characters() { 15 | List collection = asList("My", "name", "is", "John", "Doe"); 16 | List expected = asList("My", "is", "Doe"); 17 | assertThat(transform(collection)).hasSameElementsAs(expected); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/ToUpperCaseSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.ToUpperCase.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class ToUpperCaseSpec { 12 | 13 | @Test 14 | public void transformShouldConvertCollectionElementsToUpperCase() { 15 | List collection = asList("My", "name", "is", "John", "Doe"); 16 | List expected = asList("MY", "NAME", "IS", "JOHN", "DOE"); 17 | assertThat(transform(collection)).hasSameElementsAs(expected); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/FlatCollectionSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.FlatCollection.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class FlatCollectionSpec { 12 | 13 | @Test 14 | public void transformShouldFlattenCollection() { 15 | List> collection = asList(asList("Viktor", "Farcic"), asList("John", "Doe", "Third")); 16 | List expected = asList("Viktor", "Farcic", "John", "Doe", "Third"); 17 | assertThat(transform(collection)).hasSameElementsAs(expected); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/OldestPersonSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.OldestPerson.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class OldestPersonSpec { 12 | 13 | @Test 14 | public void getOldestPersonShouldReturnOldestPerson() { 15 | Person sara = new Person("Sara", 4); 16 | Person viktor = new Person("Viktor", 40); 17 | Person eva = new Person("Eva", 42); 18 | List collection = asList(sara, eva, viktor); 19 | assertThat(getOldestPerson(collection)).isEqualToComparingFieldByField(eva); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Person.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | public class Person { 4 | 5 | private String name; 6 | private int age; 7 | private String nationality; 8 | 9 | public Person(final String nameValue, final int ageValue) { 10 | name = nameValue; 11 | age = ageValue; 12 | } 13 | 14 | public Person(final String nameValue, final int ageValue, final String nationalityValue) { 15 | name = nameValue; 16 | age = ageValue; 17 | nationality = nationalityValue; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public int getAge() { 25 | return age; 26 | } 27 | 28 | public String getNationality() { 29 | return nationality; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/JoiningSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.Joining.namesToString; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class JoiningSpec { 12 | 13 | @Test 14 | public void toStringShouldReturnPeopleNamesSeparatedByComma() { 15 | Person sara = new Person("Sara", 4); 16 | Person viktor = new Person("Viktor", 40); 17 | Person eva = new Person("Eva", 42); 18 | List collection = asList(sara, viktor, eva); 19 | assertThat(namesToString(collection)) 20 | .isEqualTo("Names: Sara, Viktor, Eva."); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/OldestPerson.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.Comparator; 4 | import java.util.List; 5 | 6 | public class OldestPerson { 7 | 8 | public static Person getOldestPerson7(List people) { 9 | Person oldestPerson = new Person("", 0); 10 | for (Person person : people) { 11 | if (person.getAge() > oldestPerson.getAge()) { 12 | oldestPerson = person; 13 | } 14 | } 15 | return oldestPerson; 16 | } 17 | 18 | public static Person getOldestPerson(List people) { 19 | return people.stream() // Convert collection to Stream 20 | .max(Comparator.comparing(Person::getAge)) // Compares people ages 21 | .get(); // Gets stream result 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Kids.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.*; 4 | 5 | import static java.util.stream.Collectors.toSet; 6 | 7 | public class Kids { 8 | 9 | public static Set getKidNames7(List people) { 10 | Set kids = new HashSet<>(); 11 | for (Person person : people) { 12 | if (person.getAge() < 18) { 13 | kids.add(person.getName()); 14 | } 15 | } 16 | return kids; 17 | } 18 | 19 | public static Set getKidNames(List people) { 20 | return people.stream() 21 | .filter(person -> person.getAge() < 18) // Filter kids (under age of 18) 22 | .map(Person::getName) // Map Person elements to names 23 | .collect(toSet()); // Collect values to a Set 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/KidsSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.Kids.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class KidsSpec { 12 | 13 | @Test 14 | public void getKidNameShouldReturnNamesOfYoungerThan18() { 15 | Person sara = new Person("Sara", 4); 16 | Person viktor = new Person("Viktor", 40); 17 | Person eva = new Person("Eva", 42); 18 | Person anna = new Person("Anna", 5); 19 | List collection = asList(sara, eva, viktor, anna); 20 | assertThat(getKidNames(collection)) 21 | .contains("Sara", "Anna") 22 | .doesNotContain("Viktor", "Eva"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/PeopleStats.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.IntSummaryStatistics; 4 | import java.util.List; 5 | 6 | public class PeopleStats { 7 | 8 | private PeopleStats() { 9 | } 10 | 11 | public static Stats getStats7(List people) { 12 | long sum = 0; 13 | int min = people.get(0).getAge(); 14 | int max = 0; 15 | for (Person person : people) { 16 | int age = person.getAge(); 17 | sum += age; 18 | min = Math.min(min, age); 19 | max = Math.max(max, age); 20 | } 21 | return new Stats(people.size(), sum, min, max); 22 | } 23 | 24 | public static IntSummaryStatistics getStats(List people) { 25 | return people.stream() 26 | .mapToInt(Person::getAge) 27 | .summaryStatistics(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/ToUpperCase.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static java.util.stream.Collectors.toList; 7 | 8 | public class ToUpperCase { 9 | 10 | private ToUpperCase() { 11 | } 12 | 13 | public static List transform7(List collection) { 14 | List newCollection = new ArrayList<>(); 15 | for (String element : collection) { 16 | newCollection.add(element.toUpperCase()); 17 | } 18 | return newCollection; 19 | } 20 | 21 | public static List transform(List collection) { 22 | return collection.stream() // Convert collection to Stream 23 | .map(String::toUpperCase) // Convert each element to upper case 24 | .collect(toList()); // Collect results to a new list 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Partitioning.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.*; 4 | import static java.util.stream.Collectors.*; 5 | 6 | public class Partitioning { 7 | 8 | private Partitioning() { 9 | } 10 | 11 | public static Map> partitionAdults7(List people) { 12 | Map> map = new HashMap<>(); 13 | map.put(true, new ArrayList<>()); 14 | map.put(false, new ArrayList<>()); 15 | for (Person person : people) { 16 | map.get(person.getAge() >= 18).add(person); 17 | } 18 | return map; 19 | } 20 | 21 | public static Map> partitionAdults(List people) { 22 | return people.stream() // Convert collection to Stream 23 | .collect(partitioningBy(p -> p.getAge() >= 18)); // Partition stream of people into adults (age => 18) and kids 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/PartitioningSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import static com.technologyconversations.java8exercises.streams.Partitioning.*; 9 | import static java.util.Arrays.asList; 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | 12 | public class PartitioningSpec { 13 | 14 | @Test 15 | public void partitionAdultsShouldSeparateKidsFromAdults() { 16 | Person sara = new Person("Sara", 4); 17 | Person viktor = new Person("Viktor", 40); 18 | Person eva = new Person("Eva", 42); 19 | List collection = asList(sara, eva, viktor); 20 | Map> result = partitionAdults(collection); 21 | assertThat(result.get(true)).hasSameElementsAs(asList(viktor, eva)); 22 | assertThat(result.get(false)).hasSameElementsAs(asList(sara)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Stats.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | public class Stats { 4 | 5 | private long count; 6 | private long sum; 7 | private int min; 8 | private int max; 9 | 10 | public Stats(final long countValue, 11 | final long sumValue, 12 | final int minValue, 13 | final int maxValue) { 14 | count = countValue; 15 | sum = sumValue; 16 | min = minValue; 17 | max = maxValue; 18 | } 19 | 20 | 21 | public final long getCount() { 22 | return count; 23 | } 24 | 25 | public final long getSum() { 26 | return sum; 27 | } 28 | 29 | public final int getMin() { 30 | return min; 31 | } 32 | 33 | public final int getMax() { 34 | return max; 35 | } 36 | 37 | public final double getAverage() { 38 | return getCount() > 0 ? (double) getSum() / getCount() : 0.0d; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Joining.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.List; 4 | 5 | import static java.util.stream.Collectors.joining; 6 | 7 | public class Joining { 8 | 9 | private Joining() { 10 | } 11 | 12 | public static String namesToString7(List people) { 13 | String label = "Names: "; 14 | StringBuilder sb = new StringBuilder(label); 15 | for (Person person : people) { 16 | if (sb.length() > label.length()) { 17 | sb.append(", "); 18 | } 19 | sb.append(person.getName()); 20 | } 21 | sb.append("."); 22 | return sb.toString(); 23 | } 24 | 25 | public static String namesToString(List people) { 26 | return people.stream() // Convert collection to Stream 27 | .map(Person::getName) // Map Person to name 28 | .collect(joining(", ", "Names: ", ".")); // Join names 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/GroupingSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import static com.technologyconversations.java8exercises.streams.Grouping.*; 9 | import static java.util.Arrays.asList; 10 | import static org.assertj.core.api.Assertions.assertThat; 11 | 12 | public class GroupingSpec { 13 | 14 | @Test 15 | public void partitionAdultsShouldSeparateKidsFromAdults() { 16 | Person sara = new Person("Sara", 4, "Norwegian"); 17 | Person viktor = new Person("Viktor", 40, "Serbian"); 18 | Person eva = new Person("Eva", 42, "Norwegian"); 19 | List collection = asList(sara, eva, viktor); 20 | Map> result = groupByNationality(collection); 21 | assertThat(result.get("Norwegian")).hasSameElementsAs(asList(sara, eva)); 22 | assertThat(result.get("Serbian")).hasSameElementsAs(asList(viktor)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/FlatCollection.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static java.util.stream.Collectors.toList; 7 | 8 | public class FlatCollection { 9 | 10 | private FlatCollection() { 11 | } 12 | 13 | public static List transform7(List> collection) { 14 | List newCollection = new ArrayList<>(); 15 | for (List subCollection : collection) { 16 | for (String value : subCollection) { 17 | newCollection.add(value); 18 | } 19 | } 20 | return newCollection; 21 | } 22 | 23 | public static List transform(List> collection) { 24 | return collection.stream() // Convert collection to Stream 25 | .flatMap(value -> value.stream()) // Replace list with stream 26 | .collect(toList()); // Collect results to a new list 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/FilterCollection.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static java.util.stream.Collectors.toList; 7 | 8 | public class FilterCollection { 9 | 10 | private FilterCollection() { 11 | } 12 | 13 | public static List transform7(List collection) { 14 | List newCollection = new ArrayList<>(); 15 | for (String element : collection) { 16 | if (element.length() < 4) { 17 | newCollection.add(element); 18 | } 19 | } 20 | return newCollection; 21 | } 22 | 23 | public static List transform(List collection) { 24 | return collection.stream() // Convert collection to Stream 25 | .filter(value -> value.length() < 4) // Filter elements with length smaller than 4 characters 26 | .collect(toList()); // Collect results to a new list 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/technologyconversations/java8exercises/streams/Grouping.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import static java.util.stream.Collectors.*; 9 | 10 | public class Grouping { 11 | 12 | private Grouping() { 13 | } 14 | 15 | public static Map> groupByNationality7(List people) { 16 | Map> map = new HashMap<>(); 17 | for (Person person : people) { 18 | if (!map.containsKey(person.getNationality())) { 19 | map.put(person.getNationality(), new ArrayList<>()); 20 | } 21 | map.get(person.getNationality()).add(person); 22 | } 23 | return map; 24 | } 25 | 26 | public static Map> groupByNationality(List people) { 27 | return people.stream() // Convert collection to Stream 28 | .collect(groupingBy(Person::getNationality)); // Group people by nationality 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Prepare project for your favorite IDE 2 | =================================== 3 | Use gradle to import dependencies and create ide-specific project files. Gradle wrapper scripts are included (no need to download anything new) 4 | ``` 5 | ./gradlew eclipse # Eclipse 6 | ./gradlew idea # IntelliJ IDEA 7 | 8 | # use gradlew.bat on Windows 9 | ``` 10 | 11 | ToUpperCase (map) 12 | ================= 13 | 14 | Convert elements of a collection to upper case. 15 | 16 | FilterCollection (filter) 17 | ========================= 18 | 19 | Filter collection so that only elements with less then 4 characters are returned. 20 | 21 | FlatCollection (flatMap) 22 | ======================== 23 | 24 | Flatten multidimensional collection 25 | 26 | OldestPerson (max, comparator) 27 | ============================== 28 | 29 | Get oldest person from the collection 30 | 31 | Sum (reduce) 32 | ============ 33 | 34 | Sum all elements of a collection 35 | 36 | Kids (filter, map) 37 | ================== 38 | 39 | Get names of all kids (under age of 18) 40 | 41 | PeopleStats (summaryStatistics) 42 | =============================== 43 | 44 | Get people statistics: average age, count, maximum age, minimum age and sum og all ages. 45 | 46 | Partitionaing (partitioningBy) 47 | ========================= 48 | 49 | Partition adults and kids 50 | 51 | Grouping (groupingBy) 52 | ========================= 53 | 54 | Group people by nationality 55 | 56 | -------------------------------------------------------------------------------- /src/test/java/com/technologyconversations/java8exercises/streams/PeopleStatsSpec.java: -------------------------------------------------------------------------------- 1 | package com.technologyconversations.java8exercises.streams; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static com.technologyconversations.java8exercises.streams.PeopleStats.*; 8 | import static java.util.Arrays.asList; 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | public class PeopleStatsSpec { 12 | 13 | Person sara = new Person("Sara", 4); 14 | Person viktor = new Person("Viktor", 40); 15 | Person eva = new Person("Eva", 42); 16 | List collection = asList(sara, eva, viktor); 17 | 18 | @Test 19 | public void getStatsShouldReturnAverageAge() { 20 | assertThat(getStats(collection).getAverage()) 21 | .isEqualTo((double)(4 + 40 + 42) / 3); 22 | } 23 | 24 | @Test 25 | public void getStatsShouldReturnNumberOfPeople() { 26 | assertThat(getStats(collection).getCount()) 27 | .isEqualTo(3); 28 | } 29 | 30 | @Test 31 | public void getStatsShouldReturnMaximumAge() { 32 | assertThat(getStats(collection).getMax()) 33 | .isEqualTo(42); 34 | } 35 | 36 | @Test 37 | public void getStatsShouldReturnMinimumAge() { 38 | assertThat(getStats(collection).getMin()) 39 | .isEqualTo(4); 40 | } 41 | 42 | @Test 43 | public void getStatsShouldReturnSumOfAllAges() { 44 | assertThat(getStats(collection).getSum()) 45 | .isEqualTo(40 + 42 + 4); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | --------------------------------------------------------------------------------