[JAVA-621] Flattened modules hierarchy

This commit is contained in:
dupirefr
2020-04-07 21:28:45 +02:00
parent 635ecae691
commit 42b0086080
210 changed files with 24 additions and 44 deletions

View File

@@ -0,0 +1,59 @@
package com.baeldung.deepcopy;
import java.io.Serializable;
public class Address implements Serializable, Cloneable {
@Override
public Object clone() {
try {
return (Address) super.clone();
} catch (CloneNotSupportedException e) {
return new Address(this.street, this.getCity(), this.getCountry());
}
}
private static final long serialVersionUID = 1740913841244949416L;
private String street;
private String city;
private String country;
public Address(Address that) {
this(that.getStreet(), that.getCity(), that.getCountry());
}
public Address(String street, String city, String country) {
this.street = street;
this.city = city;
this.country = country;
}
public Address() {
}
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public void setStreet(String street) {
this.street = street;
}
public void setCity(String city) {
this.city = city;
}
public void setCountry(String country) {
this.country = country;
}
}

View File

@@ -0,0 +1,48 @@
package com.baeldung.deepcopy;
import java.io.Serializable;
public class User implements Serializable, Cloneable {
private static final long serialVersionUID = -3427002229954777557L;
private String firstName;
private String lastName;
private Address address;
public User(String firstName, String lastName, Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public User(User that) {
this(that.getFirstName(), that.getLastName(), new Address(that.getAddress()));
}
public User() {
}
@Override
public Object clone() {
User user;
try {
user = (User) super.clone();
} catch (CloneNotSupportedException e) {
user = new User(this.getFirstName(), this.getLastName(), this.getAddress());
}
user.address = (Address) this.address.clone();
return user;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Address getAddress() {
return address;
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.immutableobjects;
public final class Currency {
private final String value;
private Currency(String currencyValue) {
value = currencyValue;
}
public String getValue() {
return value;
}
public static Currency of(String value) {
return new Currency(value);
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.immutableobjects;
// 4. Immutability in Java
public final class Money {
private final double amount;
private final Currency currency;
public Money(double amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}
public Currency getCurrency() {
return currency;
}
public double getAmount() {
return amount;
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.inheritancecomposition.application;
import com.baeldung.inheritancecomposition.model.*;
public class Application {
public static void main(String[] args) {
Person person = new Person("John", "john@domain.com", 35);
Waitress waitress = new Waitress("Mary", "mary@domain.com", 22);
System.out.println(waitress.serveStarter("mixed salad"));
System.out.println(waitress.serveMainCourse("steak"));
System.out.println(waitress.serveDessert("cup of cofee"));
Actress actress = new Actress("Susan", "susan@domain.com", 30);
System.out.println(actress.readScript("Psycho"));
System.out.println(actress.performRole());
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
System.out.println(computer);
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.inheritancecomposition.model;
public class Actress extends Person {
public Actress(String name, String email, int age) {
super(name, email, age);
}
public String readScript(String movie) {
return "Reading the script of " + movie;
}
public String performRole() {
return "Performing a role";
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.inheritancecomposition.model;
import java.util.Optional;
public class Computer {
private Processor processor;
private Memory memory;
private SoundCard soundCard;
public Computer(Processor processor, Memory memory) {
this.processor = processor;
this.memory = memory;
}
public void setSoundCard(SoundCard soundCard) {
this.soundCard = soundCard;
}
public Processor getProcessor() {
return processor;
}
public Memory getMemory() {
return memory;
}
public Optional<SoundCard> getSoundCard() {
return Optional.ofNullable(soundCard);
}
@Override
public String toString() {
return "Computer{" + "processor=" + processor + ", memory=" + memory + ", soundcard=" + soundCard +"}";
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.inheritancecomposition.model;
public interface Memory {
String getBrand();
String getSize();
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.inheritancecomposition.model;
public class Person {
private final String name;
private final String email;
private final int age;
public Person(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" + "name=" + name + ", email=" + email + ", age=" + age + "}";
}
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.inheritancecomposition.model;
public interface Processor {
String getModel();
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.inheritancecomposition.model;
public interface SoundCard {
String getBrand();
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.inheritancecomposition.model;
public class StandardMemory implements Memory {
private String brand;
private String size;
public StandardMemory(String brand, String size) {
this.brand = brand;
this.size = size;
}
public String getBrand() {
return brand;
}
public String getSize() {
return size;
}
@Override
public String toString() {
return "Memory{" + "brand=" + brand + ", size=" + size + "}";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.model;
public class StandardProcessor implements Processor {
private String model;
public StandardProcessor(String model) {
this.model = model;
}
@Override
public String getModel() {
return model;
}
@Override
public String toString() {
return "Processor{" + "model=" + model + "}";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.model;
public class StandardSoundCard implements SoundCard {
private String brand;
public StandardSoundCard(String brand) {
this.brand = brand;
}
@Override
public String getBrand() {
return brand;
}
@Override
public String toString() {
return "SoundCard{" + "brand=" + brand + "}";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.model;
public class Waitress extends Person {
public Waitress(String name, String email, int age) {
super(name, email, age);
}
public String serveStarter(String starter) {
return "Serving a " + starter;
}
public String serveMainCourse(String mainCourse) {
return "Serving a " + mainCourse;
}
public String serveDessert(String dessert) {
return "Serving a " + dessert;
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.aggregation;
import java.util.List;
public class Car {
private List<Wheel> wheels;
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.relationships.aggregation;
import java.util.List;
public class CarWithStaticInnerWheel {
private List<Wheel> wheels;
public static class Wheel {
}
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.relationships.aggregation;
public class Wheel {
private Car car;
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.relationships.association;
public class Child {
private Mother mother;
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.association;
import java.util.List;
public class Mother {
private List<Child> children;
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.relationships.composition;
import java.util.List;
public class Building {
private String address;
private List<Room> rooms;
public class Room {
public String getBuildingAddress() {
return Building.this.address;
}
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.relationships.composition;
public class BuildingWithDefinitionRoomInMethod {
public Room createAnonymousRoom() {
return new Room() {
@Override
public void doInRoom() {}
};
}
public Room createInlineRoom() {
class InlineRoom implements Room {
@Override
public void doInRoom() {}
}
return new InlineRoom();
}
public Room createLambdaRoom() {
return () -> {};
}
public interface Room {
void doInRoom();
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.university;
import java.util.List;
public class Department {
private List<Professor> professors;
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.relationships.university;
import java.util.List;
public class Professor {
private List<Department> department;
private List<Professor> friends;
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.relationships.university;
import java.util.List;
public class University {
private List<Department> department;
}

View File

@@ -0,0 +1,137 @@
package com.baeldung.deepcopy;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Ignore;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
public class DeepCopyUnitTest {
@Test
public void whenCreatingDeepCopyWithCopyConstructor_thenObjectsShouldNotBeSame() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User deepCopy = new User(pm);
assertThat(deepCopy).isNotSameAs(pm);
}
@Test
public void whenModifyingOriginalObject_thenCopyShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User deepCopy = new User(pm);
address.setCountry("Great Britain");
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}
@Test
public void whenModifyingOriginalObject_thenCloneCopyShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User deepCopy = (User) pm.clone();
address.setCountry("Great Britain");
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}
@Test
public void whenModifyingOriginalObject_thenCommonsCloneShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User deepCopy = (User) SerializationUtils.clone(pm);
address.setCountry("Great Britain");
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}
@Test
public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
Gson gson = new Gson();
User deepCopy = gson.fromJson(gson.toJson(pm), User.class);
address.setCountry("Great Britain");
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}
@Test
public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws IOException {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
ObjectMapper objectMapper = new ObjectMapper();
User deepCopy = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class);
address.setCountry("Great Britain");
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
}
@Test
@Ignore
public void whenMakingCopies_thenShowHowLongEachMethodTakes() throws CloneNotSupportedException, IOException {
int times = 1000000;
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
long start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
User primeMinisterClone = (User) SerializationUtils.clone(pm);
}
long end = System.currentTimeMillis();
System.out.println("Cloning with Apache Commons Lang took " + (end - start) + " milliseconds.");
start = System.currentTimeMillis();
Gson gson = new Gson();
for (int i = 0; i < times; i++) {
User primeMinisterClone = gson.fromJson(gson.toJson(pm), User.class);
}
end = System.currentTimeMillis();
System.out.println("Cloning with Gson took " + (end - start) + " milliseconds.");
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
User primeMinisterClone = new User(pm);
}
end = System.currentTimeMillis();
System.out.println("Cloning with the copy constructor took " + (end - start) + " milliseconds.");
start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
User primeMinisterClone = (User) pm.clone();
}
end = System.currentTimeMillis();
System.out.println("Cloning with Cloneable interface took " + (end - start) + " milliseconds.");
start = System.currentTimeMillis();
ObjectMapper objectMapper = new ObjectMapper();
for (int i = 0; i < times; i++) {
User primeMinisterClone = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class);
}
end = System.currentTimeMillis();
System.out.println("Cloning with Jackson took " + (end - start) + " milliseconds.");
}
@Test
public void whenModifyingOriginalObject_ThenCopyShouldChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
address.setCountry("Great Britain");
assertThat(shallowCopy.getAddress().getCountry()).isEqualTo(pm.getAddress().getCountry());
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.deepcopy;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class ShallowCopyUnitTest {
@Test
public void whenShallowCopying_thenObjectsShouldNotBeSame() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
assertThat(shallowCopy)
.isNotSameAs(pm);
}
@Test
public void whenModifyingOriginalObject_thenCopyShouldChange() {
Address address = new Address("Downing St 10", "London", "England");
User pm = new User("Prime", "Minister", address);
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
address.setCountry("Great Britain");
assertThat(shallowCopy.getAddress().getCountry())
.isEqualTo(pm.getAddress().getCountry());
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.immutableobjects;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class ImmutableObjectsUnitTest {
@Test
public void whenCallingStringReplace_thenStringDoesNotMutate() {
// 2. What's an Immutable Object?
final String name = "baeldung";
final String newName = name.replace("dung", "----");
assertEquals("baeldung", name);
assertEquals("bael----", newName);
}
public void whenReassignFinalValue_thenCompilerError() {
// 3. The final Keyword in Java (1)
final String name = "baeldung";
// name = "bael...";
}
@Test
public void whenAddingElementToList_thenSizeChange() {
// 3. The final Keyword in Java (2)
final List<String> strings = new ArrayList<>();
assertEquals(0, strings.size());
strings.add("baeldung");
assertEquals(1, strings.size());
}
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Actress;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class ActressUnitTest {
private static Actress actress;
@BeforeClass
public static void setUpActressInstance() {
actress = new Actress("Susan", "susan@domain.com", 30);
}
@Test
public void givenActressInstance_whenCalledgetName_thenEqual() {
assertThat(actress.getName()).isEqualTo("Susan");
}
@Test
public void givenActressInstance_whenCalledgetEmail_thenEqual() {
assertThat(actress.getEmail()).isEqualTo("susan@domain.com");
}
@Test
public void givenActressInstance_whenCalledgetAge_thenEqual() {
assertThat(actress.getAge()).isEqualTo(30);
}
@Test
public void givenActressInstance_whenCalledreadScript_thenEqual() {
assertThat(actress.readScript("Psycho")).isEqualTo("Reading the script of Psycho");
}
@Test
public void givenActressInstance_whenCalledperfomRole_thenEqual() {
assertThat(actress.performRole()).isEqualTo("Performing a role");
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.*;
import org.junit.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
public class CompositionUnitTest {
@Test
public void givenComputerInstance_whenExtractedEachField_thenThreeAssertions() {
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
assertThat(computer.getProcessor()).isInstanceOf(Processor.class);
assertThat(computer.getMemory()).isInstanceOf(Memory.class);
assertThat(computer.getSoundCard()).isInstanceOf(Optional.class);
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Actress;
import com.baeldung.inheritancecomposition.model.Person;
import com.baeldung.inheritancecomposition.model.Waitress;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class InheritanceUnitTest {
@Test
public void givenWaitressInstance_whenCheckedType_thenIsInstanceOfPerson() {
assertThat(new Waitress("Mary", "mary@domain.com", 22)).isInstanceOf(Person.class);
}
@Test
public void givenActressInstance_whenCheckedType_thenIsInstanceOfPerson() {
assertThat(new Actress("Susan", "susan@domain.com", 30)).isInstanceOf(Person.class);
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Person;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class PersonUnitTest {
private static Person person;
@BeforeClass
public static void setPersonInstance() {
person = new Person("John", "john@domain.com", 35);
}
@Test
public void givenPersonInstance_whenCalledgetName_thenEqual() {
assertThat(person.getName()).isEqualTo("John");
}
@Test
public void givenPersonInstance_whenCalledgetEmail_thenEqual() {
assertThat(person.getEmail()).isEqualTo("john@domain.com");
}
@Test
public void givenPersonInstance_whenCalledgetAge_thenEqual() {
assertThat(person.getAge()).isEqualTo(35);
}
}

View File

@@ -0,0 +1,47 @@
package com.baeldung.inheritancecomposition;
import com.baeldung.inheritancecomposition.model.Waitress;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class WaitressUnitTest {
private static Waitress waitress;
@BeforeClass
public static void setUpWaitressInstance() {
waitress = new Waitress("Mary", "mary@domain.com", 22);
}
@Test
public void givenWaitressInstance_whenCalledgetName_thenOneAssertion() {
assertThat(waitress.getName()).isEqualTo("Mary");
}
@Test
public void givenWaitressInstance_whenCalledgetEmail_thenOneAssertion() {
assertThat(waitress.getEmail()).isEqualTo("mary@domain.com");
}
@Test
public void givenWaitressInstance_whenCalledgetAge_thenOneAssertion() {
assertThat(waitress.getAge()).isEqualTo(22);
}
@Test
public void givenWaitressInstance_whenCalledserveStarter_thenOneAssertion() {
assertThat(waitress.serveStarter("mixed salad")).isEqualTo("Serving a mixed salad");
}
@Test
public void givenWaitressInstance_whenCalledserveMainCourse_thenOneAssertion() {
assertThat(waitress.serveMainCourse("steak")).isEqualTo("Serving a steak");
}
@Test
public void givenWaitressInstance_whenCalledserveDessert_thenOneAssertion() {
assertThat(waitress.serveDessert("cup of coffee")).isEqualTo("Serving a cup of coffee");
}
}