Skip to content

Updating JDK to 17 and GSON to 2.8.9 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
Expand All @@ -26,7 +26,7 @@
<guava.version>31.0.1-jre</guava.version>
<j.assert.version>3.21.0</j.assert.version>
<junit.version>5.8.1</junit.version>
<gson.version>2.8.8</gson.version>
<gson.version>2.8.9</gson.version>
<commons-io.version>2.11.0</commons-io.version>
</properties>

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/amigoscode/examples/Filtering.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.amigoscode.examples;

import com.amigoscode.beans.Car;

import java.util.List;
import java.util.function.Predicate;

class Filtering {

private final Predicate<Car> price = car -> car.getPrice() < 20_000.00;
private final Predicate<Car> color = car -> car.getColor().equals("Yellow");
private final List<Car> cars;

Filtering(List<Car> cars) {
this.cars = cars;
}

List<Car> getYellowCarsUnder20k() {
return cars.stream()
.filter(price)
.filter(color)
.toList();
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/amigoscode/examples/GettingStarted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.amigoscode.examples;

import com.amigoscode.beans.Person;

import java.util.ArrayList;
import java.util.List;

class GettingStarted {

private final List<Person> people;

GettingStarted(List<Person> people) {
this.people = people;
}

List<Person> imperativeApproach() {
var youngPeople = new ArrayList<Person>();
var limit = 10;
var counter = 0;
for (Person person : people) {
if (person.getAge() <= 18) {
youngPeople.add(person);
counter++;
}
if (counter == limit) {
break;
}
}
return youngPeople;
}

List<Person> declarativeApproachUsingStreams() {
return people.stream()
.filter(person -> person.getAge() <= 18)
.limit(10)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
package com.amigoscode.examples;

import com.amigoscode.beans.Car;
import com.amigoscode.beans.Person;
import com.amigoscode.mockdata.MockData;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Filtering {
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class FilteringTest {

@Test
public void filter() throws Exception {
List<Car> cars = MockData.getCars();
void get_all_yellow_cars_under_20k() throws Exception {
var cars = MockData.getCars();
var filtering = new Filtering(cars);

var carsUnder20k = filtering.getYellowCarsUnder20k();

assertThat(carsUnder20k.size()).isEqualTo(9);
carsUnder20k.forEach(car ->
assertThat(car.getPrice()).isLessThan(20_000.00));
}

@Test
public void dropWhile() throws Exception {
System.out.println("using filter");
void dropWhile() throws Exception {
System.out.println("using filtering");
Stream.of(2, 4, 6, 8, 9, 10, 12).filter(n -> n % 2 == 0)
.forEach(n -> System.out.print(n + " "));
System.out.println();
System.out.println("using dropWhile");
}

@Test
public void takeWhile() throws Exception {
// using filter
System.out.println("using filter");
void takeWhile() throws Exception {
// using filtering
System.out.println("using filtering");
Stream.of(2, 4, 6, 8, 9, 10, 12).filter(n -> n % 2 == 0)
.forEach(n -> System.out.print(n + " "));

Expand All @@ -40,22 +42,22 @@ public void takeWhile() throws Exception {
}

@Test
public void findFirst() throws Exception {
void findFirst() throws Exception {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}

@Test
public void findAny() throws Exception {
void findAny() throws Exception {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10};
}

@Test
public void allMatch() throws Exception {
void allMatch() throws Exception {
int[] even = {2, 4, 6, 8, 10};
}

@Test
public void anyMatch() throws Exception {
void anyMatch() throws Exception {
int[] evenAndOneOdd = {2, 4, 6, 8, 10, 11};
}

Expand Down
26 changes: 0 additions & 26 deletions src/test/java/com/amigoscode/examples/GettingStarted.java

This file was deleted.

56 changes: 56 additions & 0 deletions src/test/java/com/amigoscode/examples/GettingStartedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.amigoscode.examples;

import com.amigoscode.beans.Person;
import com.amigoscode.mockdata.MockData;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.fail;


class GettingStartedTest {

private static final String FAILED_TO_FETCH_MOCK_DATA_FOR_THE_TEST = "Failed to fetch mock data for the test";
private GettingStarted gettingStarted;
private List<Person> people;

@BeforeEach
void setup() {
try {
people = MockData.getPeople();
} catch (IOException e) {
fail(FAILED_TO_FETCH_MOCK_DATA_FOR_THE_TEST);
}
gettingStarted = new GettingStarted(people);
}

@Test
void imperative_approach() {
// 1. Find people aged less or equal 18
// 2. Then change implementation to find first 10 people

var youngPeople = gettingStarted.imperativeApproach();

assertThat(youngPeople.size()).isEqualTo(10);
for (Person person : youngPeople) {
assertThat(person.getAge()).isLessThanOrEqualTo(18);
}
}

@Test
void declarativeApproachUsingStreams() {
// 1. Find people aged less or equal 18
// 2. Then change implementation to find first 10 people

var youngPeople = gettingStarted.declarativeApproachUsingStreams();

assertThat(youngPeople.size()).isEqualTo(10);
for (Person person : youngPeople) {
assertThat(person.getAge()).isLessThanOrEqualTo(18);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.amigoscode.examples;

import com.amigoscode.beans.Car;
import com.amigoscode.beans.Person;
import com.amigoscode.beans.PersonDTO;
import com.amigoscode.mockdata.MockData;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class TransformationsMapAndReduceTest {

@Test
void yourFirstTransformationWithMap() throws IOException {
List<Person> people = MockData.getPeople();

var peopleDTos = people.stream()
.map(PersonDTO::map)
.toList();

assertThat(people.size()).isEqualTo(peopleDTos.size());
peopleDTos.forEach(personDTO -> {
long count = people.stream()
.filter(person -> person.getId() == personDTO.getId())
.count();
assertThat(count).isEqualTo(1);
});
}

@Test
void mapToDoubleAndFindAverageCarPrice() throws IOException {
var cars = MockData.getCars();

double averageCarPrice = cars.stream()
.mapToDouble(Car::getPrice)
.average()
.getAsDouble();

assertThat(averageCarPrice).isEqualTo(52693.19979);
}

@Test
void reduce() {
int[] integers = {1, 2, 3, 4, 99, 100, 121, 1302, 199};

var reduced = Arrays.stream(integers)
.reduce(0, Integer::sum);

assertThat(reduced).isEqualTo(1831);
}
}

16 changes: 0 additions & 16 deletions src/test/java/com/amigoscode/examples/WorkingWithStreams.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/test/java/com/amigoscode/examples/WorkingWithStreamsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.amigoscode.examples;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class WorkingWithStreamsTest {

@Test
void count_items_on_stream() {
var names = List.of("Amigoscode", "Alex", "Zara", "Sarah", "Alan");
var stream = names.stream();

var count = stream
.filter(name -> name.charAt(0) == 'A')
.limit(2)
.count();

assertThat(count).isEqualTo(2);
}
}