Merge branch 'eugenp:master' into master
This commit is contained in:
commit
974b27305a
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.instanceofalternative.enumallt;
|
||||||
|
|
||||||
|
public enum DinosaurEnum {
|
||||||
|
Anatotitan {
|
||||||
|
@Override
|
||||||
|
public String move() {
|
||||||
|
return "running";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Euraptor {
|
||||||
|
@Override
|
||||||
|
public String move() {
|
||||||
|
return "flying";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public abstract String move();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.instanceofalternative.model;
|
||||||
|
|
||||||
|
public class Anatotitan extends Dinosaur {
|
||||||
|
// polymorphism
|
||||||
|
@Override
|
||||||
|
public String move() {
|
||||||
|
return "running";
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-polymorphism
|
||||||
|
public String run() {
|
||||||
|
return "running";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.instanceofalternative.model;
|
||||||
|
|
||||||
|
public class Dinosaur {
|
||||||
|
|
||||||
|
public String move() {
|
||||||
|
return "default movement";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.instanceofalternative.model;
|
||||||
|
|
||||||
|
public class Euraptor extends Dinosaur {
|
||||||
|
// polymorphism
|
||||||
|
@Override
|
||||||
|
public String move() {
|
||||||
|
return "flying";
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-polymorphism
|
||||||
|
public String flies() {
|
||||||
|
return "flying";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public class Anatotitan implements Dino {
|
||||||
|
|
||||||
|
String run() {
|
||||||
|
return "running";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String move(Visitor dinobehave) {
|
||||||
|
return dinobehave.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public interface Dino {
|
||||||
|
|
||||||
|
String move(Visitor dinoMove);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public class DinoVisitorImpl implements Visitor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visit(Anatotitan anatotitan) {
|
||||||
|
return anatotitan.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visit(Euraptor euraptor) {
|
||||||
|
return euraptor.flies();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public class Euraptor implements Dino {
|
||||||
|
|
||||||
|
String flies() {
|
||||||
|
return "flying";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String move(Visitor dinobehave) {
|
||||||
|
return dinobehave.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.instanceofalternative.visitorspattern;
|
||||||
|
|
||||||
|
public interface Visitor {
|
||||||
|
|
||||||
|
String visit(Anatotitan anatotitan);
|
||||||
|
|
||||||
|
String visit(Euraptor euraptor);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.instanceoftest;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.instanceofalternative.enumallt.*;
|
||||||
|
|
||||||
|
public class EnumUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingEnum_thenGetMovementOfEuraptor() {
|
||||||
|
|
||||||
|
assertEquals("flying", moveDinosaurUsingEnum(DinosaurEnum.Euraptor));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingEnum_thenGetMovementOfAnatotitan() {
|
||||||
|
assertEquals("running", moveDinosaurUsingEnum(DinosaurEnum.Anatotitan));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String moveDinosaurUsingEnum(DinosaurEnum dinosaurenum) {
|
||||||
|
return dinosaurenum.move();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.instanceoftest;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.instanceofalternative.model.*;
|
||||||
|
|
||||||
|
public class ExampleSetupUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingInstancof_thenGetMovementOfAnatotitan() {
|
||||||
|
|
||||||
|
assertEquals("running", moveDinosaurUsingInstanceof(new Anatotitan()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingInstanceof_thenGetMovementOfEuraptor() {
|
||||||
|
assertEquals("flying", moveDinosaurUsingInstanceof(new Euraptor()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String moveDinosaurUsingInstanceof(Dinosaur dinosaur) {
|
||||||
|
|
||||||
|
if (dinosaur instanceof Anatotitan) {
|
||||||
|
|
||||||
|
Anatotitan anatotitan = (Anatotitan) dinosaur;
|
||||||
|
return anatotitan.run();
|
||||||
|
} else if (dinosaur instanceof Euraptor) {
|
||||||
|
Euraptor euraptor = (Euraptor) dinosaur;
|
||||||
|
return euraptor.flies();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.instanceoftest;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.instanceofalternative.model.*;
|
||||||
|
|
||||||
|
public class GetClassUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingGetClass_thenGetMovementOfAnatotitan() {
|
||||||
|
|
||||||
|
assertEquals("running", moveDinosaurUsingGetClass(new Anatotitan()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingGetClass_thenGetMovementOfEuraptor() {
|
||||||
|
assertEquals("flying", moveDinosaurUsingGetClass(new Euraptor()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String moveDinosaurUsingGetClass(Dinosaur dinosaur) {
|
||||||
|
|
||||||
|
if (dinosaur.getClass()
|
||||||
|
.equals(Anatotitan.class)) {
|
||||||
|
|
||||||
|
Anatotitan anatotitan = (Anatotitan) dinosaur;
|
||||||
|
return anatotitan.run();
|
||||||
|
} else if (dinosaur.getClass()
|
||||||
|
.equals(Euraptor.class)) {
|
||||||
|
Euraptor euraptor = (Euraptor) dinosaur;
|
||||||
|
return euraptor.flies();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.instanceoftest;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.instanceofalternative.model.*;
|
||||||
|
|
||||||
|
public class PolymorphismUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingPolymorphism_thenGetMovementOfAnatotitan() {
|
||||||
|
|
||||||
|
assertEquals("running", moveDinosaurUsingPolymorphism(new Anatotitan()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingPolymorphism_thenGetMovementOfEuraptor() {
|
||||||
|
assertEquals("flying", moveDinosaurUsingPolymorphism(new Euraptor()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String moveDinosaurUsingPolymorphism(Dinosaur dinosaur) {
|
||||||
|
return dinosaur.move();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.instanceoftest;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.instanceofalternative.visitorspattern.*;
|
||||||
|
|
||||||
|
public class VisitorsPatternUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingVisitorPattern_thenGetMovementOfAnatotitan() {
|
||||||
|
|
||||||
|
assertEquals("running", moveDinosaurUsingVisitorPattern((Dino) new Anatotitan()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADinosaurSpecie_whenUsingVisitorPattern_thenGetMovementOfEuraptor() {
|
||||||
|
|
||||||
|
assertEquals("flying", moveDinosaurUsingVisitorPattern((Dino) new Euraptor()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String moveDinosaurUsingVisitorPattern(Dino dinosaur) {
|
||||||
|
Visitor visitor = new DinoVisitorImpl();
|
||||||
|
|
||||||
|
return dinosaur.move(visitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,12 +3,7 @@ package com.baeldung.exception.exceptions_vs_errors;
|
||||||
public class ErrorExample {
|
public class ErrorExample {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
overflow();
|
throw new AssertionError();
|
||||||
}
|
|
||||||
|
|
||||||
public static void overflow() {
|
|
||||||
System.out.println("overflow...");
|
|
||||||
overflow();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,11 @@ package com.baeldung.exception.exceptions_vs_errors;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
|
||||||
// Unit test for the ErrorExample class.
|
|
||||||
public class ErrorExampleUnitTest {
|
public class ErrorExampleUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenMainMethodIsRun_thenStackOverflowError() {
|
public void whenMainMethodIsRun_thenStackOverflowError() {
|
||||||
Assertions.assertThrows(StackOverflowError.class,
|
Assertions.assertThrows(AssertionError.class,
|
||||||
() -> ErrorExample.main(null));
|
() -> ErrorExample.main(null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,5 +14,4 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||||
- [An Introduction to the Constant Pool in the JVM](https://www.baeldung.com/jvm-constant-pool)
|
- [An Introduction to the Constant Pool in the JVM](https://www.baeldung.com/jvm-constant-pool)
|
||||||
- [List All the Classes Loaded in the JVM](https://www.baeldung.com/jvm-list-all-classes-loaded)
|
- [List All the Classes Loaded in the JVM](https://www.baeldung.com/jvm-list-all-classes-loaded)
|
||||||
- [Static Fields and Garbage Collection](https://www.baeldung.com/java-static-fields-gc)
|
- [Static Fields and Garbage Collection](https://www.baeldung.com/java-static-fields-gc)
|
||||||
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm) [[next -->]](/core-java-modules/core-java-jvm-3)
|
||||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
## Core Java JVM Cookbooks and Examples
|
||||||
|
|
||||||
|
This module contains articles about working with the Java Virtual Machine (JVM).
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
|
||||||
|
- [Difference Between Class.getResource() and ClassLoader.getResource()](https://www.baeldung.com/java-class-vs-classloader-getresource)
|
||||||
|
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm-2)
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-jvm-3</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-jvm-3</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.anonymousclass;
|
||||||
|
|
||||||
|
public class EmailSenderService implements SenderService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String callSender(Sender sender) {
|
||||||
|
return sender.send("Email Notification");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.anonymousclass;
|
||||||
|
|
||||||
|
public interface Sender {
|
||||||
|
|
||||||
|
String send(final String message);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.anonymousclass;
|
||||||
|
|
||||||
|
public interface SenderService {
|
||||||
|
|
||||||
|
String callSender(Sender sender);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.anonymousclass;
|
||||||
|
|
||||||
|
public class SmsSenderService implements SenderService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String callSender(Sender sender) {
|
||||||
|
return sender.send("SMS Notification");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.anonymousclass;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class AnonymousClassToLambdaIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPassingAnonymousClass_thenSuccess() {
|
||||||
|
final SenderService emailSenderService = new EmailSenderService();
|
||||||
|
|
||||||
|
final String emailNotif = emailSenderService.callSender(new Sender() {
|
||||||
|
@Override
|
||||||
|
public String send(String message) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(emailNotif, "Email Notification");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPassingLambdaExpression_thenSuccess() {
|
||||||
|
final SenderService smsSenderService = new SmsSenderService();
|
||||||
|
|
||||||
|
final String smsNotif = smsSenderService.callSender((String message) -> {
|
||||||
|
return message;
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(smsNotif, "SMS Notification");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
|
abstract class Animal {
|
||||||
|
|
||||||
|
protected final String type;
|
||||||
|
protected final String name;
|
||||||
|
|
||||||
|
protected Animal(String type, String name) {
|
||||||
|
this.type = type;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract String makeSound();
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
class AnimalDemo {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AnimalDemo.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<Cat> cats = new ArrayList<>();
|
||||||
|
cats.add(new Cat("Persian", "Bono"));
|
||||||
|
cats.add(new Cat("Egyptian", "Lulu"));
|
||||||
|
cats.add(new Cat("Siamese", "Ra"));
|
||||||
|
|
||||||
|
order(cats);
|
||||||
|
logger.info("Ordered cats: {}", cats);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends Animal & Comparable<T>> void order(List<T> list) {
|
||||||
|
list.sort(Comparable::compareTo);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
class Cat extends Animal implements Comparable<Cat> {
|
||||||
|
public Cat(String type, String name) {
|
||||||
|
super(type, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String makeSound() {
|
||||||
|
return "Meow";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Warning: Inconsistent with the equals method.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int compareTo(Cat cat) {
|
||||||
|
return this.getName().length() - cat.getName().length();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Cat{" + "type='" + type + '\'' + ", name='" + name + '\'' + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(type, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if(o == this) return true;
|
||||||
|
if(!(o instanceof Cat)) return false;
|
||||||
|
Cat cat = (Cat) o;
|
||||||
|
return type.equals(cat.type) && name.equals(cat.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class CollectionUtils {
|
||||||
|
|
||||||
|
private CollectionUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> void print(T item) {
|
||||||
|
System.out.println(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void swap(List<?> list, int src, int des) {
|
||||||
|
swapHelper(list, src, des);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <E> void swapHelper(List<E> list, int src, int des) {
|
||||||
|
list.set(src, list.set(des, list.get(src)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E> List<E> mergeTypeParameter(List<? extends E> listOne, List<? extends E> listTwo) {
|
||||||
|
return Stream.concat(listOne.stream(), listTwo.stream())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E> List<? extends E> mergeWildcard(List<? extends E> listOne, List<? extends E> listTwo) {
|
||||||
|
return Stream.concat(listOne.stream(), listTwo.stream())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sum(List<Number> numbers) {
|
||||||
|
return numbers.stream()
|
||||||
|
.mapToLong(Number::longValue)
|
||||||
|
.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends Number> long sumTypeParameter(List<T> numbers) {
|
||||||
|
return numbers.stream()
|
||||||
|
.mapToLong(Number::longValue)
|
||||||
|
.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long sumWildcard(List<? extends Number> numbers) {
|
||||||
|
return numbers.stream()
|
||||||
|
.mapToLong(Number::longValue)
|
||||||
|
.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addNumber(List<? super Integer> list, Integer number) {
|
||||||
|
list.add(number);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
class CollectionUtilsDemo {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(CollectionUtilsDemo.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CollectionUtils.print("Baeldung");
|
||||||
|
|
||||||
|
List<Number> numbers1 = new ArrayList<>();
|
||||||
|
numbers1.add(5);
|
||||||
|
numbers1.add(10L);
|
||||||
|
|
||||||
|
List<Number> numbers2 = new ArrayList<>();
|
||||||
|
numbers2.add(15f);
|
||||||
|
numbers2.add(20.0);
|
||||||
|
|
||||||
|
List<Number> numbersMerged = CollectionUtils.mergeTypeParameter(numbers1, numbers2);
|
||||||
|
logger.info("Merged numbers: {}", numbersMerged);
|
||||||
|
|
||||||
|
List<Number> numbers = new ArrayList<>();
|
||||||
|
numbers.add(5);
|
||||||
|
numbers.add(10L);
|
||||||
|
numbers.add(15f);
|
||||||
|
numbers.add(20.0);
|
||||||
|
|
||||||
|
logger.info("Sum: {}", CollectionUtils.sum(numbers));
|
||||||
|
logger.info("Sum (wildcard): {}", CollectionUtils.sumWildcard(numbers));
|
||||||
|
logger.info("Sum (type parameter): {}", CollectionUtils.sumTypeParameter(numbers));
|
||||||
|
|
||||||
|
List<Integer> integers = new ArrayList<>();
|
||||||
|
integers.add(5);
|
||||||
|
logger.info("Sum integers (wildcard): {}", CollectionUtils.sumWildcard(integers));
|
||||||
|
|
||||||
|
CollectionUtils.addNumber(numbers, 4);
|
||||||
|
CollectionUtils.addNumber(integers, 5);
|
||||||
|
|
||||||
|
logger.info("Before swap: {}", numbers);
|
||||||
|
CollectionUtils.swap(numbers, 0, 1);
|
||||||
|
logger.info("After swap: {}", numbers);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
|
public class Dog extends Animal {
|
||||||
|
|
||||||
|
public Dog(String type, String name) {
|
||||||
|
super(type, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String makeSound() {
|
||||||
|
return "Wuf";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.convertinttochar;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class ConvertCharToIntUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAChar_whenUsingGetNumericValue_thenExpectedNumericType() {
|
||||||
|
//char value
|
||||||
|
char c = '7';
|
||||||
|
// using getNumericValue
|
||||||
|
int n = Character.getNumericValue(c);
|
||||||
|
|
||||||
|
assertEquals(7, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAChar_whenSubtracting0_thenExpectedNumericType() {
|
||||||
|
//char value
|
||||||
|
char c = '7';
|
||||||
|
// subtract '0' from the char
|
||||||
|
int n = c - '0';
|
||||||
|
|
||||||
|
assertEquals(7, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAChar_whenUsingParseInt_thenExpectedNumericType() {
|
||||||
|
//char value
|
||||||
|
char c = '7';
|
||||||
|
// using parseInt
|
||||||
|
int n = Integer.parseInt(String.valueOf(c));
|
||||||
|
|
||||||
|
assertEquals(7, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAChar_whenCastingFromCharToInt_thenExpectedUnicodeRepresentation() {
|
||||||
|
//char value
|
||||||
|
char c = '7';
|
||||||
|
//cast to int
|
||||||
|
assertEquals(55, (int) c);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.convertinttochar;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class ConvertIntToCharUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnInt_whenAdding0_thenExpectedCharType() {
|
||||||
|
int num = 7;
|
||||||
|
//add '0' to convert int to char
|
||||||
|
char c = (char) ('0' + num);
|
||||||
|
|
||||||
|
assertEquals('7', c);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnInt_whenUsingForDigit_thenExpectedCharType() {
|
||||||
|
int num = 7;
|
||||||
|
// Convert using forDigit() method
|
||||||
|
char c = Character.forDigit(num, 10);
|
||||||
|
|
||||||
|
assertEquals('7', c);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnInt_whenUsingToString_thenExpectedCharType() {
|
||||||
|
int num = 7;
|
||||||
|
//convert int to char using toString()
|
||||||
|
char c = Integer.toString(num)
|
||||||
|
.charAt(0);
|
||||||
|
|
||||||
|
assertEquals('7', c);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.math.pascaltriangle;
|
||||||
|
|
||||||
|
public class PascalTriangle {
|
||||||
|
|
||||||
|
public static int factorial(int i) {
|
||||||
|
if (i == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return i * factorial(i - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printUseRecursion(int n) {
|
||||||
|
for (int i = 0; i <= n; i++) {
|
||||||
|
for (int j = 0; j <= n - i; j++) {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int k = 0; k <= i; k++) {
|
||||||
|
System.out.print(" " + factorial(i) / (factorial(i - k) * factorial(k)));
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printUseBinomialExpansion(int n) {
|
||||||
|
for (int line = 1; line <= n; line++) {
|
||||||
|
for (int j = 0; j <= n - line; j++) {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
int k = 1;
|
||||||
|
for (int i = 1; i <= line; i++) {
|
||||||
|
System.out.print(k + " ");
|
||||||
|
k = k * (line - i) / i;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = 5;
|
||||||
|
printUseRecursion(n);
|
||||||
|
// printUseBinomialExpansion(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -73,6 +73,7 @@
|
||||||
<module>core-java-jndi</module>
|
<module>core-java-jndi</module>
|
||||||
<module>core-java-jvm</module>
|
<module>core-java-jvm</module>
|
||||||
<module>core-java-jvm-2</module>
|
<module>core-java-jvm-2</module>
|
||||||
|
<module>core-java-jvm-3</module>
|
||||||
<module>core-java-lambdas</module>
|
<module>core-java-lambdas</module>
|
||||||
<module>core-java-lang</module>
|
<module>core-java-lang</module>
|
||||||
<module>core-java-lang-2</module>
|
<module>core-java-lang-2</module>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.dddcontexts.infrastructure</groupId>
|
<groupId>com.baeldung.dddcontexts.infrastructure</groupId>
|
||||||
<artifactId>infrastructure</artifactId>
|
<artifactId>ddd-contexts-infrastructure</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -18,17 +18,17 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.dddcontexts.shippingcontext</groupId>
|
<groupId>com.baeldung.dddcontexts.shippingcontext</groupId>
|
||||||
<artifactId>shippingcontext</artifactId>
|
<artifactId>ddd-contexts-shippingcontext</artifactId>
|
||||||
<version>${appmodules.version}</version>
|
<version>${appmodules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.dddcontexts.ordercontext</groupId>
|
<groupId>com.baeldung.dddcontexts.ordercontext</groupId>
|
||||||
<artifactId>ordercontext</artifactId>
|
<artifactId>ddd-contexts-ordercontext</artifactId>
|
||||||
<version>${appmodules.version}</version>
|
<version>${appmodules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
||||||
<artifactId>sharedkernel</artifactId>
|
<artifactId>ddd-contexts-sharedkernel</artifactId>
|
||||||
<version>${appmodules.version}</version>
|
<version>${appmodules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.dddcontexts.mainapp</groupId>
|
<groupId>com.baeldung.dddcontexts.mainapp</groupId>
|
||||||
<artifactId>mainapp</artifactId>
|
<artifactId>ddd-contexts-mainapp</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.dddcontexts.infrastructure</groupId>
|
<groupId>com.baeldung.dddcontexts.infrastructure</groupId>
|
||||||
<artifactId>infrastructure</artifactId>
|
<artifactId>ddd-contexts-infrastructure</artifactId>
|
||||||
<version>${appmodules.version}</version>
|
<version>${appmodules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.dddcontexts.ordercontext</groupId>
|
<groupId>com.baeldung.dddcontexts.ordercontext</groupId>
|
||||||
<artifactId>ordercontext</artifactId>
|
<artifactId>ddd-contexts-ordercontext</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
||||||
<artifactId>sharedkernel</artifactId>
|
<artifactId>ddd-contexts-sharedkernel</artifactId>
|
||||||
<version>${appmodules.version}</version>
|
<version>${appmodules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
||||||
<artifactId>sharedkernel</artifactId>
|
<artifactId>ddd-contexts-sharedkernel</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.dddcontexts.shippingcontext</groupId>
|
<groupId>com.baeldung.dddcontexts.shippingcontext</groupId>
|
||||||
<artifactId>shippingcontext</artifactId>
|
<artifactId>ddd-contexts-shippingcontext</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
<groupId>com.baeldung.dddcontexts.sharedkernel</groupId>
|
||||||
<artifactId>sharedkernel</artifactId>
|
<artifactId>ddd-contexts-sharedkernel</artifactId>
|
||||||
<version>${appmodules.version}</version>
|
<version>${appmodules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
|
@ -16,11 +16,11 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>sharedkernel</module>
|
<module>ddd-contexts-sharedkernel</module>
|
||||||
<module>infrastructure</module>
|
<module>ddd-contexts-infrastructure</module>
|
||||||
<module>shippingcontext</module>
|
<module>ddd-contexts-shippingcontext</module>
|
||||||
<module>ordercontext</module>
|
<module>ddd-contexts-ordercontext</module>
|
||||||
<module>mainapp</module>
|
<module>ddd-contexts-mainapp</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-spring-5</artifactId>
|
<artifactId>parent-spring-5</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-spring-5</relativePath>
|
<relativePath>../../parent-spring-5</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.handler;
|
||||||
|
|
||||||
|
import org.apache.hc.core5.http.ClassicHttpResponse;
|
||||||
|
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
||||||
|
|
||||||
|
public class CustomHttpClientResponseHandler implements HttpClientResponseHandler<ClassicHttpResponse> {
|
||||||
|
@Override
|
||||||
|
public ClassicHttpResponse handleResponse(ClassicHttpResponse response) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,114 +1,87 @@
|
||||||
package com.baeldung.httpclient.sec;
|
package com.baeldung.httpclient.sec;
|
||||||
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import com.baeldung.handler.CustomHttpClientResponseHandler;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.client.protocol.HttpClientContext;
|
|
||||||
import org.apache.http.cookie.ClientCookie;
|
|
||||||
import org.apache.http.impl.client.BasicCookieStore;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.apache.http.impl.cookie.BasicClientCookie;
|
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
|
||||||
import org.apache.http.protocol.HttpContext;
|
|
||||||
import com.baeldung.httpclient.ResponseUtil;
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||||
import org.junit.Before;
|
import org.apache.hc.client5.http.cookie.BasicCookieStore;
|
||||||
import org.junit.Test;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
import org.slf4j.Logger;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
|
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
|
||||||
|
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||||
|
|
||||||
|
import org.apache.hc.core5.http.protocol.BasicHttpContext;
|
||||||
|
import org.apache.hc.core5.http.protocol.HttpContext;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
public class HttpClientCookieLiveTest {
|
class HttpClientCookieLiveTest {
|
||||||
|
|
||||||
private CloseableHttpClient instance;
|
|
||||||
|
|
||||||
private CloseableHttpResponse response;
|
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(HttpClientCookieLiveTest.class);
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public final void before() {
|
|
||||||
instance = HttpClientBuilder.create().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public final void after() throws IllegalStateException, IOException {
|
|
||||||
ResponseUtil.closeResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCookiesOnARequest_thenCorrect() throws IOException {
|
final void whenSettingCookiesOnARequest_thenCorrect() throws IOException {
|
||||||
instance = HttpClientBuilder.create().build();
|
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
request.setHeader("Cookie", "JSESSIONID=1234");
|
request.setHeader("Cookie", "JSESSIONID=1234");
|
||||||
|
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = (CloseableHttpResponse) client.execute(request, new CustomHttpClientResponseHandler());) {
|
||||||
response = instance.execute(request);
|
assertThat(response.getCode(), equalTo(200));
|
||||||
|
}
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws IOException {
|
final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws IOException {
|
||||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
||||||
cookie.setDomain(".github.com");
|
cookie.setDomain(".github.com");
|
||||||
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
|
cookie.setAttribute("domain", "true");
|
||||||
|
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
|
|
||||||
DefaultHttpClient client = new DefaultHttpClient();
|
|
||||||
client.setCookieStore(cookieStore);
|
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("https://www.github.com");
|
final HttpGet request = new HttpGet("https://www.github.com");
|
||||||
|
try (CloseableHttpClient client = HttpClientBuilder.create()
|
||||||
|
.setDefaultCookieStore(cookieStore)
|
||||||
|
.build(); CloseableHttpResponse response = (CloseableHttpResponse) client.execute(request, new CustomHttpClientResponseHandler())) {
|
||||||
|
|
||||||
response = (CloseableHttpResponse) client.execute(request);
|
assertThat(response.getCode(), equalTo(200));
|
||||||
|
}
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws IOException {
|
final void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() throws IOException {
|
||||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
||||||
cookie.setDomain(".github.com");
|
cookie.setDomain(".github.com");
|
||||||
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
|
cookie.setAttribute("domain", "true");
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
instance = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
|
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
|
|
||||||
response = instance.execute(request);
|
try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
|
||||||
|
CloseableHttpResponse response = (CloseableHttpResponse) client.execute(request, new CustomHttpClientResponseHandler())) {
|
||||||
|
|
||||||
|
assertThat(response.getCode(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws IOException {
|
final void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() throws IOException {
|
||||||
final BasicCookieStore cookieStore = new BasicCookieStore();
|
final BasicCookieStore cookieStore = new BasicCookieStore();
|
||||||
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
|
||||||
cookie.setDomain(".github.com");
|
cookie.setDomain(".github.com");
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookieStore.addCookie(cookie);
|
cookieStore.addCookie(cookie);
|
||||||
instance = HttpClientBuilder.create().build();
|
|
||||||
|
|
||||||
final HttpGet request = new HttpGet("http://www.github.com");
|
final HttpGet request = new HttpGet("http://www.github.com");
|
||||||
|
|
||||||
final HttpContext localContext = new BasicHttpContext();
|
final HttpContext localContext = new BasicHttpContext();
|
||||||
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
|
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
|
||||||
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
|
// localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
|
||||||
response = instance.execute(request, localContext);
|
|
||||||
|
|
||||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
try (CloseableHttpClient client = HttpClientBuilder.create().build();
|
||||||
|
CloseableHttpResponse response = (CloseableHttpResponse) client.execute(request, localContext, new CustomHttpClientResponseHandler())) {
|
||||||
|
assertThat(response.getCode(), equalTo(200));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,17 @@
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.11.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.baeldung.jsonvaluegetter;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class JSONObjectValueGetter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get values associated with the provided key in the given JSONObject instance
|
||||||
|
*
|
||||||
|
* @param jsonObject JSONObject instance in which to search the key
|
||||||
|
* @param key Key we're interested in
|
||||||
|
*
|
||||||
|
* @return List of values associated with the given key, in the order of appearance.
|
||||||
|
* If the key is absent, empty list is returned.
|
||||||
|
*/
|
||||||
|
public List<String> getValuesInObject(JSONObject jsonObject, String key) {
|
||||||
|
List<String> accumulatedValues = new ArrayList<>();
|
||||||
|
for (String currentKey : jsonObject.keySet()) {
|
||||||
|
Object value = jsonObject.get(currentKey);
|
||||||
|
if (currentKey.equals(key)) {
|
||||||
|
accumulatedValues.add(value.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value instanceof JSONObject) {
|
||||||
|
accumulatedValues.addAll(getValuesInObject((JSONObject)value, key));
|
||||||
|
} else if (value instanceof JSONArray) {
|
||||||
|
accumulatedValues.addAll(getValuesInArray((JSONArray)value, key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulatedValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get values associated with the provided key in the given JSONArray instance
|
||||||
|
*
|
||||||
|
* @param jsonArray JSONArray instance in which to search the key
|
||||||
|
* @param key Key we're interested in
|
||||||
|
*
|
||||||
|
* @return List of values associated with the given key, in the order of appearance.
|
||||||
|
* If the key is absent, empty list is returned.
|
||||||
|
*/
|
||||||
|
public List<String> getValuesInArray(JSONArray jsonArray, String key) {
|
||||||
|
List<String> accumulatedValues = new ArrayList<>();
|
||||||
|
for (Object obj : jsonArray) {
|
||||||
|
if (obj instanceof JSONArray) {
|
||||||
|
accumulatedValues.addAll(getValuesInArray((JSONArray)obj, key));
|
||||||
|
} else if (obj instanceof JSONObject) {
|
||||||
|
accumulatedValues.addAll(getValuesInObject((JSONObject)obj, key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulatedValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Among all the values associated with the given key, get the N-th value
|
||||||
|
*
|
||||||
|
* @param jsonObject JSONObject instance in which to search the key
|
||||||
|
* @param key Key we're interested in
|
||||||
|
* @param N Index of the value to get
|
||||||
|
*
|
||||||
|
* @return N-th value associated with the key, or null if the key is absent or
|
||||||
|
* the number of values associated with the key is less than N
|
||||||
|
*/
|
||||||
|
public String getNthValue(JSONObject jsonObject, String key, int N) {
|
||||||
|
List<String> values = getValuesInObject(jsonObject, key);
|
||||||
|
return (values.size() >= N) ? values.get(N - 1) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count the number of values associated with the given key
|
||||||
|
*
|
||||||
|
* @param jsonObject JSONObject instance in which to count the key
|
||||||
|
* @param key Key we're interested in
|
||||||
|
*
|
||||||
|
* @return The number of values associated with the given key
|
||||||
|
*/
|
||||||
|
public int getCount(JSONObject jsonObject, String key) {
|
||||||
|
List<String> values = getValuesInObject(jsonObject, key);
|
||||||
|
return values.size();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.baeldung.jsonvaluegetter;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class JSONObjectValueGetterUnitTest {
|
||||||
|
|
||||||
|
private static JSONObject jsonObject;
|
||||||
|
private static JSONObjectValueGetter jsonObjectValueGetter = new JSONObjectValueGetter();
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void loadJsonContent() throws IOException {
|
||||||
|
InputStream inputStream = JSONObjectValueGetterUnitTest.class.getClassLoader().getResourceAsStream("employee.json");
|
||||||
|
String jsonString = IOUtils.toString(inputStream, "UTF-8");
|
||||||
|
jsonObject = new JSONObject(jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getValueDirectly() {
|
||||||
|
JSONArray family = jsonObject.getJSONArray("family");
|
||||||
|
JSONObject sonObject = family.getJSONObject(1);
|
||||||
|
JSONObject sonData = sonObject.getJSONObject("son");
|
||||||
|
String sonName = sonData.getString("name");
|
||||||
|
Assertions.assertEquals(sonName, "Peter");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAllAssociatedValuesRecursively() {
|
||||||
|
List<String> values = jsonObjectValueGetter.getValuesInObject(jsonObject, "son");
|
||||||
|
Assertions.assertEquals(values.size(), 1);
|
||||||
|
|
||||||
|
String sonString = values.get(0);
|
||||||
|
Assertions.assertTrue(sonString.contains("Peter"));
|
||||||
|
Assertions.assertTrue(sonString.contains("Schoolboy"));
|
||||||
|
Assertions.assertTrue(sonString.contains("11"));
|
||||||
|
|
||||||
|
values = jsonObjectValueGetter.getValuesInObject(jsonObject, "name");
|
||||||
|
Assertions.assertEquals(values.size(), 3);
|
||||||
|
|
||||||
|
Assertions.assertEquals(values.get(0), "Bob");
|
||||||
|
Assertions.assertEquals(values.get(1), "Alice");
|
||||||
|
Assertions.assertEquals(values.get(2), "Peter");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getNthValueRecursively() {
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getNthValue(jsonObject, "name", 1), "Bob");
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getNthValue(jsonObject, "name", 2), "Alice");
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getNthValue(jsonObject, "name", 3), "Peter");
|
||||||
|
Assertions.assertNull(jsonObjectValueGetter.getNthValue(jsonObject, "nonExistingKey", 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getCountRecursively() {
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "name"), 3);
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "age"), 3);
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "occupation"), 1);
|
||||||
|
Assertions.assertEquals(jsonObjectValueGetter.getCount(jsonObject, "nonExistingKey"), 0);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"name" : "Bob",
|
||||||
|
"profession" : "Software engineer",
|
||||||
|
"department" : "Research",
|
||||||
|
"age" : 40,
|
||||||
|
"family" : [
|
||||||
|
{
|
||||||
|
"wife" : {
|
||||||
|
"name" : "Alice",
|
||||||
|
"profession" : "Doctor",
|
||||||
|
"age" : 38
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"son" : {
|
||||||
|
"name" : "Peter",
|
||||||
|
"occupation" : "Schoolboy",
|
||||||
|
"age" : 11
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"performance" : [
|
||||||
|
{
|
||||||
|
"2020" : 4.5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2021" : 4.8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.daomodule</groupId>
|
<groupId>com.baeldung.daomodule</groupId>
|
||||||
<artifactId>daomodule</artifactId>
|
<artifactId>maven-daomodule</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.entitymodule</groupId>
|
<groupId>com.baeldung.entitymodule</groupId>
|
||||||
<artifactId>entitymodule</artifactId>
|
<artifactId>maven-entitymodule</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.mainappmodule</groupId>
|
<groupId>com.baeldung.mainappmodule</groupId>
|
||||||
<artifactId>mainappmodule</artifactId>
|
<artifactId>maven-mainappmodule</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -17,17 +17,17 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.entitymodule</groupId>
|
<groupId>com.baeldung.entitymodule</groupId>
|
||||||
<artifactId>entitymodule</artifactId>
|
<artifactId>maven-entitymodule</artifactId>
|
||||||
<version>${entitymodule.version}</version>
|
<version>${entitymodule.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.daomodule</groupId>
|
<groupId>com.baeldung.daomodule</groupId>
|
||||||
<artifactId>daomodule</artifactId>
|
<artifactId>maven-daomodule</artifactId>
|
||||||
<version>${daomodule.version}</version>
|
<version>${daomodule.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung.userdaomodule</groupId>
|
<groupId>com.baeldung.userdaomodule</groupId>
|
||||||
<artifactId>userdaomodule</artifactId>
|
<artifactId>maven-userdaomodule</artifactId>
|
||||||
<version>${userdaomodule.version}</version>
|
<version>${userdaomodule.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.userdaomodule</groupId>
|
<groupId>com.baeldung.userdaomodule</groupId>
|
||||||
<artifactId>userdaomodule</artifactId>
|
<artifactId>maven-userdaomodule</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
|
@ -17,10 +17,10 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>entitymodule</module>
|
<module>maven-entitymodule</module>
|
||||||
<module>daomodule</module>
|
<module>maven-daomodule</module>
|
||||||
<module>userdaomodule</module>
|
<module>maven-userdaomodule</module>
|
||||||
<module>mainappmodule</module>
|
<module>maven-mainappmodule</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -13,8 +13,8 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>project-a</module>
|
<module>version-collision-project-a</module>
|
||||||
<module>project-b</module>
|
<module>version-collision-project-b</module>
|
||||||
<module>project-collision</module>
|
<module>project-collision</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>project-a</artifactId>
|
<artifactId>version-collision-project-a</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<!-- uncomment to exclude guava transitive artifact from module -->
|
<!-- uncomment to exclude guava transitive artifact from module -->
|
||||||
<!-- <exclusions> -->
|
<!-- <exclusions> -->
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>project-b</artifactId>
|
<artifactId>version-collision-project-b</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>project-a</artifactId>
|
<artifactId>version-collision-project-a</artifactId>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>version-collision</artifactId>
|
<artifactId>version-collision</artifactId>
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>project-b</artifactId>
|
<artifactId>version-collision-project-b</artifactId>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>version-collision</artifactId>
|
<artifactId>version-collision</artifactId>
|
|
@ -13,8 +13,8 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>child-a</module>
|
<module>version-overriding-child-a</module>
|
||||||
<module>child-b</module>
|
<module>version-overriding-child-b</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>child-a</artifactId>
|
<artifactId>version-overriding-child-a</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
|
@ -3,7 +3,7 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>child-b</artifactId>
|
<artifactId>version-overriding-child-b</artifactId>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>version-overriding-plugins</artifactId>
|
<artifactId>version-overriding-plugins</artifactId>
|
|
@ -24,7 +24,7 @@
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring.version>5.3.23</spring.version>
|
<spring.version>5.3.24</spring.version>
|
||||||
<spring-security.version>5.7.3</spring-security.version>
|
<spring-security.version>5.7.3</spring-security.version>
|
||||||
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
### Relevant Articles:
|
|
@ -0,0 +1,121 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>blaze-persistence</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>blaze-persistence</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.4.0</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<blaze-persistence.version>1.6.8</blaze-persistence.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-bom</artifactId>
|
||||||
|
<version>${blaze-persistence.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Core dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-core-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-core-impl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-integration-hibernate-5.4</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Entity View dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-entity-view-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-entity-view-impl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-entity-view-processor</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring integration dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-integration-entity-view-spring</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.blazebit</groupId>
|
||||||
|
<artifactId>blaze-persistence-integration-spring-data-2.4</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-orm</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Test dependencies -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.model.Person;
|
||||||
|
import com.baeldung.model.Post;
|
||||||
|
import com.baeldung.repository.PersonRepository;
|
||||||
|
import com.baeldung.repository.PostRepository;
|
||||||
|
import com.baeldung.repository.PostViewRepository;
|
||||||
|
import com.baeldung.view.PostWithAuthorView;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class BlazePersistenceApplication implements CommandLineRunner {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(BlazePersistenceApplication.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PersonRepository personRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PostRepository postRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PostViewRepository postViewRepository;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(BlazePersistenceApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) {
|
||||||
|
|
||||||
|
logger.info("All Posts:");
|
||||||
|
Iterable<Post> posts = postRepository.findAll();
|
||||||
|
posts.forEach(p -> logger.info(String.valueOf(p)));
|
||||||
|
|
||||||
|
logger.info("Posts with title 'Spring' or author 'Peter':");
|
||||||
|
Iterable<PostWithAuthorView> postsFiltered = postRepository.findBy("Spring", "Peter");
|
||||||
|
postsFiltered.forEach(p -> logger.info(String.valueOf(p)));
|
||||||
|
|
||||||
|
logger.info("Find all post with author view:");
|
||||||
|
Iterable<PostWithAuthorView> postsView = postViewRepository.findAll();
|
||||||
|
postsView.forEach(p -> logger.info(String.valueOf(p)));
|
||||||
|
|
||||||
|
logger.info("Person with at least two posts:");
|
||||||
|
Iterable<Person> personIterable = personRepository.find();
|
||||||
|
personIterable.forEach(p -> logger.info(String.valueOf(p)));
|
||||||
|
|
||||||
|
logger.info("All Persons:");
|
||||||
|
Iterable<Person> personIterableAll = personRepository.findAll();
|
||||||
|
personIterableAll.forEach(p -> logger.info(String.valueOf(p)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.config;
|
||||||
|
|
||||||
|
import com.blazebit.persistence.Criteria;
|
||||||
|
import com.blazebit.persistence.CriteriaBuilderFactory;
|
||||||
|
import com.blazebit.persistence.integration.view.spring.EnableEntityViews;
|
||||||
|
import com.blazebit.persistence.spi.CriteriaBuilderConfiguration;
|
||||||
|
import com.blazebit.persistence.spring.data.repository.config.EnableBlazeRepositories;
|
||||||
|
import com.blazebit.persistence.view.EntityViewManager;
|
||||||
|
import com.blazebit.persistence.view.spi.EntityViewConfiguration;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableEntityViews(basePackages = {"com.baeldung.view"})
|
||||||
|
@EnableBlazeRepositories(basePackages = "com.baeldung.repository")
|
||||||
|
public class BlazePersistenceConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EntityManagerFactory entityManagerFactory;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CriteriaBuilderFactory createCriteriaBuilderFactory() {
|
||||||
|
CriteriaBuilderConfiguration config = Criteria.getDefault();
|
||||||
|
return config.createCriteriaBuilderFactory(entityManagerFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public EntityViewManager createEntityViewManager(CriteriaBuilderFactory criteriaBuilderFactory,
|
||||||
|
EntityViewConfiguration entityViewConfiguration) {
|
||||||
|
return entityViewConfiguration.createEntityViewManager(criteriaBuilderFactory);
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue