Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
ba62e71e2a
129
activejdbc/pom.xml
Normal file
129
activejdbc/pom.xml
Normal file
@ -0,0 +1,129 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>activejdbc</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<activejdbc.version>1.4.13</activejdbc.version>
|
||||
<environments>development.test,development</environments>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc-instrumentation</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>instrument</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>db-migrator-maven-plugin</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<configuration>
|
||||
<configFile>${project.basedir}/src/main/resources/database.properties</configFile>
|
||||
<environments>${environments}</environments>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.34</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.18.1</version>
|
||||
<configuration>
|
||||
<reportFormat>brief</reportFormat>
|
||||
<trimStackTrace>true</trimStackTrace>
|
||||
<useFile>false</useFile>
|
||||
<includes>
|
||||
<include>**/*Spec*.java</include>
|
||||
<include>**/*Test*.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/helpers/*</exclude>
|
||||
<exclude>**/*$*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>opensymphony</groupId>
|
||||
<artifactId>oscache</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.34</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>snapshots1</id>
|
||||
<name>JavaLite Snapshots1</name>
|
||||
<url>http://repo.javalite.io/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
<checksumPolicy>warn</checksumPolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>snapshots2</id>
|
||||
<name>JavaLite Snapshots2</name>
|
||||
<url>http://repo.javalite.io/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
<checksumPolicy>warn</checksumPolicy>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
</project>
|
61
activejdbc/src/main/java/com/baeldung/ActiveJDBCApp.java
Normal file
61
activejdbc/src/main/java/com/baeldung/ActiveJDBCApp.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.baeldung;
|
||||
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
import com.baeldung.model.Role;
|
||||
import org.javalite.activejdbc.Base;
|
||||
import org.javalite.activejdbc.LazyList;
|
||||
import org.javalite.activejdbc.Model;
|
||||
|
||||
public class ActiveJDBCApp
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
try {
|
||||
Base.open();
|
||||
ActiveJDBCApp app = new ActiveJDBCApp();
|
||||
app.create();
|
||||
app.update();
|
||||
app.delete();
|
||||
app.deleteCascade();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
Base.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected void create() {
|
||||
Employee employee = new Employee("Hugo","C","M","BN");
|
||||
employee.saveIt();
|
||||
employee.add(new Role("Java Developer","BN"));
|
||||
LazyList<Model> all = Employee.findAll();
|
||||
System.out.println(all.size());
|
||||
}
|
||||
|
||||
protected void update() {
|
||||
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||
employee.set("last_namea","Choi").saveIt();
|
||||
employee = Employee.findFirst("last_name = ?","Choi");
|
||||
System.out.println(employee.getString("first_name") + " " + employee.getString("last_name"));
|
||||
}
|
||||
|
||||
protected void delete() {
|
||||
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||
employee.delete();
|
||||
employee = Employee.findFirst("last_name = ?","Choi");
|
||||
if(null == employee){
|
||||
System.out.println("No such Employee found!");
|
||||
}
|
||||
}
|
||||
|
||||
protected void deleteCascade() {
|
||||
create();
|
||||
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||
employee.deleteCascade();
|
||||
employee = Employee.findFirst("last_name = ?","C");
|
||||
if(null == employee){
|
||||
System.out.println("No such Employee found!");
|
||||
}
|
||||
}
|
||||
}
|
19
activejdbc/src/main/java/com/baeldung/model/Employee.java
Normal file
19
activejdbc/src/main/java/com/baeldung/model/Employee.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
|
||||
import org.javalite.activejdbc.Model;
|
||||
|
||||
public class Employee extends Model {
|
||||
|
||||
public Employee(){
|
||||
|
||||
}
|
||||
|
||||
public Employee(String firstName, String lastName, String gender, String createdBy) {
|
||||
set("first_name1",firstName);
|
||||
set("last_name",lastName);
|
||||
set("gender",gender);
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
}
|
18
activejdbc/src/main/java/com/baeldung/model/Role.java
Normal file
18
activejdbc/src/main/java/com/baeldung/model/Role.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
|
||||
import org.javalite.activejdbc.Model;
|
||||
import org.javalite.activejdbc.annotations.Table;
|
||||
|
||||
@Table("EMP_ROLES")
|
||||
public class Role extends Model {
|
||||
|
||||
public Role(){
|
||||
|
||||
}
|
||||
|
||||
public Role(String role,String createdBy){
|
||||
set("role_name",role);
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
}
|
25
activejdbc/src/main/migration/_create_tables.sql
Normal file
25
activejdbc/src/main/migration/_create_tables.sql
Normal file
@ -0,0 +1,25 @@
|
||||
# noinspection SqlNoDataSourceInspectionForFile
|
||||
|
||||
create table organisation.employees
|
||||
(
|
||||
id int not null auto_increment
|
||||
primary key,
|
||||
first_name varchar(100) not null,
|
||||
last_name varchar(100) not null,
|
||||
gender varchar(1) not null,
|
||||
created_at datetime not null,
|
||||
updated_at datetime null,
|
||||
created_by varchar(100) not null,
|
||||
updated_by varchar(100) null
|
||||
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
|
||||
|
||||
create table organisation.emp_roles
|
||||
(
|
||||
id int not null auto_increment primary key,
|
||||
employee_id int not null,
|
||||
role_name varchar(100) not null,
|
||||
created_at datetime not null,
|
||||
updated_at datetime null,
|
||||
created_by varchar(100) not null,
|
||||
updated_by varchar(100) null
|
||||
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
|
10
activejdbc/src/main/resources/database.properties
Normal file
10
activejdbc/src/main/resources/database.properties
Normal file
@ -0,0 +1,10 @@
|
||||
development.driver=com.mysql.jdbc.Driver
|
||||
development.username=root
|
||||
development.password=123456
|
||||
development.url=jdbc:mysql://localhost/organisation
|
||||
|
||||
|
||||
development.test.driver=com.mysql.jdbc.Driver
|
||||
development.test.username=root
|
||||
development.test.password=123456
|
||||
development.test.url=jdbc:mysql://localhost/organisation_test
|
51
activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java
Normal file
51
activejdbc/src/test/java/com/baeldung/ActiveJDBCAppTest.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
import com.baeldung.model.Role;
|
||||
import org.javalite.activejdbc.test.DBSpec;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ActiveJDBCAppTest extends DBSpec
|
||||
{
|
||||
@Test
|
||||
public void ifEmployeeCreated_thenIsValid() {
|
||||
Employee employee = new Employee("B", "N", "M", "BN");
|
||||
the(employee).shouldBe("valid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifEmployeeCreatedWithRoles_thenShouldPersist() {
|
||||
Employee employee = new Employee("B", "N", "M", "BN");
|
||||
employee.saveIt();
|
||||
employee.add(new Role("Java Developer","BN"));
|
||||
employee.add(new Role("Lead Java Developer","BN"));
|
||||
a(Role.count()).shouldBeEqual(2);
|
||||
List<Role> roles = employee.getAll(Role.class).orderBy("created_at");
|
||||
the(roles.get(0).getRoleName()).shouldBeEqual("Java Developer");
|
||||
the(roles.get(1).getRoleName()).shouldBeEqual("Lead Java Developer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifEmployeeCreatedWithRoles_whenNameUpdated_thenShouldShowNewName() {
|
||||
Employee employee = new Employee("Binesh", "N", "M", "BN");
|
||||
employee.saveIt();
|
||||
employee.add(new Role("Java Developer","BN"));
|
||||
employee.add(new Role("Lead Java Developer","BN"));
|
||||
employee = Employee.findFirst("first_name = ?", "Binesh");
|
||||
employee.set("last_name","Narayanan").saveIt();
|
||||
Employee updated = Employee.findFirst("first_name = ?", "Binesh");
|
||||
the(updated.getLastName()).shouldBeEqual("Narayanan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifEmployeeCreatedWithRoles_whenDeleted_thenShouldNotBeFound() {
|
||||
Employee employee = new Employee("Binesh", "N", "M", "BN");
|
||||
employee.saveIt();
|
||||
employee.add(new Role("Java Developer","BN"));
|
||||
employee.delete();
|
||||
employee = Employee.findFirst("first_name = ?", "Binesh");
|
||||
the(employee).shouldBeNull();
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class BFSMazeSolver {
|
||||
private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
|
||||
|
||||
public List<Coordinate> solve(Maze maze) {
|
||||
LinkedList<Coordinate> nextToVisit = new LinkedList<>();
|
||||
Coordinate start = maze.getEntry();
|
||||
nextToVisit.add(start);
|
||||
|
||||
while (!nextToVisit.isEmpty()) {
|
||||
Coordinate cur = nextToVisit.remove();
|
||||
|
||||
if (!maze.isValidLocation(cur.getX(), cur.getY()) || maze.isExplored(cur.getX(), cur.getY())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (maze.isWall(cur.getX(), cur.getY())) {
|
||||
maze.setVisited(cur.getX(), cur.getY(), true);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (maze.isExit(cur.getX(), cur.getY())) {
|
||||
return backtrackPath(cur);
|
||||
}
|
||||
|
||||
for (int[] direction : DIRECTIONS) {
|
||||
Coordinate coordinate = new Coordinate(cur.getX() + direction[0], cur.getY() + direction[1], cur);
|
||||
nextToVisit.add(coordinate);
|
||||
maze.setVisited(cur.getX(), cur.getY(), true);
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private List<Coordinate> backtrackPath(Coordinate cur) {
|
||||
List<Coordinate> path = new ArrayList<>();
|
||||
Coordinate iter = cur;
|
||||
|
||||
while (iter != null) {
|
||||
path.add(iter);
|
||||
iter = iter.parent;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
public class Coordinate {
|
||||
int x;
|
||||
int y;
|
||||
Coordinate parent;
|
||||
|
||||
public Coordinate(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.parent = null;
|
||||
}
|
||||
|
||||
public Coordinate(int x, int y, Coordinate parent) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
Coordinate getParent() {
|
||||
return parent;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class DFSMazeSolver {
|
||||
private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
|
||||
|
||||
public List<Coordinate> solve(Maze maze) {
|
||||
List<Coordinate> path = new ArrayList<>();
|
||||
if (explore(maze, maze.getEntry()
|
||||
.getX(),
|
||||
maze.getEntry()
|
||||
.getY(),
|
||||
path)) {
|
||||
return path;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private boolean explore(Maze maze, int row, int col, List<Coordinate> path) {
|
||||
if (!maze.isValidLocation(row, col) || maze.isWall(row, col) || maze.isExplored(row, col)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
path.add(new Coordinate(row, col));
|
||||
maze.setVisited(row, col, true);
|
||||
|
||||
if (maze.isExit(row, col)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int[] direction : DIRECTIONS) {
|
||||
Coordinate coordinate = getNextCoordinate(row, col, direction[0], direction[1]);
|
||||
if (explore(maze, coordinate.getX(), coordinate.getY(), path)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
path.remove(path.size() - 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
private Coordinate getNextCoordinate(int row, int col, int i, int j) {
|
||||
return new Coordinate(row + i, col + j);
|
||||
}
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Maze {
|
||||
private static final int ROAD = 0;
|
||||
private static final int WALL = 1;
|
||||
private static final int START = 2;
|
||||
private static final int EXIT = 3;
|
||||
private static final int PATH = 4;
|
||||
|
||||
private int[][] maze;
|
||||
private boolean[][] visited;
|
||||
private Coordinate start;
|
||||
private Coordinate end;
|
||||
|
||||
public Maze(File maze) throws FileNotFoundException {
|
||||
String fileText = "";
|
||||
try (Scanner input = new Scanner(maze)) {
|
||||
while (input.hasNextLine()) {
|
||||
fileText += input.nextLine() + "\n";
|
||||
}
|
||||
}
|
||||
initializeMaze(fileText);
|
||||
}
|
||||
|
||||
private void initializeMaze(String text) {
|
||||
if (text == null || (text = text.trim()).length() == 0) {
|
||||
throw new IllegalArgumentException("empty lines data");
|
||||
}
|
||||
|
||||
String[] lines = text.split("[\r]?\n");
|
||||
maze = new int[lines.length][lines[0].length()];
|
||||
visited = new boolean[lines.length][lines[0].length()];
|
||||
|
||||
for (int row = 0; row < getHeight(); row++) {
|
||||
if (lines[row].length() != getWidth()) {
|
||||
throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")");
|
||||
}
|
||||
|
||||
for (int col = 0; col < getWidth(); col++) {
|
||||
if (lines[row].charAt(col) == '#')
|
||||
maze[row][col] = WALL;
|
||||
else if (lines[row].charAt(col) == 'S') {
|
||||
maze[row][col] = START;
|
||||
start = new Coordinate(row, col);
|
||||
} else if (lines[row].charAt(col) == 'E') {
|
||||
maze[row][col] = EXIT;
|
||||
end = new Coordinate(row, col);
|
||||
} else
|
||||
maze[row][col] = ROAD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return maze.length;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return maze[0].length;
|
||||
}
|
||||
|
||||
public Coordinate getEntry() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public Coordinate getExit() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public boolean isExit(int x, int y) {
|
||||
return x == end.getX() && y == end.getY();
|
||||
}
|
||||
|
||||
public boolean isStart(int x, int y) {
|
||||
return x == start.getX() && y == start.getY();
|
||||
}
|
||||
|
||||
public boolean isExplored(int row, int col) {
|
||||
return visited[row][col];
|
||||
}
|
||||
|
||||
public boolean isWall(int row, int col) {
|
||||
return maze[row][col] == WALL;
|
||||
}
|
||||
|
||||
public void setVisited(int row, int col, boolean value) {
|
||||
visited[row][col] = value;
|
||||
}
|
||||
|
||||
public boolean isValidLocation(int row, int col) {
|
||||
if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void printPath(List<Coordinate> path) {
|
||||
int[][] tempMaze = Arrays.stream(maze)
|
||||
.map(int[]::clone)
|
||||
.toArray(int[][]::new);
|
||||
for (Coordinate coordinate : path) {
|
||||
if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) {
|
||||
continue;
|
||||
}
|
||||
tempMaze[coordinate.getX()][coordinate.getY()] = PATH;
|
||||
}
|
||||
System.out.println(toString(tempMaze));
|
||||
}
|
||||
|
||||
public String toString(int[][] maze) {
|
||||
StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1));
|
||||
for (int row = 0; row < getHeight(); row++) {
|
||||
for (int col = 0; col < getWidth(); col++) {
|
||||
if (maze[row][col] == ROAD) {
|
||||
result.append(' ');
|
||||
} else if (maze[row][col] == WALL) {
|
||||
result.append('#');
|
||||
} else if (maze[row][col] == START) {
|
||||
result.append('S');
|
||||
} else if (maze[row][col] == EXIT) {
|
||||
result.append('E');
|
||||
} else {
|
||||
result.append('.');
|
||||
}
|
||||
}
|
||||
result.append('\n');
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
for (int i = 0; i < visited.length; i++)
|
||||
Arrays.fill(visited[i], false);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class MazeDriver {
|
||||
public static void main(String[] args) throws Exception {
|
||||
File maze1 = new File("src/main/resources/maze/maze1.txt");
|
||||
File maze2 = new File("src/main/resources/maze/maze2.txt");
|
||||
|
||||
execute(maze1);
|
||||
execute(maze2);
|
||||
}
|
||||
|
||||
private static void execute(File file) throws Exception {
|
||||
Maze maze = new Maze(file);
|
||||
dfs(maze);
|
||||
bfs(maze);
|
||||
}
|
||||
|
||||
private static void bfs(Maze maze) {
|
||||
BFSMazeSolver bfs = new BFSMazeSolver();
|
||||
List<Coordinate> path = bfs.solve(maze);
|
||||
maze.printPath(path);
|
||||
maze.reset();
|
||||
}
|
||||
|
||||
private static void dfs(Maze maze) {
|
||||
DFSMazeSolver dfs = new DFSMazeSolver();
|
||||
List<Coordinate> path = dfs.solve(maze);
|
||||
maze.printPath(path);
|
||||
maze.reset();
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BacktrackingAlgorithm {
|
||||
|
||||
private static final int BOARD_SIZE = 9;
|
||||
private static final int SUBSECTION_SIZE = 3;
|
||||
private static final int BOARD_START_INDEX = 0;
|
||||
|
||||
private static final int NO_VALUE = 0;
|
||||
private static final int MIN_VALUE = 1;
|
||||
private static final int MAX_VALUE = 9;
|
||||
|
||||
private static int[][] board = {
|
||||
{8, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 3, 6, 0, 0, 0, 0, 0},
|
||||
{0, 7, 0, 0, 9, 0, 2, 0, 0},
|
||||
{0, 5, 0, 0, 0, 7, 0, 0, 0},
|
||||
{0, 0, 0, 0, 4, 5, 7, 0, 0},
|
||||
{0, 0, 0, 1, 0, 0, 0, 3, 0},
|
||||
{0, 0, 1, 0, 0, 0, 0, 6, 8},
|
||||
{0, 0, 8, 5, 0, 0, 0, 1, 0},
|
||||
{0, 9, 0, 0, 0, 0, 4, 0, 0}
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
BacktrackingAlgorithm solver = new BacktrackingAlgorithm();
|
||||
solver.solve(board);
|
||||
solver.printBoard();
|
||||
}
|
||||
|
||||
private void printBoard() {
|
||||
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
|
||||
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
|
||||
System.out.print(board[row][column] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean solve(int[][] board) {
|
||||
for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) {
|
||||
for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) {
|
||||
if (board[r][c] == NO_VALUE) {
|
||||
for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
|
||||
board[r][c] = k;
|
||||
if (isValid(board, r, c) && solve(board)) {
|
||||
return true;
|
||||
}
|
||||
board[r][c] = NO_VALUE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isValid(int[][] board, int r, int c) {
|
||||
return rowConstraint(board, r) &&
|
||||
columnConstraint(board, c) &&
|
||||
subsectionConstraint(board, r, c);
|
||||
}
|
||||
|
||||
private boolean subsectionConstraint(int[][] board, int r, int c) {
|
||||
boolean[] constraint = new boolean[BOARD_SIZE];
|
||||
int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE;
|
||||
int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
|
||||
|
||||
int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE;
|
||||
int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
|
||||
|
||||
for (int i = subsectionRowStart; i < subsectionRowEnd; i++) {
|
||||
for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) {
|
||||
if (!checkConstraint(board, i, constraint, j)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean columnConstraint(int[][] board, int c) {
|
||||
boolean[] constraint = new boolean[BOARD_SIZE];
|
||||
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
|
||||
.allMatch(i -> checkConstraint(board, i, constraint, c));
|
||||
}
|
||||
|
||||
private boolean rowConstraint(int[][] board, int r) {
|
||||
boolean[] constraint = new boolean[BOARD_SIZE];
|
||||
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
|
||||
.allMatch(i -> checkConstraint(board, r, constraint, i));
|
||||
}
|
||||
|
||||
private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) {
|
||||
if (board[r][c] != NO_VALUE) {
|
||||
if (!constraint[board[r][c] - 1]) {
|
||||
constraint[board[r][c] - 1] = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
class ColumnNode extends DancingNode {
|
||||
int size;
|
||||
String name;
|
||||
|
||||
ColumnNode(String n) {
|
||||
super();
|
||||
size = 0;
|
||||
name = n;
|
||||
C = this;
|
||||
}
|
||||
|
||||
void cover() {
|
||||
unlinkLR();
|
||||
for (DancingNode i = this.D; i != this; i = i.D) {
|
||||
for (DancingNode j = i.R; j != i; j = j.R) {
|
||||
j.unlinkUD();
|
||||
j.C.size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void uncover() {
|
||||
for (DancingNode i = this.U; i != this; i = i.U) {
|
||||
for (DancingNode j = i.L; j != i; j = j.L) {
|
||||
j.C.size++;
|
||||
j.relinkUD();
|
||||
}
|
||||
}
|
||||
relinkLR();
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class DancingLinks {
|
||||
|
||||
private ColumnNode header;
|
||||
private List<DancingNode> answer;
|
||||
|
||||
private void search(int k) {
|
||||
if (header.R == header) {
|
||||
handleSolution(answer);
|
||||
} else {
|
||||
ColumnNode c = selectColumnNodeHeuristic();
|
||||
c.cover();
|
||||
|
||||
for (DancingNode r = c.D; r != c; r = r.D) {
|
||||
answer.add(r);
|
||||
|
||||
for (DancingNode j = r.R; j != r; j = j.R) {
|
||||
j.C.cover();
|
||||
}
|
||||
|
||||
search(k + 1);
|
||||
|
||||
r = answer.remove(answer.size() - 1);
|
||||
c = r.C;
|
||||
|
||||
for (DancingNode j = r.L; j != r; j = j.L) {
|
||||
j.C.uncover();
|
||||
}
|
||||
}
|
||||
c.uncover();
|
||||
}
|
||||
}
|
||||
|
||||
private ColumnNode selectColumnNodeHeuristic() {
|
||||
int min = Integer.MAX_VALUE;
|
||||
ColumnNode ret = null;
|
||||
for (ColumnNode c = (ColumnNode) header.R; c != header; c = (ColumnNode) c.R) {
|
||||
if (c.size < min) {
|
||||
min = c.size;
|
||||
ret = c;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private ColumnNode makeDLXBoard(boolean[][] grid) {
|
||||
final int COLS = grid[0].length;
|
||||
|
||||
ColumnNode headerNode = new ColumnNode("header");
|
||||
List<ColumnNode> columnNodes = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < COLS; i++) {
|
||||
ColumnNode n = new ColumnNode(Integer.toString(i));
|
||||
columnNodes.add(n);
|
||||
headerNode = (ColumnNode) headerNode.hookRight(n);
|
||||
}
|
||||
headerNode = headerNode.R.C;
|
||||
|
||||
for (boolean[] aGrid : grid) {
|
||||
DancingNode prev = null;
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
if (aGrid[j]) {
|
||||
ColumnNode col = columnNodes.get(j);
|
||||
DancingNode newNode = new DancingNode(col);
|
||||
if (prev == null)
|
||||
prev = newNode;
|
||||
col.U.hookDown(newNode);
|
||||
prev = prev.hookRight(newNode);
|
||||
col.size++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
headerNode.size = COLS;
|
||||
|
||||
return headerNode;
|
||||
}
|
||||
|
||||
DancingLinks(boolean[][] cover) {
|
||||
header = makeDLXBoard(cover);
|
||||
}
|
||||
|
||||
public void runSolver() {
|
||||
answer = new LinkedList<>();
|
||||
search(0);
|
||||
}
|
||||
|
||||
private void handleSolution(List<DancingNode> answer) {
|
||||
int[][] result = parseBoard(answer);
|
||||
printSolution(result);
|
||||
}
|
||||
|
||||
private int size = 9;
|
||||
|
||||
private int[][] parseBoard(List<DancingNode> answer) {
|
||||
int[][] result = new int[size][size];
|
||||
for (DancingNode n : answer) {
|
||||
DancingNode rcNode = n;
|
||||
int min = Integer.parseInt(rcNode.C.name);
|
||||
for (DancingNode tmp = n.R; tmp != n; tmp = tmp.R) {
|
||||
int val = Integer.parseInt(tmp.C.name);
|
||||
if (val < min) {
|
||||
min = val;
|
||||
rcNode = tmp;
|
||||
}
|
||||
}
|
||||
int ans1 = Integer.parseInt(rcNode.C.name);
|
||||
int ans2 = Integer.parseInt(rcNode.R.C.name);
|
||||
int r = ans1 / size;
|
||||
int c = ans1 % size;
|
||||
int num = (ans2 % size) + 1;
|
||||
result[r][c] = num;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void printSolution(int[][] result) {
|
||||
int size = result.length;
|
||||
for (int[] aResult : result) {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
for (int j = 0; j < size; j++) {
|
||||
ret.append(aResult[j]).append(" ");
|
||||
}
|
||||
System.out.println(ret);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class DancingLinksAlgorithm {
|
||||
private static final int BOARD_SIZE = 9;
|
||||
private static final int SUBSECTION_SIZE = 3;
|
||||
private static final int NO_VALUE = 0;
|
||||
private static final int CONSTRAINTS = 4;
|
||||
private static final int MIN_VALUE = 1;
|
||||
private static final int MAX_VALUE = 9;
|
||||
private static final int COVER_START_INDEX = 1;
|
||||
|
||||
private static int[][] board = {
|
||||
{8, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 3, 6, 0, 0, 0, 0, 0},
|
||||
{0, 7, 0, 0, 9, 0, 2, 0, 0},
|
||||
{0, 5, 0, 0, 0, 7, 0, 0, 0},
|
||||
{0, 0, 0, 0, 4, 5, 7, 0, 0},
|
||||
{0, 0, 0, 1, 0, 0, 0, 3, 0},
|
||||
{0, 0, 1, 0, 0, 0, 0, 6, 8},
|
||||
{0, 0, 8, 5, 0, 0, 0, 1, 0},
|
||||
{0, 9, 0, 0, 0, 0, 4, 0, 0}
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
DancingLinksAlgorithm solver = new DancingLinksAlgorithm();
|
||||
solver.solve(board);
|
||||
}
|
||||
|
||||
private void solve(int[][] board) {
|
||||
boolean[][] cover = initializeExactCoverBoard(board);
|
||||
DancingLinks dlx = new DancingLinks(cover);
|
||||
dlx.runSolver();
|
||||
}
|
||||
|
||||
private int getIndex(int row, int col, int num) {
|
||||
return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1);
|
||||
}
|
||||
|
||||
private boolean[][] createExactCoverBoard() {
|
||||
boolean[][] coverBoard = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS];
|
||||
|
||||
int hBase = 0;
|
||||
hBase = checkCellConstraint(coverBoard, hBase);
|
||||
hBase = checkRowConstraint(coverBoard, hBase);
|
||||
hBase = checkColumnConstraint(coverBoard, hBase);
|
||||
checkSubsectionConstraint(coverBoard, hBase);
|
||||
|
||||
return coverBoard;
|
||||
}
|
||||
|
||||
private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) {
|
||||
for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
|
||||
for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) {
|
||||
for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) {
|
||||
int index = getIndex(br + rDelta, bc + cDelta, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private int checkColumnConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
|
||||
for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) {
|
||||
int index = getIndex(r1, c, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private int checkRowConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
|
||||
for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) {
|
||||
int index = getIndex(r, c1, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private int checkCellConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
|
||||
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
|
||||
int index = getIndex(r, c, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private boolean[][] initializeExactCoverBoard(int[][] board) {
|
||||
boolean[][] coverBoard = createExactCoverBoard();
|
||||
for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) {
|
||||
for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) {
|
||||
int n = board[i - 1][j - 1];
|
||||
if (n != NO_VALUE) {
|
||||
for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
|
||||
if (num != n) {
|
||||
Arrays.fill(coverBoard[getIndex(i, j, num)], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return coverBoard;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
class DancingNode {
|
||||
DancingNode L, R, U, D;
|
||||
ColumnNode C;
|
||||
|
||||
DancingNode hookDown(DancingNode n1) {
|
||||
assert (this.C == n1.C);
|
||||
n1.D = this.D;
|
||||
n1.D.U = n1;
|
||||
n1.U = this;
|
||||
this.D = n1;
|
||||
return n1;
|
||||
}
|
||||
|
||||
DancingNode hookRight(DancingNode n1) {
|
||||
n1.R = this.R;
|
||||
n1.R.L = n1;
|
||||
n1.L = this;
|
||||
this.R = n1;
|
||||
return n1;
|
||||
}
|
||||
|
||||
void unlinkLR() {
|
||||
this.L.R = this.R;
|
||||
this.R.L = this.L;
|
||||
}
|
||||
|
||||
void relinkLR() {
|
||||
this.L.R = this.R.L = this;
|
||||
}
|
||||
|
||||
void unlinkUD() {
|
||||
this.U.D = this.D;
|
||||
this.D.U = this.U;
|
||||
}
|
||||
|
||||
void relinkUD() {
|
||||
this.U.D = this.D.U = this;
|
||||
}
|
||||
|
||||
DancingNode() {
|
||||
L = R = U = D = this;
|
||||
}
|
||||
|
||||
DancingNode(ColumnNode c) {
|
||||
this();
|
||||
C = c;
|
||||
}
|
||||
}
|
12
algorithms/src/main/resources/maze/maze1.txt
Normal file
12
algorithms/src/main/resources/maze/maze1.txt
Normal file
@ -0,0 +1,12 @@
|
||||
S ########
|
||||
# #
|
||||
# ### ## #
|
||||
# # # #
|
||||
# # # # #
|
||||
# ## #####
|
||||
# # #
|
||||
# # # # #
|
||||
##### ####
|
||||
# # E
|
||||
# # # #
|
||||
##########
|
22
algorithms/src/main/resources/maze/maze2.txt
Normal file
22
algorithms/src/main/resources/maze/maze2.txt
Normal file
@ -0,0 +1,22 @@
|
||||
S ##########################
|
||||
# # # #
|
||||
# # #### ############### #
|
||||
# # # # # #
|
||||
# # #### # # ###############
|
||||
# # # # # # #
|
||||
# # # #### ### ########### #
|
||||
# # # # # #
|
||||
# ################## #
|
||||
######### # # # # #
|
||||
# # #### # ####### # #
|
||||
# # ### ### # # # # #
|
||||
# # ## # ##### # #
|
||||
##### ####### # # # # #
|
||||
# # ## ## #### # #
|
||||
# ##### ####### # #
|
||||
# # ############
|
||||
####### ######### # #
|
||||
# # ######## #
|
||||
# ####### ###### ## # E
|
||||
# # # ## #
|
||||
############################
|
@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
- [Microsoft Word Processing in Java with Apache POI](http://www.baeldung.com/java-microsoft-word-with-apache-poi)
|
||||
- [Working with Microsoft Excel in Java](http://www.baeldung.com/java-microsoft-excel)
|
||||
- [Creating a MS PowerPoint Presentation in Java](https://github.com/eugenp/tutorials/tree/master/apache-poi)
|
||||
|
@ -37,3 +37,6 @@
|
||||
- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream)
|
||||
- [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream)
|
||||
- [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices)
|
||||
- [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency)
|
||||
- [Primitive Type Streams in Java 8](http://www.baeldung.com/java-8-primitive-streams)
|
||||
- [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
||||
|
@ -1,45 +1,19 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
public class Executor {
|
||||
public void executeCustomSpliterator() {
|
||||
Article article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
|
||||
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
|
||||
new Author("Alice", 1), new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
|
||||
Stream<Author> stream = IntStream.range(0, article.getListOfAuthors()
|
||||
.size())
|
||||
.mapToObj(article.getListOfAuthors()::get);
|
||||
System.out.println("count= " + countAutors(stream.parallel()));
|
||||
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
|
||||
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
|
||||
System.out.println("count= " + countAutors(stream2.parallel()));
|
||||
}
|
||||
|
||||
public void executeSpliterator() {
|
||||
Spliterator<Article> split1 = generateElements().spliterator();
|
||||
Spliterator<Article> split2 = split1.trySplit();
|
||||
ExecutorService service = Executors.newCachedThreadPool();
|
||||
service.execute(new Task(split1));
|
||||
service.execute(new Task(split2));
|
||||
}
|
||||
public static int countAutors(Stream<Author> stream) {
|
||||
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
|
||||
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
||||
return wordCounter.getCounter();
|
||||
}
|
||||
|
||||
private static int countAutors(Stream<Author> stream) {
|
||||
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
||||
return wordCounter.getCounter();
|
||||
}
|
||||
public static List<Article> generateElements() {
|
||||
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Article> generateElements() {
|
||||
return Stream.generate(() -> new Article("Java"))
|
||||
.limit(35000)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
@ -2,46 +2,48 @@ package com.baeldung.spliteratorAPI;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class RelatedAuthorSpliterator implements Spliterator<Author> {
|
||||
private final List<Author> list;
|
||||
private int current = 0;
|
||||
private final List<Author> list;
|
||||
AtomicInteger current = new AtomicInteger();
|
||||
|
||||
public RelatedAuthorSpliterator(List<Author> list) {
|
||||
this.list = list;
|
||||
}
|
||||
public RelatedAuthorSpliterator(List<Author> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Author> action) {
|
||||
action.accept(list.get(current++));
|
||||
return current < list.size();
|
||||
}
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Author> action) {
|
||||
|
||||
@Override
|
||||
public Spliterator<Author> trySplit() {
|
||||
int currentSize = list.size() - current;
|
||||
if (currentSize < 10) {
|
||||
return null;
|
||||
}
|
||||
for (int splitPos = currentSize / 2 + current; splitPos < list.size(); splitPos++) {
|
||||
if (list.get(splitPos)
|
||||
.getRelatedArticleId() == 0) {
|
||||
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current, splitPos));
|
||||
current = splitPos;
|
||||
return spliterator;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
action.accept(list.get(current.getAndIncrement()));
|
||||
return current.get() < list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long estimateSize() {
|
||||
return list.size() - current;
|
||||
}
|
||||
@Override
|
||||
public Spliterator<Author> trySplit() {
|
||||
int currentSize = list.size() - current.get();
|
||||
if (currentSize < 10) {
|
||||
return null;
|
||||
}
|
||||
for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) {
|
||||
if (list.get(splitPos).getRelatedArticleId() == 0) {
|
||||
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos));
|
||||
current.set(splitPos);
|
||||
return spliterator;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long estimateSize() {
|
||||
return list.size() - current.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int characteristics() {
|
||||
return CONCURRENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int characteristics() {
|
||||
return SIZED + CONCURRENT;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
|
||||
import java.util.Spliterator;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class Task implements Runnable {
|
||||
public class Task implements Callable<String> {
|
||||
private Spliterator<Article> spliterator;
|
||||
private final static String SUFFIX = "- published by Baeldung";
|
||||
|
||||
@ -11,7 +12,7 @@ public class Task implements Runnable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public String call() {
|
||||
int current = 0;
|
||||
while (spliterator.tryAdvance(article -> {
|
||||
article.setName(article.getName()
|
||||
@ -20,7 +21,7 @@ public class Task implements Runnable {
|
||||
current++;
|
||||
}
|
||||
;
|
||||
System.out.println(Thread.currentThread()
|
||||
.getName() + ":" + current);
|
||||
return Thread.currentThread()
|
||||
.getName() + ":" + current;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
package com.baeldung.spliteratorAPI;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Spliterator;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ExecutorTest {
|
||||
Article article;
|
||||
Stream<Author> stream;
|
||||
Spliterator<Author> spliterator;
|
||||
Spliterator<Article> split1;
|
||||
Spliterator<Article> split2;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1),
|
||||
new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
|
||||
new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
|
||||
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
|
||||
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
|
||||
new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
|
||||
stream = article.getListOfAuthors().stream();
|
||||
split1 = Executor.generateElements().spliterator();
|
||||
split2 = split1.trySplit();
|
||||
spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAstreamOfAuthors_whenProcessedInParallelWithCustomSpliterator_coubtProducessRightOutput() {
|
||||
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
|
||||
assertThat(Executor.countAutors(stream2.parallel())).isEqualTo(9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSpliterator_whenAppliedToAListOfArticle_thenSplittedInHalf() {
|
||||
assertThat(new Task(split1).call()).containsSequence(Executor.generateElements().size() / 2 + "");
|
||||
assertThat(new Task(split2).call()).containsSequence(Executor.generateElements().size() / 2 + "");
|
||||
}
|
||||
}
|
@ -0,0 +1,218 @@
|
||||
package com.baeldung.java9.httpclient;
|
||||
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.collection.IsEmptyCollection.empty;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
public class HttpClientTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromString("Sample body"))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.proxy(ProxySelector.getDefault())
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM));
|
||||
assertThat(response.body(), containsString("https://stackoverflow.com/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.finalRequest()
|
||||
.uri()
|
||||
.toString(), equalTo("https://stackoverflow.com/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/basic-auth"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.authenticator(new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication("postman", "password".toCharArray());
|
||||
}
|
||||
})
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromString("Sample body"))
|
||||
.build();
|
||||
CompletableFuture<HttpResponse<String>> response = HttpClient.newBuilder()
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
|
||||
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
|
||||
.executor(executorService)
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
|
||||
.executor(executorService)
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
|
||||
.executor(executorService)
|
||||
.build()
|
||||
.sendAsync(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
CompletableFuture.allOf(response1, response2, response3)
|
||||
.join();
|
||||
|
||||
assertThat(response1.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response2.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response3.get()
|
||||
.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
|
||||
.build();
|
||||
|
||||
httpClient.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(httpClient.cookieManager()
|
||||
.get()
|
||||
.getCookieStore()
|
||||
.getCookies(), empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpClient httpClient = HttpClient.newBuilder()
|
||||
.cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
|
||||
.build();
|
||||
|
||||
httpClient.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(httpClient.cookieManager()
|
||||
.get()
|
||||
.getCookieStore()
|
||||
.getCookies(), not(empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException {
|
||||
List<URI> targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2"));
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
|
||||
List<CompletableFuture<String>> futures = targets.stream()
|
||||
.map(target -> client.sendAsync(HttpRequest.newBuilder(target)
|
||||
.GET()
|
||||
.build(), HttpResponse.BodyHandler.asString())
|
||||
.thenApply(response -> response.body()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
|
||||
.join();
|
||||
|
||||
if (futures.get(0)
|
||||
.get()
|
||||
.contains("foo1")) {
|
||||
assertThat(futures.get(0)
|
||||
.get(), containsString("bar1"));
|
||||
assertThat(futures.get(1)
|
||||
.get(), containsString("bar2"));
|
||||
} else {
|
||||
assertThat(futures.get(1)
|
||||
.get(), containsString("bar2"));
|
||||
assertThat(futures.get(1)
|
||||
.get(), containsString("bar1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
package com.baeldung.java9.httpclient;
|
||||
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Duration;
|
||||
|
||||
import static java.time.temporal.ChronoUnit.SECONDS;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
public class HttpRequestTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.headers("key1", "value1", "key2", "value2")
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.timeout(Duration.of(10, SECONDS))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.POST(HttpRequest.noBody())
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromString("Sample request body"))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException {
|
||||
byte[] sampleData = "Sample request body".getBytes();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromInputStream(() -> new ByteArrayInputStream(sampleData)))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||
byte[] sampleData = "Sample request body".getBytes();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromByteArray(sampleData))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/post"))
|
||||
.headers("Content-Type", "text/plain;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyProcessor.fromFile(Paths.get("src/test/resources/sample.txt")))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), containsString("Sample file content"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.baeldung.java9.httpclient;
|
||||
|
||||
import jdk.incubator.http.HttpClient;
|
||||
import jdk.incubator.http.HttpRequest;
|
||||
import jdk.incubator.http.HttpResponse;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.Matchers.isEmptyString;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
public class HttpResponseTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://postman-echo.com/get"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = HttpClient.newHttpClient()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK));
|
||||
assertThat(response.body(), not(isEmptyString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://stackoverflow.com"))
|
||||
.version(HttpClient.Version.HTTP_1_1)
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||
.build()
|
||||
.send(request, HttpResponse.BodyHandler.asString());
|
||||
|
||||
assertThat(request.uri()
|
||||
.toString(), equalTo("http://stackoverflow.com"));
|
||||
assertThat(response.uri()
|
||||
.toString(), equalTo("https://stackoverflow.com/"));
|
||||
}
|
||||
|
||||
}
|
@ -33,3 +33,4 @@
|
||||
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
|
||||
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
|
||||
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
|
||||
- [ExecutorService - Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
|
||||
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.concurrent.prioritytaskexecution;
|
||||
|
||||
public class Job implements Runnable {
|
||||
private String jobName;
|
||||
private JobPriority jobPriority;
|
||||
|
||||
public Job(String jobName, JobPriority jobPriority) {
|
||||
this.jobName = jobName;
|
||||
this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM;
|
||||
}
|
||||
|
||||
public JobPriority getJobPriority() {
|
||||
return jobPriority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println("Job:" + jobName + " Priority:" + jobPriority);
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.concurrent.prioritytaskexecution;
|
||||
|
||||
public enum JobPriority {
|
||||
HIGH,
|
||||
MEDIUM,
|
||||
LOW
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.baeldung.concurrent.prioritytaskexecution;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class PriorityJobScheduler {
|
||||
|
||||
private ExecutorService priorityJobPoolExecutor;
|
||||
private ExecutorService priorityJobScheduler;
|
||||
private PriorityBlockingQueue<Runnable> priorityQueue;
|
||||
|
||||
public PriorityJobScheduler(Integer poolSize, Integer queueSize) {
|
||||
priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize);
|
||||
Comparator<? super Job> jobComparator = Comparator.comparing(Job::getJobPriority);
|
||||
priorityQueue = new PriorityBlockingQueue<Runnable>(queueSize,
|
||||
(Comparator<? super Runnable>) jobComparator);
|
||||
|
||||
priorityJobScheduler = Executors.newSingleThreadExecutor();
|
||||
priorityJobScheduler.execute(()->{
|
||||
while (true) {
|
||||
try {
|
||||
priorityJobPoolExecutor.execute(priorityQueue.take());
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void scheduleJob(Job job) {
|
||||
priorityQueue.add(job);
|
||||
}
|
||||
|
||||
public int getQueuedTaskCount() {
|
||||
return priorityQueue.size();
|
||||
}
|
||||
|
||||
protected void close(ExecutorService scheduler) {
|
||||
scheduler.shutdown();
|
||||
try {
|
||||
if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
|
||||
scheduler.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
scheduler.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
public void closeScheduler() {
|
||||
close(priorityJobPoolExecutor);
|
||||
close(priorityJobScheduler);
|
||||
}
|
||||
}
|
@ -43,8 +43,9 @@ public class ControlSubThread implements Runnable {
|
||||
try {
|
||||
Thread.sleep(interval);
|
||||
} catch (InterruptedException e) {
|
||||
// no-op, just loop again
|
||||
}
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread was interrupted, Failed to complete operation");
|
||||
}
|
||||
// do something
|
||||
}
|
||||
stopped.set(true);
|
||||
|
@ -11,7 +11,10 @@ public class Data {
|
||||
while (transfer) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
transfer = true;
|
||||
|
||||
@ -23,7 +26,10 @@ public class Data {
|
||||
while (!transfer) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
transfer = false;
|
||||
|
||||
|
@ -19,7 +19,10 @@ public class Receiver implements Runnable {
|
||||
//Thread.sleep() to mimic heavy server-side processing
|
||||
try {
|
||||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
|
||||
} catch (InterruptedException e) {}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -11,11 +11,11 @@ public class Sender implements Runnable {
|
||||
|
||||
public void run() {
|
||||
String packets[] = {
|
||||
"First packet",
|
||||
"Second packet",
|
||||
"Third packet",
|
||||
"Fourth packet",
|
||||
"End"
|
||||
"First packet",
|
||||
"Second packet",
|
||||
"Third packet",
|
||||
"Fourth packet",
|
||||
"End"
|
||||
};
|
||||
|
||||
for (String packet : packets) {
|
||||
@ -24,7 +24,10 @@ public class Sender implements Runnable {
|
||||
//Thread.sleep() to mimic heavy server-side processing
|
||||
try {
|
||||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
|
||||
} catch (InterruptedException e) {}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.concurrent.prioritytaskexecution;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PriorityJobSchedulerUnitTest {
|
||||
private static int POOL_SIZE = 1;
|
||||
private static int QUEUE_SIZE = 10;
|
||||
|
||||
@Test
|
||||
public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() {
|
||||
Job job1 = new Job("Job1", JobPriority.LOW);
|
||||
Job job2 = new Job("Job2", JobPriority.MEDIUM);
|
||||
Job job3 = new Job("Job3", JobPriority.HIGH);
|
||||
Job job4 = new Job("Job4", JobPriority.MEDIUM);
|
||||
Job job5 = new Job("Job5", JobPriority.LOW);
|
||||
Job job6 = new Job("Job6", JobPriority.HIGH);
|
||||
|
||||
PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE);
|
||||
|
||||
pjs.scheduleJob(job1);
|
||||
pjs.scheduleJob(job2);
|
||||
pjs.scheduleJob(job3);
|
||||
pjs.scheduleJob(job4);
|
||||
pjs.scheduleJob(job5);
|
||||
pjs.scheduleJob(job6);
|
||||
|
||||
// ensure no tasks is pending before closing the scheduler
|
||||
while (pjs.getQueuedTaskCount() != 0);
|
||||
|
||||
// delay to avoid job sleep (added for demo) being interrupted
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
|
||||
pjs.closeScheduler();
|
||||
}
|
||||
}
|
@ -13,38 +13,38 @@ import org.junit.Test;
|
||||
|
||||
public class NetworkIntegrationTest {
|
||||
|
||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
||||
private String expected;
|
||||
|
||||
@Before
|
||||
public void setUpStreams() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
System.setErr(new PrintStream(errContent));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUpExpectedOutput() {
|
||||
StringWriter expectedStringWriter = new StringWriter();
|
||||
|
||||
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
|
||||
printWriter.println("First packet");
|
||||
printWriter.println("Second packet");
|
||||
printWriter.println("Third packet");
|
||||
printWriter.println("Fourth packet");
|
||||
printWriter.close();
|
||||
|
||||
expected = expectedStringWriter.toString();
|
||||
}
|
||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
||||
private String expected;
|
||||
|
||||
@Before
|
||||
public void setUpStreams() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
System.setErr(new PrintStream(errContent));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUpExpectedOutput() {
|
||||
StringWriter expectedStringWriter = new StringWriter();
|
||||
|
||||
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
|
||||
printWriter.println("First packet");
|
||||
printWriter.println("Second packet");
|
||||
printWriter.println("Third packet");
|
||||
printWriter.println("Fourth packet");
|
||||
printWriter.close();
|
||||
|
||||
expected = expectedStringWriter.toString();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUpStreams() {
|
||||
System.setOut(null);
|
||||
System.setErr(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
|
||||
@After
|
||||
public void cleanUpStreams() {
|
||||
System.setOut(null);
|
||||
System.setErr(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
|
||||
Data data = new Data();
|
||||
Thread sender = new Thread(new Sender(data));
|
||||
Thread receiver = new Thread(new Receiver(data));
|
||||
@ -54,12 +54,13 @@ public class NetworkIntegrationTest {
|
||||
|
||||
//wait for sender and receiver to finish before we test against expected
|
||||
try {
|
||||
sender.join();
|
||||
receiver.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
sender.join();
|
||||
receiver.join();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
}
|
||||
|
@ -118,3 +118,12 @@
|
||||
- [Implementing a Binary Tree in Java](http://www.baeldung.com/java-binary-tree)
|
||||
- [A Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random)
|
||||
- [RegEx for matching Date Pattern in Java](http://www.baeldung.com/java-date-regular-expressions)
|
||||
- [Introduction to the JDBC RowSet Interface in Java](http://www.baeldung.com/java-jdbc-rowset)
|
||||
- [Nested Classes in Java](http://www.baeldung.com/java-nested-classes)
|
||||
- [A Guide to Java Loops](http://www.baeldung.com/java-loops)
|
||||
- [Varargs in Java](http://www.baeldung.com/java-varargs)
|
||||
- [A Guide to HashSet in Java](http://www.baeldung.com/java-hashset)
|
||||
- [A Guide to Inner Interfaces in Java](http://www.baeldung.com/java-inner-interfaces)
|
||||
- [Polymorphism in Java](http://www.baeldung.com/java-polymorphism)
|
||||
- [Recursion In Java](http://www.baeldung.com/java-recursion)
|
||||
- [A Guide to the finalize Method in Java](http://www.baeldung.com/java-finalize)
|
||||
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
public interface Channel {
|
||||
public void update(Object o);
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NewsAgency {
|
||||
private String news;
|
||||
private List<Channel> channels = new ArrayList<>();
|
||||
|
||||
public void addObserver(Channel channel) {
|
||||
this.channels.add(channel);
|
||||
}
|
||||
|
||||
public void removeObserver(Channel channel) {
|
||||
this.channels.remove(channel);
|
||||
}
|
||||
|
||||
public void setNews(String news) {
|
||||
this.news = news;
|
||||
for (Channel channel : this.channels) {
|
||||
channel.update(this.news);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
public class NewsChannel implements Channel {
|
||||
|
||||
private String news;
|
||||
|
||||
@Override
|
||||
public void update(Object news) {
|
||||
this.setNews((String) news);
|
||||
}
|
||||
|
||||
public String getNews() {
|
||||
return news;
|
||||
}
|
||||
|
||||
public void setNews(String news) {
|
||||
this.news = news;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
import java.util.Observable;
|
||||
|
||||
public class ONewsAgency extends Observable {
|
||||
private String news;
|
||||
|
||||
public void setNews(String news) {
|
||||
this.news = news;
|
||||
setChanged();
|
||||
notifyObservers(news);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
public class ONewsChannel implements Observer {
|
||||
|
||||
private String news;
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object news) {
|
||||
this.setNews((String) news);
|
||||
}
|
||||
|
||||
public String getNews() {
|
||||
return news;
|
||||
}
|
||||
|
||||
public void setNews(String news) {
|
||||
this.news = news;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
|
||||
public class PCLNewsAgency {
|
||||
private String news;
|
||||
|
||||
private PropertyChangeSupport support;
|
||||
|
||||
public PCLNewsAgency() {
|
||||
support = new PropertyChangeSupport(this);
|
||||
}
|
||||
|
||||
public void addPropertyChangeListener(PropertyChangeListener pcl) {
|
||||
support.addPropertyChangeListener(pcl);
|
||||
}
|
||||
|
||||
public void removePropertyChangeListener(PropertyChangeListener pcl) {
|
||||
support.removePropertyChangeListener(pcl);
|
||||
}
|
||||
|
||||
public void setNews(String value) {
|
||||
support.firePropertyChange("news", this.news, value);
|
||||
this.news = value;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
|
||||
public class PCLNewsChannel implements PropertyChangeListener {
|
||||
|
||||
private String news;
|
||||
|
||||
public void propertyChange(PropertyChangeEvent evt) {
|
||||
this.setNews((String) evt.getNewValue());
|
||||
}
|
||||
|
||||
public String getNews() {
|
||||
return news;
|
||||
}
|
||||
|
||||
public void setNews(String news) {
|
||||
this.news = news;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.finalize;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class CloseableResource implements AutoCloseable {
|
||||
private BufferedReader reader;
|
||||
|
||||
public CloseableResource() {
|
||||
InputStream input = this.getClass().getClassLoader().getResourceAsStream("file.txt");
|
||||
reader = new BufferedReader(new InputStreamReader(input));
|
||||
}
|
||||
|
||||
public String readFirstLine() throws IOException {
|
||||
String firstLine = reader.readLine();
|
||||
return firstLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
reader.close();
|
||||
System.out.println("Closed BufferedReader in the close method");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.finalize;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class Finalizable {
|
||||
private BufferedReader reader;
|
||||
|
||||
public Finalizable() {
|
||||
InputStream input = this.getClass().getClassLoader().getResourceAsStream("file.txt");
|
||||
reader = new BufferedReader(new InputStreamReader(input));
|
||||
}
|
||||
|
||||
public String readFirstLine() throws IOException {
|
||||
String firstLine = reader.readLine();
|
||||
return firstLine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finalize() {
|
||||
try {
|
||||
reader.close();
|
||||
System.out.println("Closed BufferedReader in the finalizer");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
16
core-java/src/main/java/com/baeldung/javac/Data.java
Normal file
16
core-java/src/main/java/com/baeldung/javac/Data.java
Normal file
@ -0,0 +1,16 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Data {
|
||||
List<String> textList = new ArrayList();
|
||||
|
||||
public void addText(String text) {
|
||||
textList.add(text);
|
||||
}
|
||||
|
||||
public List getTextList() {
|
||||
return this.textList;
|
||||
}
|
||||
}
|
62
core-java/src/main/java/com/baeldung/trie/Trie.java
Normal file
62
core-java/src/main/java/com/baeldung/trie/Trie.java
Normal file
@ -0,0 +1,62 @@
|
||||
package com.baeldung.trie;
|
||||
|
||||
public class Trie {
|
||||
private TrieNode root;
|
||||
|
||||
Trie() {
|
||||
root = new TrieNode();
|
||||
}
|
||||
|
||||
public void insert(String word) {
|
||||
TrieNode current = root;
|
||||
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
current = current.getChildren().computeIfAbsent(word.charAt(i), c -> new TrieNode());
|
||||
}
|
||||
current.setEndOfWord(true);
|
||||
}
|
||||
|
||||
public boolean delete(String word) {
|
||||
return delete(root, word, 0);
|
||||
}
|
||||
|
||||
public boolean containsNode(String word) {
|
||||
TrieNode current = root;
|
||||
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
TrieNode node = current.getChildren().get(ch);
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
current = node;
|
||||
}
|
||||
return current.isEndOfWord();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
private boolean delete(TrieNode current, String word, int index) {
|
||||
if (index == word.length()) {
|
||||
if (!current.isEndOfWord()) {
|
||||
return false;
|
||||
}
|
||||
current.setEndOfWord(false);
|
||||
return current.getChildren().isEmpty();
|
||||
}
|
||||
char ch = word.charAt(index);
|
||||
TrieNode node = current.getChildren().get(ch);
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
|
||||
|
||||
if (shouldDeleteCurrentNode) {
|
||||
current.getChildren().remove(ch);
|
||||
return current.getChildren().isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
31
core-java/src/main/java/com/baeldung/trie/TrieNode.java
Normal file
31
core-java/src/main/java/com/baeldung/trie/TrieNode.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.baeldung.trie;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class TrieNode {
|
||||
private Map<Character, TrieNode> children;
|
||||
private boolean endOfWord;
|
||||
|
||||
public TrieNode() {
|
||||
children = new HashMap<>();
|
||||
endOfWord = false;
|
||||
}
|
||||
|
||||
public Map<Character, TrieNode> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(Map<Character, TrieNode> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public boolean isEndOfWord() {
|
||||
return endOfWord;
|
||||
}
|
||||
|
||||
public void setEndOfWord(boolean endOfWord) {
|
||||
this.endOfWord = endOfWord;
|
||||
}
|
||||
|
||||
}
|
2
core-java/src/main/java/javac-args/arguments
Normal file
2
core-java/src/main/java/javac-args/arguments
Normal file
@ -0,0 +1,2 @@
|
||||
-d javac-target -verbose
|
||||
com/baeldung/javac/Data.java
|
2
core-java/src/main/java/javac-args/options
Normal file
2
core-java/src/main/java/javac-args/options
Normal file
@ -0,0 +1,2 @@
|
||||
-d javac-target
|
||||
-verbose
|
1
core-java/src/main/java/javac-args/types
Normal file
1
core-java/src/main/java/javac-args/types
Normal file
@ -0,0 +1 @@
|
||||
com/baeldung/javac/Data.java
|
3
core-java/src/main/java/javac-args/xlint-ops
Normal file
3
core-java/src/main/java/javac-args/xlint-ops
Normal file
@ -0,0 +1,3 @@
|
||||
-d javac-target
|
||||
-Xlint:rawtypes,unchecked,static,cast,serial,fallthrough
|
||||
com/baeldung/javac/Data.java
|
@ -0,0 +1,47 @@
|
||||
package com.baeldung.designpatterns.observer;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.designpatterns.observer.NewsAgency;
|
||||
import com.baeldung.designpatterns.observer.NewsChannel;
|
||||
|
||||
public class ObserverIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenChangingNewsAgencyState_thenNewsChannelNotified() {
|
||||
|
||||
NewsAgency observable = new NewsAgency();
|
||||
NewsChannel observer = new NewsChannel();
|
||||
|
||||
observable.addObserver(observer);
|
||||
|
||||
observable.setNews("news");
|
||||
assertEquals(observer.getNews(), "news");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangingONewsAgencyState_thenONewsChannelNotified() {
|
||||
|
||||
ONewsAgency observable = new ONewsAgency();
|
||||
ONewsChannel observer = new ONewsChannel();
|
||||
|
||||
observable.addObserver(observer);
|
||||
|
||||
observable.setNews("news");
|
||||
assertEquals(observer.getNews(), "news");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangingPCLNewsAgencyState_thenONewsChannelNotified() {
|
||||
|
||||
PCLNewsAgency observable = new PCLNewsAgency();
|
||||
PCLNewsChannel observer = new PCLNewsChannel();
|
||||
|
||||
observable.addPropertyChangeListener(observer);
|
||||
|
||||
observable.setNews("news");
|
||||
assertEquals(observer.getNews(), "news");
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.finalize;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FinalizeUnitTest {
|
||||
@Test
|
||||
public void whenGC_thenFinalizerExecuted() throws IOException {
|
||||
String firstLine = new Finalizable().readFirstLine();
|
||||
Assert.assertEquals("baeldung.com", firstLine);
|
||||
System.gc();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTryWResourcesExits_thenResourceClosed() throws IOException {
|
||||
try (CloseableResource resource = new CloseableResource()) {
|
||||
String firstLine = resource.readFirstLine();
|
||||
Assert.assertEquals("baeldung.com", firstLine);
|
||||
}
|
||||
}
|
||||
}
|
68
core-java/src/test/java/com/baeldung/trie/TrieTest.java
Normal file
68
core-java/src/test/java/com/baeldung/trie/TrieTest.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.baeldung.trie;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TrieTest {
|
||||
|
||||
@Test
|
||||
public void whenEmptyTrie_thenNoElements() {
|
||||
Trie trie = new Trie();
|
||||
|
||||
assertFalse(trie.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenATrie_whenAddingElements_thenTrieNotEmpty() {
|
||||
Trie trie = createExampleTrie();
|
||||
|
||||
assertFalse(trie.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenATrie_whenAddingElements_thenTrieHasThoseElements() {
|
||||
Trie trie = createExampleTrie();
|
||||
|
||||
assertFalse(trie.containsNode("3"));
|
||||
assertFalse(trie.containsNode("vida"));
|
||||
|
||||
assertTrue(trie.containsNode("Programming"));
|
||||
assertTrue(trie.containsNode("is"));
|
||||
assertTrue(trie.containsNode("a"));
|
||||
assertTrue(trie.containsNode("way"));
|
||||
assertTrue(trie.containsNode("of"));
|
||||
assertTrue(trie.containsNode("life"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenATrie_whenLookingForNonExistingElement_thenReturnsFalse() {
|
||||
Trie trie = createExampleTrie();
|
||||
|
||||
assertFalse(trie.containsNode("99"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenATrie_whenDeletingElements_thenTreeDoesNotContainThoseElements() {
|
||||
|
||||
Trie trie = createExampleTrie();
|
||||
|
||||
assertTrue(trie.containsNode("Programming"));
|
||||
trie.delete("Programming");
|
||||
assertFalse(trie.containsNode("Programming"));
|
||||
}
|
||||
|
||||
private Trie createExampleTrie() {
|
||||
Trie trie = new Trie();
|
||||
|
||||
trie.insert("Programming");
|
||||
trie.insert("is");
|
||||
trie.insert("a");
|
||||
trie.insert("way");
|
||||
trie.insert("of");
|
||||
trie.insert("life");
|
||||
|
||||
return trie;
|
||||
}
|
||||
}
|
@ -16,5 +16,5 @@
|
||||
- [Delegated Properties in Kotlin](http://www.baeldung.com/kotlin-delegated-properties)
|
||||
- [Sealed Classes in Kotlin](http://www.baeldung.com/kotlin-sealed-classes)
|
||||
- [JUnit 5 for Kotlin Developers](http://www.baeldung.com/junit-5-kotlin)
|
||||
|
||||
- [Extension Methods in Kotlin](http://www.baeldung.com/kotlin-extension-methods)
|
||||
|
||||
|
@ -44,6 +44,16 @@
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>${kotlin-stdlib.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jre8</artifactId>
|
||||
<version>${kotlin-stdlib.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>khttp</groupId>
|
||||
<artifactId>khttp</artifactId>
|
||||
<version>0.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-junit</artifactId>
|
||||
|
67
core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt
Normal file
67
core-kotlin/src/test/kotlin/com/baeldung/kotlin/UseTest.kt
Normal file
@ -0,0 +1,67 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Test
|
||||
import java.beans.ExceptionListener
|
||||
import java.beans.XMLEncoder
|
||||
import java.io.*
|
||||
import java.lang.Exception
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.fail
|
||||
|
||||
class UseTest {
|
||||
|
||||
@Test
|
||||
fun givenCloseable_whenUseIsCalled_thenItIsClosed() {
|
||||
val stringWriter = StringWriter()
|
||||
val writer = BufferedWriter(stringWriter) //Using a BufferedWriter because after close() it throws.
|
||||
writer.use {
|
||||
assertEquals(writer, it)
|
||||
|
||||
it.write("something")
|
||||
}
|
||||
try {
|
||||
writer.write("something else")
|
||||
|
||||
fail("write() should have thrown an exception because the writer is closed.")
|
||||
} catch (e: IOException) {
|
||||
//Ok
|
||||
}
|
||||
|
||||
assertEquals("something", stringWriter.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenAutoCloseable_whenUseIsCalled_thenItIsClosed() {
|
||||
val baos = ByteArrayOutputStream()
|
||||
val encoder = XMLEncoder(PrintStream(baos)) //XMLEncoder is AutoCloseable but not Closeable.
|
||||
//Here, we use a PrintStream because after close() it throws.
|
||||
encoder.exceptionListener = ThrowingExceptionListener()
|
||||
encoder.use {
|
||||
assertEquals(encoder, it)
|
||||
|
||||
it.writeObject("something")
|
||||
}
|
||||
try {
|
||||
encoder.writeObject("something else")
|
||||
encoder.flush()
|
||||
|
||||
fail("write() should have thrown an exception because the encoder is closed.")
|
||||
} catch (e: IOException) {
|
||||
//Ok
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSimpleFormIsUsed_thenItWorks() {
|
||||
StringWriter().use { it.write("something") }
|
||||
}
|
||||
}
|
||||
|
||||
class ThrowingExceptionListener : ExceptionListener {
|
||||
override fun exceptionThrown(e: Exception?) {
|
||||
if(e != null) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
package com.baeldung.kotlin.khttp
|
||||
|
||||
import khttp.structures.files.FileLike
|
||||
import org.json.JSONObject
|
||||
import org.junit.Test
|
||||
import java.beans.ExceptionListener
|
||||
import java.beans.XMLEncoder
|
||||
import java.io.*
|
||||
import java.lang.Exception
|
||||
import java.net.ConnectException
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.fail
|
||||
|
||||
class KhttpTest {
|
||||
|
||||
@Test
|
||||
fun whenHttpGetRequestIsMade_thenArgsAreReturned() {
|
||||
val response = khttp.get(
|
||||
url = "http://httpbin.org/get",
|
||||
params = mapOf("p1" to "1", "p2" to "2"))
|
||||
val args = response.jsonObject.getJSONObject("args")
|
||||
|
||||
assertEquals("1", args["p1"])
|
||||
assertEquals("2", args["p2"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenAlternateHttpGetRequestIsMade_thenArgsAreReturned() {
|
||||
val response = khttp.request(
|
||||
method = "GET",
|
||||
url = "http://httpbin.org/get",
|
||||
params = mapOf("p1" to "1", "p2" to "2"))
|
||||
val args = response.jsonObject.getJSONObject("args")
|
||||
|
||||
assertEquals("1", args["p1"])
|
||||
assertEquals("2", args["p2"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHeadersAreSet_thenHeadersAreSent() {
|
||||
val response = khttp.get(
|
||||
url = "http://httpbin.org/get",
|
||||
headers = mapOf("header1" to "1", "header2" to "2"))
|
||||
val headers = response.jsonObject.getJSONObject("headers")
|
||||
|
||||
assertEquals("1", headers["Header1"])
|
||||
assertEquals("2", headers["Header2"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpPostRequestIsMadeWithJson_thenBodyIsReturned() {
|
||||
val response = khttp.post(
|
||||
url = "http://httpbin.org/post",
|
||||
params = mapOf("p1" to "1", "p2" to "2"),
|
||||
json = mapOf("pr1" to "1", "pr2" to "2"))
|
||||
|
||||
val args = response.jsonObject.getJSONObject("args")
|
||||
|
||||
assertEquals("1", args["p1"])
|
||||
assertEquals("2", args["p2"])
|
||||
|
||||
val json = response.jsonObject.getJSONObject("json")
|
||||
|
||||
assertEquals("1", json["pr1"])
|
||||
assertEquals("2", json["pr2"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpPostRequestIsMadeWithMapData_thenBodyIsReturned() {
|
||||
val response = khttp.post(
|
||||
url = "http://httpbin.org/post",
|
||||
params = mapOf("p1" to "1", "p2" to "2"),
|
||||
data = mapOf("pr1" to "1", "pr2" to "2"))
|
||||
|
||||
val args = response.jsonObject.getJSONObject("args")
|
||||
|
||||
assertEquals("1", args["p1"])
|
||||
assertEquals("2", args["p2"])
|
||||
|
||||
val form = response.jsonObject.getJSONObject("form")
|
||||
|
||||
assertEquals("1", form["pr1"])
|
||||
assertEquals("2", form["pr2"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpPostRequestIsMadeWithFiles_thenBodyIsReturned() {
|
||||
val response = khttp.post(
|
||||
url = "http://httpbin.org/post",
|
||||
params = mapOf("p1" to "1", "p2" to "2"),
|
||||
files = listOf(
|
||||
FileLike("file1", "content1"),
|
||||
FileLike("file2", javaClass.getResource("KhttpTest.class").openStream().readBytes())))
|
||||
|
||||
val args = response.jsonObject.getJSONObject("args")
|
||||
|
||||
assertEquals("1", args["p1"])
|
||||
assertEquals("2", args["p2"])
|
||||
|
||||
val files = response.jsonObject.getJSONObject("files")
|
||||
|
||||
assertEquals("content1", files["file1"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpPostRequestIsMadeWithInputStream_thenBodyIsReturned() {
|
||||
val response = khttp.post(
|
||||
url = "http://httpbin.org/post",
|
||||
params = mapOf("p1" to "1", "p2" to "2"),
|
||||
data = ByteArrayInputStream("content!".toByteArray()))
|
||||
|
||||
val args = response.jsonObject.getJSONObject("args")
|
||||
|
||||
assertEquals("1", args["p1"])
|
||||
assertEquals("2", args["p2"])
|
||||
|
||||
assertEquals("content!", response.jsonObject["data"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpPostStreamingRequestIsMade_thenBodyIsReturnedInChunks() {
|
||||
val response = khttp.post(
|
||||
url = "http://httpbin.org/post",
|
||||
stream = true,
|
||||
json = mapOf("pr1" to "1", "pr2" to "2"))
|
||||
|
||||
val baos = ByteArrayOutputStream()
|
||||
response.contentIterator(chunkSize = 10).forEach { arr : ByteArray -> baos.write(arr) }
|
||||
val json = JSONObject(String(baos.toByteArray())).getJSONObject("json")
|
||||
|
||||
assertEquals("1", json["pr1"])
|
||||
assertEquals("2", json["pr2"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpRequestFails_thenExceptionIsThrown() {
|
||||
try {
|
||||
khttp.get(url = "http://localhost/nothing/to/see/here")
|
||||
|
||||
fail("Should have thrown an exception")
|
||||
} catch (e : ConnectException) {
|
||||
//Ok
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenHttpNotFound_thenExceptionIsThrown() {
|
||||
val response = khttp.get(url = "http://httpbin.org/nothing/to/see/here")
|
||||
|
||||
assertEquals(404, response.statusCode)
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
## Relevant articles:
|
||||
|
||||
- [Intro to Dubbo](http://www.baeldung.com/dubbo-intro)
|
||||
- [Introduction to Dubbo](http://www.baeldung.com/dubbo)
|
||||
|
||||
|
@ -1,2 +1,3 @@
|
||||
### Relevant Articles:
|
||||
- [Database Migrations with Flyway](http://www.baeldung.com/database-migrations-with-flyway)
|
||||
- [A Guide to Flyway Callbacks](http://www.baeldung.com/flyway-callbacks)
|
||||
|
@ -1,2 +1,4 @@
|
||||
## Relevant articles:
|
||||
- [Introduction to Gradle](http://www.baeldung.com/gradle)
|
||||
- [Writing Custom Gradle Plugins](http://www.baeldung.com/gradle-create-plugin)
|
||||
- [Creating a Fat Jar in Gradle](http://www.baeldung.com/gradle-fat-jar)
|
||||
|
@ -0,0 +1,17 @@
|
||||
package org.baeldung.guava.memoizer;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CostlySupplier {
|
||||
|
||||
public static BigInteger generateBigNumber() {
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
return new BigInteger("12345");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.baeldung.guava.memoizer;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class Factorial {
|
||||
|
||||
private static LoadingCache<Integer, BigInteger> memo = CacheBuilder.newBuilder()
|
||||
.build(CacheLoader.from(Factorial::getFactorial));
|
||||
|
||||
public static BigInteger getFactorial(int n) {
|
||||
if (n == 0) {
|
||||
return BigInteger.ONE;
|
||||
} else {
|
||||
return BigInteger.valueOf(n).multiply(memo.getUnchecked(n - 1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.baeldung.guava.memoizer;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class FibonacciSequence {
|
||||
|
||||
private static LoadingCache<Integer, BigInteger> memo = CacheBuilder.newBuilder()
|
||||
.maximumSize(100)
|
||||
.build(CacheLoader.from(FibonacciSequence::getFibonacciNumber));
|
||||
|
||||
public static BigInteger getFibonacciNumber(int n) {
|
||||
if (n == 0) {
|
||||
return BigInteger.ZERO;
|
||||
} else if (n == 1) {
|
||||
return BigInteger.ONE;
|
||||
} else {
|
||||
return memo.getUnchecked(n - 1).add(memo.getUnchecked(n - 2));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.base.Suppliers;
|
||||
import org.baeldung.guava.memoizer.CostlySupplier;
|
||||
import org.baeldung.guava.memoizer.Factorial;
|
||||
import org.baeldung.guava.memoizer.FibonacciSequence;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.hamcrest.number.IsCloseTo.closeTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class GuavaMemoizerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInteger_whenGetFibonacciNumber_thenShouldCalculateFibonacciNumber() {
|
||||
// given
|
||||
int n = 95;
|
||||
|
||||
// when
|
||||
BigInteger fibonacciNumber = FibonacciSequence.getFibonacciNumber(n);
|
||||
|
||||
// then
|
||||
BigInteger expectedFibonacciNumber = new BigInteger("31940434634990099905");
|
||||
assertThat(fibonacciNumber, is(equalTo(expectedFibonacciNumber)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInteger_whenGetFactorial_thenShouldCalculateFactorial() {
|
||||
// given
|
||||
int n = 95;
|
||||
|
||||
// when
|
||||
BigInteger factorial = Factorial.getFactorial(n);
|
||||
|
||||
// then
|
||||
BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000");
|
||||
assertThat(factorial, is(equalTo(expectedFactorial)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() {
|
||||
// given
|
||||
Supplier<BigInteger> memoizedSupplier;
|
||||
memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber);
|
||||
|
||||
// when
|
||||
BigInteger expectedValue = new BigInteger("12345");
|
||||
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
|
||||
|
||||
// then
|
||||
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
|
||||
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException {
|
||||
// given
|
||||
Supplier<BigInteger> memoizedSupplier;
|
||||
memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS);
|
||||
|
||||
// when
|
||||
BigInteger expectedValue = new BigInteger("12345");
|
||||
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
|
||||
|
||||
// then
|
||||
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
|
||||
// add one more second until memoized Supplier is evicted from memory
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
|
||||
}
|
||||
|
||||
private <T> void assertSupplierGetExecutionResultAndDuration(Supplier<T> supplier,
|
||||
T expectedValue,
|
||||
double expectedDurationInMs) {
|
||||
Instant start = Instant.now();
|
||||
T value = supplier.get();
|
||||
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
|
||||
double marginOfErrorInMs = 100D;
|
||||
|
||||
assertThat(value, is(equalTo(expectedValue)));
|
||||
assertThat(durationInMs.doubleValue(), is(closeTo(expectedDurationInMs, marginOfErrorInMs)));
|
||||
}
|
||||
|
||||
}
|
15
guest/deep-jsf/README.md
Normal file
15
guest/deep-jsf/README.md
Normal file
@ -0,0 +1,15 @@
|
||||
## Building
|
||||
|
||||
To build the module, use Maven's `package` goal:
|
||||
|
||||
```
|
||||
mvn clean package
|
||||
```
|
||||
|
||||
The `war` file will be available at `target/deep-jsf.war`
|
||||
|
||||
## Running
|
||||
|
||||
The `war` application is deployed to a Java EE 7 compliant application server, for example, to GlassFish 4 or later.
|
||||
|
||||
The example then will be accessible at http://localhost:8080/deep-jsf/index.xhtml
|
40
guest/deep-jsf/pom.xml
Normal file
40
guest/deep-jsf/pom.xml
Normal file
@ -0,0 +1,40 @@
|
||||
<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>
|
||||
|
||||
<groupId>com.stackify</groupId>
|
||||
<artifactId>deep-jsf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>7.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>deep-jsf</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.stackify.deepjsf;
|
||||
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.RequestScoped;
|
||||
|
||||
@ManagedBean
|
||||
@RequestScoped
|
||||
public class GreetControllerBean {
|
||||
|
||||
public String greet() {
|
||||
return "greet";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.stackify.deepjsf;
|
||||
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.RequestScoped;
|
||||
import javax.faces.component.UIComponent;
|
||||
import javax.faces.component.UIViewRoot;
|
||||
import javax.faces.component.visit.VisitContext;
|
||||
import javax.faces.component.visit.VisitResult;
|
||||
import javax.faces.event.PhaseEvent;
|
||||
import javax.faces.event.PhaseId;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@ManagedBean
|
||||
@RequestScoped
|
||||
public class PhaseListenerBean {
|
||||
|
||||
public void beforeListener(PhaseEvent event) {
|
||||
if (!event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
|
||||
return;
|
||||
}
|
||||
UIViewRoot root = event.getFacesContext().getViewRoot();
|
||||
|
||||
boolean showNewFeature = showNewFeatureForIp(event);
|
||||
|
||||
processComponentTree(root, event, showNewFeature);
|
||||
}
|
||||
|
||||
private boolean showNewFeatureForIp(PhaseEvent event) {
|
||||
HttpServletRequest request = (HttpServletRequest) event.getFacesContext()
|
||||
.getExternalContext().getRequest();
|
||||
String ip = request.getRemoteAddr();
|
||||
return !ip.startsWith("127.0");
|
||||
}
|
||||
|
||||
private void processComponentTree(UIComponent component, PhaseEvent event, boolean show) {
|
||||
component.visitTree(VisitContext.createVisitContext(event.getFacesContext()),
|
||||
(context, target) -> {
|
||||
if (target.getId() != null
|
||||
&& target.getId().startsWith("new-feature-")
|
||||
&& !show) {
|
||||
target.setRendered(false);
|
||||
}
|
||||
return VisitResult.ACCEPT;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.stackify.deepjsf;
|
||||
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.SessionScoped;
|
||||
import javax.faces.event.ValueChangeEvent;
|
||||
|
||||
@ManagedBean
|
||||
@SessionScoped
|
||||
public class UserBean {
|
||||
|
||||
private String name = "";
|
||||
|
||||
private String lastName = "";
|
||||
|
||||
private String proposedLogin = "";
|
||||
|
||||
public void nameChanged(ValueChangeEvent event) {
|
||||
this.proposedLogin = event.getNewValue() + "-" + lastName;
|
||||
}
|
||||
|
||||
public void lastNameChanged(ValueChangeEvent event) {
|
||||
this.proposedLogin = name + "-" + event.getNewValue();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getProposedLogin() {
|
||||
return proposedLogin;
|
||||
}
|
||||
|
||||
public void setProposedLogin(String proposedLogin) {
|
||||
this.proposedLogin = proposedLogin;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.stackify.deepjsf;
|
||||
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.RequestScoped;
|
||||
|
||||
@ManagedBean
|
||||
@RequestScoped
|
||||
public class UserControllerBean {
|
||||
|
||||
public String register() {
|
||||
return "register-success";
|
||||
}
|
||||
|
||||
}
|
15
guest/deep-jsf/src/main/webapp/WEB-INF/faces-config.xml
Normal file
15
guest/deep-jsf/src/main/webapp/WEB-INF/faces-config.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version='1.0' encoding='UTF-8'?>
|
||||
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
|
||||
|
||||
<navigation-rule>
|
||||
<from-view-id>/register.xhtml</from-view-id>
|
||||
<navigation-case>
|
||||
<from-outcome>register-success</from-outcome>
|
||||
<to-view-id>/hello.xhtml</to-view-id>
|
||||
</navigation-case>
|
||||
</navigation-rule>
|
||||
|
||||
</faces-config>
|
11
guest/deep-jsf/src/main/webapp/greet.xhtml
Normal file
11
guest/deep-jsf/src/main/webapp/greet.xhtml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:h="http://xmlns.jcp.org/jsf/html"
|
||||
xmlns:f="http://xmlns.jcp.org/jsf/core">
|
||||
<f:view beforePhase="#{phaseListenerBean.beforeListener}">
|
||||
<h:outputLabel value="Hello, #{userBean.name}"/>
|
||||
<h:outputLabel id="new-feature-last-name" value=" #{userBean.lastName}"/>
|
||||
</f:view>
|
||||
</html>
|
10
guest/deep-jsf/src/main/webapp/hello.xhtml
Normal file
10
guest/deep-jsf/src/main/webapp/hello.xhtml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:h="http://xmlns.jcp.org/jsf/html"
|
||||
xmlns:f="http://xmlns.jcp.org/jsf/core">
|
||||
<f:view>
|
||||
<h:outputLabel value="Hello, #{userBean.name} #{userBean.lastName}! Your login is: #{userBean.proposedLogin}"/>
|
||||
</f:view>
|
||||
</html>
|
18
guest/deep-jsf/src/main/webapp/index.xhtml
Normal file
18
guest/deep-jsf/src/main/webapp/index.xhtml
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:h="http://xmlns.jcp.org/jsf/html"
|
||||
xmlns:f="http://xmlns.jcp.org/jsf/core">
|
||||
<f:view beforePhase="#{phaseListenerBean.beforeListener}">
|
||||
<h:form>
|
||||
<h:panelGrid columns="2">
|
||||
<h:outputLabel value="First Name:"/>
|
||||
<h:inputText id="name" value="#{userBean.name}"/>
|
||||
<h:outputLabel id="new-feature-last-name-label" value="Last Name:"/>
|
||||
<h:inputText id="new-feature-last-name" value="#{userBean.lastName}"/>
|
||||
<h:commandButton value="Submit" action="#{greetControllerBean.greet}"/>
|
||||
</h:panelGrid>
|
||||
</h:form>
|
||||
</f:view>
|
||||
</html>
|
31
guest/deep-jsf/src/main/webapp/register.xhtml
Normal file
31
guest/deep-jsf/src/main/webapp/register.xhtml
Normal file
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:h="http://xmlns.jcp.org/jsf/html"
|
||||
xmlns:f="http://xmlns.jcp.org/jsf/core">
|
||||
|
||||
<h:head>
|
||||
<h:outputScript library="javax.faces" name="jsf.js"/>
|
||||
</h:head>
|
||||
|
||||
<f:view>
|
||||
<h:form>
|
||||
<h:panelGrid columns="2">
|
||||
<h:outputLabel value="First Name:"/>
|
||||
<h:inputText id="name" value="#{userBean.name}"
|
||||
valueChangeListener="#{userBean.nameChanged}">
|
||||
<f:ajax event="change" execute="@this" render="proposed-login"/>
|
||||
</h:inputText>
|
||||
<h:outputLabel id="lastname-label" value="Last Name:"/>
|
||||
<h:inputText id="lastname" value="#{userBean.lastName}"
|
||||
valueChangeListener="#{userBean.lastNameChanged}">
|
||||
<f:ajax event="change" execute="@this" render="proposed-login"/>
|
||||
</h:inputText>
|
||||
<h:outputLabel id="login-label" value="Proposed Login:"/>
|
||||
<h:inputText id="proposed-login" disabled="true" value="#{userBean.proposedLogin}"/>
|
||||
<h:commandButton value="Submit" action="#{userControllerBean.register}"/>
|
||||
</h:panelGrid>
|
||||
</h:form>
|
||||
</f:view>
|
||||
</html>
|
@ -5,3 +5,5 @@
|
||||
- [Hibernate – Mapping Date and Time](http://www.baeldung.com/hibernate-date-time)
|
||||
- [Hibernate Inheritance Mapping](http://www.baeldung.com/hibernate-inheritance)
|
||||
- [A Guide to Multitenancy in Hibernate 5](http://www.baeldung.com/hibernate-5-multitenancy)
|
||||
- [Introduction to Hibernate Spatial](http://www.baeldung.com/hibernate-spatial)
|
||||
- [Hibernate Interceptors](http://www.baeldung.com/hibernate-interceptor)
|
||||
|
@ -81,6 +81,7 @@ public class HibernateUtil {
|
||||
metadataSources.addAnnotatedClass(Bag.class);
|
||||
metadataSources.addAnnotatedClass(PointEntity.class);
|
||||
metadataSources.addAnnotatedClass(PolygonEntity.class);
|
||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
|
||||
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.baeldung.hibernate.converters;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
|
||||
import com.baeldung.hibernate.pojo.PersonName;
|
||||
|
||||
@Converter
|
||||
public class PersonNameConverter implements AttributeConverter<PersonName, String> {
|
||||
|
||||
private static final String SEPARATOR = ", ";
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(PersonName person) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (person.getSurname() != null) {
|
||||
sb.append(person.getSurname());
|
||||
sb.append(SEPARATOR);
|
||||
}
|
||||
|
||||
if (person.getName() != null) {
|
||||
sb.append(person.getName());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersonName convertToEntityAttribute(String dbPerson) {
|
||||
String[] pieces = dbPerson.split(SEPARATOR);
|
||||
|
||||
if (pieces == null || pieces.length != 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PersonName personName = new PersonName();
|
||||
personName.setSurname(pieces[0]);
|
||||
personName.setName(pieces[1]);
|
||||
|
||||
return personName;
|
||||
}
|
||||
|
||||
}
|
@ -9,7 +9,7 @@ import org.hibernate.Interceptor;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.type.Type;
|
||||
|
||||
public class CustomInterceptorImpl implements Interceptor {
|
||||
public class CustomInterceptorImpl implements Interceptor, Serializable {
|
||||
|
||||
@Override
|
||||
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException {
|
||||
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.hibernate.pojo;
|
||||
|
||||
import javax.persistence.Convert;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import com.baeldung.hibernate.converters.PersonNameConverter;
|
||||
|
||||
@Entity(name = "PersonTable")
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Convert(converter = PersonNameConverter.class)
|
||||
private PersonName personName;
|
||||
|
||||
public PersonName getPersonName() {
|
||||
return personName;
|
||||
}
|
||||
|
||||
public void setPersonName(PersonName personName) {
|
||||
this.personName = personName;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.hibernate.pojo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class PersonName implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7883094644631050150L;
|
||||
|
||||
private String name;
|
||||
|
||||
private String surname;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.baeldung.hibernate.converter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.pojo.Person;
|
||||
import com.baeldung.hibernate.pojo.PersonName;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PersonNameConverterTest {
|
||||
|
||||
private Session session;
|
||||
private Transaction transaction;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
session = HibernateUtil.getSessionFactory()
|
||||
.openSession();
|
||||
transaction = session.beginTransaction();
|
||||
|
||||
session.createNativeQuery("delete from personTable")
|
||||
.executeUpdate();
|
||||
|
||||
transaction.commit();
|
||||
transaction = session.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
transaction.rollback();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonName_WhenSaving_ThenNameAndSurnameConcat() {
|
||||
final String name = "name";
|
||||
final String surname = "surname";
|
||||
|
||||
PersonName personName = new PersonName();
|
||||
personName.setName(name);
|
||||
personName.setSurname(surname);
|
||||
|
||||
Person person = new Person();
|
||||
person.setPersonName(personName);
|
||||
|
||||
Long id = (Long) session.save(person);
|
||||
|
||||
session.flush();
|
||||
session.clear();
|
||||
|
||||
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
|
||||
.setParameter("id", id)
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals(surname + ", " + name, dbPersonName);
|
||||
|
||||
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
|
||||
.setParameter("id", id)
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals(dbPerson.getPersonName()
|
||||
.getName(), name);
|
||||
assertEquals(dbPerson.getPersonName()
|
||||
.getSurname(), surname);
|
||||
}
|
||||
|
||||
}
|
@ -8,7 +8,6 @@ import org.influxdb.dto.*;
|
||||
import org.influxdb.impl.InfluxDBResultMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -55,7 +54,7 @@ public class InfluxDBConnectionLiveTest {
|
||||
|
||||
InfluxDB connection = connectDatabase();
|
||||
|
||||
// Create "baeldung and check for it
|
||||
// Create "baeldung" and check for it
|
||||
connection.createDatabase("baeldung");
|
||||
assertTrue(connection.databaseExists("baeldung"));
|
||||
|
||||
@ -103,7 +102,7 @@ public class InfluxDBConnectionLiveTest {
|
||||
// another brief pause.
|
||||
Thread.sleep(10);
|
||||
|
||||
List<MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
|
||||
List<com.baeldung.influxdb.MemoryPoint> memoryPointList = getPoints(connection, "Select * from memory", "baeldung");
|
||||
|
||||
assertEquals(10, memoryPointList.size());
|
||||
|
||||
|
20
java-rmi/pom.xml
Normal file
20
java-rmi/pom.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<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>
|
||||
|
||||
<groupId>com.baeldung.rmi</groupId>
|
||||
<artifactId>java-rmi</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
36
java-rmi/src/main/java/com/baeldung/rmi/Message.java
Normal file
36
java-rmi/src/main/java/com/baeldung/rmi/Message.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.baeldung.rmi;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Message implements Serializable {
|
||||
|
||||
private String messageText;
|
||||
|
||||
private String contentType;
|
||||
|
||||
public Message() {
|
||||
}
|
||||
|
||||
public Message(String messageText, String contentType) {
|
||||
|
||||
this.messageText = messageText;
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
public String getMessageText() {
|
||||
return messageText;
|
||||
}
|
||||
|
||||
public void setMessageText(String messageText) {
|
||||
this.messageText = messageText;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.rmi;
|
||||
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public interface MessengerService extends Remote {
|
||||
|
||||
public String sendMessage(String clientMessage) throws RemoteException;
|
||||
|
||||
public Message sendMessage(Message clientMessage) throws RemoteException;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.rmi;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
public class MessengerServiceImpl implements MessengerService {
|
||||
|
||||
public String sendMessage(String clientMessage) {
|
||||
|
||||
String serverMessage = null;
|
||||
if (clientMessage.equals("Client Message")) {
|
||||
serverMessage = "Server Message";
|
||||
}
|
||||
|
||||
return serverMessage;
|
||||
}
|
||||
|
||||
public void createStubAndBind() throws RemoteException {
|
||||
|
||||
MessengerService stub = (MessengerService) UnicastRemoteObject.exportObject((MessengerService) this, 0);
|
||||
Registry registry = LocateRegistry.createRegistry(1099);
|
||||
registry.rebind("MessengerService", stub);
|
||||
}
|
||||
|
||||
public Message sendMessage(Message clientMessage) throws RemoteException {
|
||||
|
||||
Message serverMessage = null;
|
||||
if (clientMessage.getMessageText().equals("Client Message")) {
|
||||
serverMessage = new Message("Server Message", "text/plain");
|
||||
}
|
||||
|
||||
return serverMessage;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.baeldung.rmi;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaRMIIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void whenRunServer_thenServerStarts() {
|
||||
|
||||
try {
|
||||
MessengerServiceImpl server = new MessengerServiceImpl();
|
||||
server.createStubAndBind();
|
||||
} catch (RemoteException e) {
|
||||
fail("Exception Occurred");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() {
|
||||
|
||||
try {
|
||||
Registry registry = LocateRegistry.getRegistry();
|
||||
MessengerService server = (MessengerService) registry.lookup("MessengerService");
|
||||
String responseMessage = server.sendMessage("Client Message");
|
||||
|
||||
String expectedMessage = "Server Message";
|
||||
assertEquals(responseMessage, expectedMessage);
|
||||
} catch (RemoteException e) {
|
||||
fail("Exception Occurred");
|
||||
} catch (NotBoundException nb) {
|
||||
fail("Exception Occurred");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -6,10 +6,11 @@
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<validation-api.version>2.0.0.Final</validation-api.version>
|
||||
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
||||
<validation-api.version>2.0.1.Final</validation-api.version>
|
||||
<hibernate-validator.version>6.0.7.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||
<javax.el.version>2.2.6</javax.el.version>
|
||||
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@ -21,12 +22,6 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${validation-api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
@ -50,6 +45,16 @@
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>${javax.el.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,38 @@
|
||||
package org.baeldung.javaxval.methodvalidation;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Customer;
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan({ "org.baeldung.javaxval.methodvalidation.model" })
|
||||
public class MethodValidationConfig {
|
||||
|
||||
@Bean
|
||||
public MethodValidationPostProcessor methodValidationPostProcessor() {
|
||||
return new MethodValidationPostProcessor();
|
||||
}
|
||||
|
||||
@Bean("customer")
|
||||
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
public Customer customer(String firstName, String lastName) {
|
||||
|
||||
Customer customer = new Customer(firstName, lastName);
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Bean("reservation")
|
||||
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
public Reservation reservation(LocalDate begin, LocalDate end, Customer customer, int room) {
|
||||
|
||||
Reservation reservation = new Reservation(begin, end, customer, room);
|
||||
return reservation;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import javax.validation.constraintvalidation.SupportedValidationTarget;
|
||||
import javax.validation.constraintvalidation.ValidationTarget;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@SupportedValidationTarget(ValidationTarget.PARAMETERS)
|
||||
public class ConsistentDateParameterValidator implements ConstraintValidator<ConsistentDateParameters, Object[]> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(Object[] value, ConstraintValidatorContext context) {
|
||||
|
||||
if (value[0] == null || value[1] == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(value[0] instanceof LocalDate) || !(value[1] instanceof LocalDate)) {
|
||||
throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate.");
|
||||
}
|
||||
|
||||
return ((LocalDate) value[0]).isAfter(LocalDate.now()) && ((LocalDate) value[0]).isBefore((LocalDate) value[1]);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Constraint(validatedBy = ConsistentDateParameterValidator.class)
|
||||
@Target({ METHOD, CONSTRUCTOR })
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
public @interface ConsistentDateParameters {
|
||||
|
||||
String message() default "End date must be after begin date and both must be in the future";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.CONSTRUCTOR;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Constraint(validatedBy = ValidReservationValidator.class)
|
||||
@Target({ METHOD, CONSTRUCTOR })
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
public @interface ValidReservation {
|
||||
|
||||
String message() default "End date must be after begin date and both must be in the future, room number must be bigger than 0";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package org.baeldung.javaxval.methodvalidation.constraints;
|
||||
|
||||
import org.baeldung.javaxval.methodvalidation.model.Reservation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class ValidReservationValidator implements ConstraintValidator<ValidReservation, Reservation> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(Reservation reservation, ConstraintValidatorContext context) {
|
||||
|
||||
if (reservation == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(reservation instanceof Reservation)) {
|
||||
throw new IllegalArgumentException("Illegal method signature, expected parameter of type Reservation.");
|
||||
}
|
||||
|
||||
if (reservation.getBegin() == null || reservation.getEnd() == null || reservation.getCustomer() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (reservation.getBegin()
|
||||
.isAfter(LocalDate.now())
|
||||
&& reservation.getBegin()
|
||||
.isBefore(reservation.getEnd())
|
||||
&& reservation.getRoom() > 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.baeldung.javaxval.methodvalidation.model;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Validated
|
||||
public class Customer {
|
||||
|
||||
@Size(min = 5, max = 200)
|
||||
private String firstName;
|
||||
|
||||
@Size(min = 5, max = 200)
|
||||
private String lastName;
|
||||
|
||||
public Customer(@Size(min = 5, max = 200) @NotNull String firstName, @Size(min = 5, max = 200) @NotNull String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Customer() {
|
||||
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user