Merge branch 'master' into BAEL-1559

This commit is contained in:
Bruno Mattos Torrão 2018-02-25 16:00:40 -03:00 committed by GitHub
commit e7b899b233
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
490 changed files with 16978 additions and 459 deletions

1
.gitignore vendored
View File

@ -43,3 +43,4 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
*.springBeans
20171220-JMeter.csv

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.algorithms.multiswarm;
/**
* Constants used by the Multi-swarm optimization algorithms.
*
* @author Donato Rimenti
*
*/
public class Constants {
/**
* The inertia factor encourages a particle to continue moving in its
* current direction.
*/
public static final double INERTIA_FACTOR = 0.729;
/**
* The cognitive weight encourages a particle to move toward its historical
* best-known position.
*/
public static final double COGNITIVE_WEIGHT = 1.49445;
/**
* The social weight encourages a particle to move toward the best-known
* position found by any of the particles swarm-mates.
*/
public static final double SOCIAL_WEIGHT = 1.49445;
/**
* The global weight encourages a particle to move toward the best-known
* position found by any particle in any swarm.
*/
public static final double GLOBAL_WEIGHT = 0.3645;
/**
* Upper bound for the random generation. We use it to reduce the
* computation time since we can rawly estimate it.
*/
public static final int PARTICLE_UPPER_BOUND = 10000000;
/**
* Private constructor for utility class.
*/
private Constants() {
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.algorithms.multiswarm;
/**
* Interface for a fitness function, used to decouple the main algorithm logic
* from the specific problem solution.
*
* @author Donato Rimenti
*
*/
public interface FitnessFunction {
/**
* Returns the fitness of a particle given its position.
*
* @param particlePosition
* the position of the particle
* @return the fitness of the particle
*/
public double getFitness(long[] particlePosition);
}

View File

@ -0,0 +1,227 @@
package com.baeldung.algorithms.multiswarm;
import java.util.Arrays;
import java.util.Random;
/**
* Represents a collection of {@link Swarm}.
*
* @author Donato Rimenti
*
*/
public class Multiswarm {
/**
* The swarms managed by this multiswarm.
*/
private Swarm[] swarms;
/**
* The best position found within all the {@link #swarms}.
*/
private long[] bestPosition;
/**
* The best fitness score found within all the {@link #swarms}.
*/
private double bestFitness = Double.NEGATIVE_INFINITY;
/**
* A random generator.
*/
private Random random = new Random();
/**
* The fitness function used to determine how good is a particle.
*/
private FitnessFunction fitnessFunction;
/**
* Instantiates a new Multiswarm.
*
* @param numSwarms
* the number of {@link #swarms}
* @param particlesPerSwarm
* the number of particle for each {@link #swarms}
* @param fitnessFunction
* the {@link #fitnessFunction}
*/
public Multiswarm(int numSwarms, int particlesPerSwarm, FitnessFunction fitnessFunction) {
this.fitnessFunction = fitnessFunction;
this.swarms = new Swarm[numSwarms];
for (int i = 0; i < numSwarms; i++) {
swarms[i] = new Swarm(particlesPerSwarm);
}
}
/**
* Main loop of the algorithm. Iterates all particles of all
* {@link #swarms}. For each particle, computes the new fitness and checks
* if a new best position has been found among itself, the swarm and all the
* swarms and finally updates the particle position and speed.
*/
public void mainLoop() {
for (Swarm swarm : swarms) {
for (Particle particle : swarm.getParticles()) {
long[] particleOldPosition = particle.getPosition().clone();
// Calculate the particle fitness.
particle.setFitness(fitnessFunction.getFitness(particleOldPosition));
// Check if a new best position has been found for the particle
// itself, within the swarm and the multiswarm.
if (particle.getFitness() > particle.getBestFitness()) {
particle.setBestFitness(particle.getFitness());
particle.setBestPosition(particleOldPosition);
if (particle.getFitness() > swarm.getBestFitness()) {
swarm.setBestFitness(particle.getFitness());
swarm.setBestPosition(particleOldPosition);
if (swarm.getBestFitness() > bestFitness) {
bestFitness = swarm.getBestFitness();
bestPosition = swarm.getBestPosition().clone();
}
}
}
// Updates the particle position by adding the speed to the
// actual position.
long[] position = particle.getPosition();
long[] speed = particle.getSpeed();
position[0] += speed[0];
position[1] += speed[1];
// Updates the particle speed.
speed[0] = getNewParticleSpeedForIndex(particle, swarm, 0);
speed[1] = getNewParticleSpeedForIndex(particle, swarm, 1);
}
}
}
/**
* Computes a new speed for a given particle of a given swarm on a given
* axis. The new speed is computed using the formula:
*
* <pre>
* ({@link Constants#INERTIA_FACTOR} * {@link Particle#getSpeed()}) +
* (({@link Constants#COGNITIVE_WEIGHT} * random(0,1)) * ({@link Particle#getBestPosition()} - {@link Particle#getPosition()})) +
* (({@link Constants#SOCIAL_WEIGHT} * random(0,1)) * ({@link Swarm#getBestPosition()} - {@link Particle#getPosition()})) +
* (({@link Constants#GLOBAL_WEIGHT} * random(0,1)) * ({@link #bestPosition} - {@link Particle#getPosition()}))
* </pre>
*
* @param particle
* the particle whose new speed needs to be computed
* @param swarm
* the swarm which contains the particle
* @param index
* the index of the particle axis whose speeds needs to be
* computed
* @return the new speed of the particle passed on the given axis
*/
private int getNewParticleSpeedForIndex(Particle particle, Swarm swarm, int index) {
return (int) ((Constants.INERTIA_FACTOR * particle.getSpeed()[index])
+ (randomizePercentage(Constants.COGNITIVE_WEIGHT)
* (particle.getBestPosition()[index] - particle.getPosition()[index]))
+ (randomizePercentage(Constants.SOCIAL_WEIGHT)
* (swarm.getBestPosition()[index] - particle.getPosition()[index]))
+ (randomizePercentage(Constants.GLOBAL_WEIGHT)
* (bestPosition[index] - particle.getPosition()[index])));
}
/**
* Returns a random number between 0 and the value passed as argument.
*
* @param value
* the value to randomize
* @return a random value between 0 and the one passed as argument
*/
private double randomizePercentage(double value) {
return random.nextDouble() * value;
}
/**
* Gets the {@link #bestPosition}.
*
* @return the {@link #bestPosition}
*/
public long[] getBestPosition() {
return bestPosition;
}
/**
* Gets the {@link #bestFitness}.
*
* @return the {@link #bestFitness}
*/
public double getBestFitness() {
return bestFitness;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(bestFitness);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + Arrays.hashCode(bestPosition);
result = prime * result + ((fitnessFunction == null) ? 0 : fitnessFunction.hashCode());
result = prime * result + ((random == null) ? 0 : random.hashCode());
result = prime * result + Arrays.hashCode(swarms);
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Multiswarm other = (Multiswarm) obj;
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
return false;
if (!Arrays.equals(bestPosition, other.bestPosition))
return false;
if (fitnessFunction == null) {
if (other.fitnessFunction != null)
return false;
} else if (!fitnessFunction.equals(other.fitnessFunction))
return false;
if (random == null) {
if (other.random != null)
return false;
} else if (!random.equals(other.random))
return false;
if (!Arrays.equals(swarms, other.swarms))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Multiswarm [swarms=" + Arrays.toString(swarms) + ", bestPosition=" + Arrays.toString(bestPosition)
+ ", bestFitness=" + bestFitness + ", random=" + random + ", fitnessFunction=" + fitnessFunction + "]";
}
}

View File

@ -0,0 +1,204 @@
package com.baeldung.algorithms.multiswarm;
import java.util.Arrays;
/**
* Represents a particle, the basic component of a {@link Swarm}.
*
* @author Donato Rimenti
*
*/
public class Particle {
/**
* The current position of this particle.
*/
private long[] position;
/**
* The speed of this particle.
*/
private long[] speed;
/**
* The fitness of this particle for the current position.
*/
private double fitness;
/**
* The best position found by this particle.
*/
private long[] bestPosition;
/**
* The best fitness found by this particle.
*/
private double bestFitness = Double.NEGATIVE_INFINITY;
/**
* Instantiates a new Particle.
*
* @param initialPosition
* the initial {@link #position}
* @param initialSpeed
* the initial {@link #speed}
*/
public Particle(long[] initialPosition, long[] initialSpeed) {
this.position = initialPosition;
this.speed = initialSpeed;
}
/**
* Gets the {@link #position}.
*
* @return the {@link #position}
*/
public long[] getPosition() {
return position;
}
/**
* Gets the {@link #speed}.
*
* @return the {@link #speed}
*/
public long[] getSpeed() {
return speed;
}
/**
* Gets the {@link #fitness}.
*
* @return the {@link #fitness}
*/
public double getFitness() {
return fitness;
}
/**
* Gets the {@link #bestPosition}.
*
* @return the {@link #bestPosition}
*/
public long[] getBestPosition() {
return bestPosition;
}
/**
* Gets the {@link #bestFitness}.
*
* @return the {@link #bestFitness}
*/
public double getBestFitness() {
return bestFitness;
}
/**
* Sets the {@link #position}.
*
* @param position
* the new {@link #position}
*/
public void setPosition(long[] position) {
this.position = position;
}
/**
* Sets the {@link #speed}.
*
* @param speed
* the new {@link #speed}
*/
public void setSpeed(long[] speed) {
this.speed = speed;
}
/**
* Sets the {@link #fitness}.
*
* @param fitness
* the new {@link #fitness}
*/
public void setFitness(double fitness) {
this.fitness = fitness;
}
/**
* Sets the {@link #bestPosition}.
*
* @param bestPosition
* the new {@link #bestPosition}
*/
public void setBestPosition(long[] bestPosition) {
this.bestPosition = bestPosition;
}
/**
* Sets the {@link #bestFitness}.
*
* @param bestFitness
* the new {@link #bestFitness}
*/
public void setBestFitness(double bestFitness) {
this.bestFitness = bestFitness;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(bestFitness);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + Arrays.hashCode(bestPosition);
temp = Double.doubleToLongBits(fitness);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + Arrays.hashCode(position);
result = prime * result + Arrays.hashCode(speed);
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Particle other = (Particle) obj;
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
return false;
if (!Arrays.equals(bestPosition, other.bestPosition))
return false;
if (Double.doubleToLongBits(fitness) != Double.doubleToLongBits(other.fitness))
return false;
if (!Arrays.equals(position, other.position))
return false;
if (!Arrays.equals(speed, other.speed))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Particle [position=" + Arrays.toString(position) + ", speed=" + Arrays.toString(speed) + ", fitness="
+ fitness + ", bestPosition=" + Arrays.toString(bestPosition) + ", bestFitness=" + bestFitness + "]";
}
}

View File

@ -0,0 +1,155 @@
package com.baeldung.algorithms.multiswarm;
import java.util.Arrays;
import java.util.Random;
/**
* Represents a collection of {@link Particle}.
*
* @author Donato Rimenti
*
*/
public class Swarm {
/**
* The particles of this swarm.
*/
private Particle[] particles;
/**
* The best position found within the particles of this swarm.
*/
private long[] bestPosition;
/**
* The best fitness score found within the particles of this swarm.
*/
private double bestFitness = Double.NEGATIVE_INFINITY;
/**
* A random generator.
*/
private Random random = new Random();
/**
* Instantiates a new Swarm.
*
* @param numParticles
* the number of particles of the swarm
*/
public Swarm(int numParticles) {
particles = new Particle[numParticles];
for (int i = 0; i < numParticles; i++) {
long[] initialParticlePosition = { random.nextInt(Constants.PARTICLE_UPPER_BOUND),
random.nextInt(Constants.PARTICLE_UPPER_BOUND) };
long[] initialParticleSpeed = { random.nextInt(Constants.PARTICLE_UPPER_BOUND),
random.nextInt(Constants.PARTICLE_UPPER_BOUND) };
particles[i] = new Particle(initialParticlePosition, initialParticleSpeed);
}
}
/**
* Gets the {@link #particles}.
*
* @return the {@link #particles}
*/
public Particle[] getParticles() {
return particles;
}
/**
* Gets the {@link #bestPosition}.
*
* @return the {@link #bestPosition}
*/
public long[] getBestPosition() {
return bestPosition;
}
/**
* Gets the {@link #bestFitness}.
*
* @return the {@link #bestFitness}
*/
public double getBestFitness() {
return bestFitness;
}
/**
* Sets the {@link #bestPosition}.
*
* @param bestPosition
* the new {@link #bestPosition}
*/
public void setBestPosition(long[] bestPosition) {
this.bestPosition = bestPosition;
}
/**
* Sets the {@link #bestFitness}.
*
* @param bestFitness
* the new {@link #bestFitness}
*/
public void setBestFitness(double bestFitness) {
this.bestFitness = bestFitness;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(bestFitness);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + Arrays.hashCode(bestPosition);
result = prime * result + Arrays.hashCode(particles);
result = prime * result + ((random == null) ? 0 : random.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Swarm other = (Swarm) obj;
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
return false;
if (!Arrays.equals(bestPosition, other.bestPosition))
return false;
if (!Arrays.equals(particles, other.particles))
return false;
if (random == null) {
if (other.random != null)
return false;
} else if (!random.equals(other.random))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Swarm [particles=" + Arrays.toString(particles) + ", bestPosition=" + Arrays.toString(bestPosition)
+ ", bestFitness=" + bestFitness + ", random=" + random + "]";
}
}

View File

@ -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 row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
if (board[row][column] == NO_VALUE) {
for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
board[row][column] = k;
if (isValid(board, row, column) && solve(board)) {
return true;
}
board[row][column] = NO_VALUE;
}
return false;
}
}
}
return true;
}
private boolean isValid(int[][] board, int row, int column) {
return rowConstraint(board, row) &&
columnConstraint(board, column) &&
subsectionConstraint(board, row, column);
}
private boolean subsectionConstraint(int[][] board, int row, int column) {
boolean[] constraint = new boolean[BOARD_SIZE];
int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
if (!checkConstraint(board, r, constraint, c)) return false;
}
}
return true;
}
private boolean columnConstraint(int[][] board, int column) {
boolean[] constraint = new boolean[BOARD_SIZE];
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
.allMatch(row -> checkConstraint(board, row, constraint, column));
}
private boolean rowConstraint(int[][] board, int row) {
boolean[] constraint = new boolean[BOARD_SIZE];
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
.allMatch(column -> checkConstraint(board, row, constraint, column));
}
private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) {
if (board[row][column] != NO_VALUE) {
if (!constraint[board[row][column] - 1]) {
constraint[board[row][column] - 1] = true;
} else {
return false;
}
}
return true;
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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 column, int num) {
return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 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 row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) {
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) {
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) {
for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) {
int index = getIndex(row + rowDelta, column + columnDelta, n);
coverBoard[index][hBase] = true;
}
}
}
}
}
return hBase;
}
private int checkColumnConstraint(boolean[][] coverBoard, int hBase) {
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
int index = getIndex(row, column, n);
coverBoard[index][hBase] = true;
}
}
}
return hBase;
}
private int checkRowConstraint(boolean[][] coverBoard, int hBase) {
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
int index = getIndex(row, column, n);
coverBoard[index][hBase] = true;
}
}
}
return hBase;
}
private int checkCellConstraint(boolean[][] coverBoard, int hBase) {
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) {
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
int index = getIndex(row, column, n);
coverBoard[index][hBase] = true;
}
}
}
return hBase;
}
private boolean[][] initializeExactCoverBoard(int[][] board) {
boolean[][] coverBoard = createExactCoverBoard();
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
int n = board[row - 1][column - 1];
if (n != NO_VALUE) {
for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
if (num != n) {
Arrays.fill(coverBoard[getIndex(row, column, num)], false);
}
}
}
}
}
return coverBoard;
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.algorithms.sudoku;
class DancingNode {
DancingNode L, R, U, D;
ColumnNode C;
DancingNode hookDown(DancingNode node) {
assert (this.C == node.C);
node.D = this.D;
node.D.U = node;
node.U = this;
this.D = node;
return node;
}
DancingNode hookRight(DancingNode node) {
node.R = this.R;
node.R.L = node;
node.L = this;
this.R = node;
return node;
}
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;
}
}

View File

@ -0,0 +1,12 @@
S ########
# #
# ### ## #
# # # #
# # # # #
# ## #####
# # #
# # # # #
##### ####
# # E
# # # #
##########

View File

@ -0,0 +1,22 @@
S ##########################
# # # #
# # #### ############### #
# # # # # #
# # #### # # ###############
# # # # # # #
# # # #### ### ########### #
# # # # # #
# ################## #
######### # # # # #
# # #### # ####### # #
# # ### ### # # # # #
# # ## # ##### # #
##### ####### # # # # #
# # ## ## #### # #
# ##### ####### # #
# # ############
####### ######### # #
# # ######## #
# ####### ###### ## # E
# # # ## #
############################

View File

@ -0,0 +1,52 @@
package com.baeldung.algorithms.multiswarm;
/**
* Specific fitness function implementation to solve the League of Legends
* problem. This is the problem statement: <br>
* <br>
* In League of Legends, a player's Effective Health when defending against
* physical damage is given by E=H(100+A)/100, where H is health and A is armor.
* Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You have
* 3600 gold, and you need to optimize the effectiveness E of your health and
* armor to survive as long as possible against the enemy team's attacks. How
* much of each should you buy? <br>
* <br>
*
* @author Donato Rimenti
*
*/
public class LolFitnessFunction implements FitnessFunction {
/*
* (non-Javadoc)
*
* @see
* com.baeldung.algorithms.multiswarm.FitnessFunction#getFitness(long[])
*/
@Override
public double getFitness(long[] particlePosition) {
long health = particlePosition[0];
long armor = particlePosition[1];
// No negatives values accepted.
if (health < 0 && armor < 0) {
return -(health * armor);
} else if (health < 0) {
return health;
} else if (armor < 0) {
return armor;
}
// Checks if the solution is actually feasible provided our gold.
double cost = (health * 2.5) + (armor * 18);
if (cost > 3600) {
return 3600 - cost;
} else {
// Check how good is the solution.
long fitness = (health * (100 + armor)) / 100;
return fitness;
}
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.algorithms.multiswarm;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import com.baeldung.algorithms.support.MayFailRule;
/**
* Test for {@link Multiswarm}.
*
* @author Donato Rimenti
*
*/
public class MultiswarmUnitTest {
/**
* Rule for handling expected failures. We use this since this test may
* actually fail due to bad luck in the random generation.
*/
@Rule
public MayFailRule mayFailRule = new MayFailRule();
/**
* Tests the multiswarm algorithm with a generic problem. The problem is the
* following: <br>
* <br>
* In League of Legends, a player's Effective Health when defending against
* physical damage is given by E=H(100+A)/100, where H is health and A is
* armor. Health costs 2.5 gold per unit, and Armor costs 18 gold per unit.
* You have 3600 gold, and you need to optimize the effectiveness E of your
* health and armor to survive as long as possible against the enemy team's
* attacks. How much of each should you buy? <br>
* <br>
* The solution is H = 1080, A = 50 for a total fitness of 1620. Tested with
* 50 swarms each with 1000 particles.
*/
@Test
public void givenMultiswarm_whenThousandIteration_thenSolutionFound() {
Multiswarm multiswarm = new Multiswarm(50, 1000, new LolFitnessFunction());
// Iterates 1000 times through the main loop and prints the result.
for (int i = 0; i < 1000; i++) {
multiswarm.mainLoop();
}
System.out.println("Best fitness found: " + multiswarm.getBestFitness() + "[" + multiswarm.getBestPosition()[0]
+ "," + multiswarm.getBestPosition()[1] + "]");
Assert.assertEquals(1080, multiswarm.getBestPosition()[0]);
Assert.assertEquals(50, multiswarm.getBestPosition()[1]);
Assert.assertEquals(1620, (int) multiswarm.getBestFitness());
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.algorithms.support;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* JUnit custom rule for managing tests that may fail due to heuristics or
* randomness. In order to use this, just instantiate this object as a public
* field inside the test class and annotate it with {@link Rule}.
*
* @author Donato Rimenti
*
*/
public class MayFailRule implements TestRule {
/*
* (non-Javadoc)
*
* @see org.junit.rules.TestRule#apply(org.junit.runners.model.Statement,
* org.junit.runner.Description)
*/
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (Throwable e) {
// Ignore the exception since we expect this.
}
}
};
}
}

30
apache-zookeeper/pom.xml Normal file
View File

@ -0,0 +1,30 @@
<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>apache-zookeeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.2</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
package com.baeldung.zookeeper.connection;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
public class ZKConnection {
private ZooKeeper zoo;
final CountDownLatch connectionLatch = new CountDownLatch(1);
public ZKConnection() {
}
public ZooKeeper connect(String host) throws IOException, InterruptedException {
zoo = new ZooKeeper(host, 2000, new Watcher() {
public void process(WatchedEvent we) {
if (we.getState() == KeeperState.SyncConnected) {
connectionLatch.countDown();
}
}
});
connectionLatch.await();
return zoo;
}
public void close() throws InterruptedException {
zoo.close();
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.zookeeper.manager;
import org.apache.zookeeper.KeeperException;
public interface ZKManager {
/**
* Create a Znode and save some data
*
* @param path
* @param data
* @throws KeeperException
* @throws InterruptedException
*/
public void create(String path, byte[] data) throws KeeperException, InterruptedException;
/**
* Get ZNode Data
*
* @param path
* @param boolean watchFlag
* @throws KeeperException
* @throws InterruptedException
*/
public Object getZNodeData(String path, boolean watchFlag);
/**
* Update the ZNode Data
*
* @param path
* @param data
* @throws KeeperException
* @throws InterruptedException
*/
public void update(String path, byte[] data) throws KeeperException, InterruptedException, KeeperException;
}

View File

@ -0,0 +1,58 @@
package com.baeldung.zookeeper.manager;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import com.baeldung.zookeeper.connection.ZKConnection;
public class ZKManagerImpl implements ZKManager {
private static ZooKeeper zkeeper;
private static ZKConnection zkConnection;
public ZKManagerImpl() {
initialize();
}
/** * Initialize connection */
private void initialize() {
try {
zkConnection = new ZKConnection();
zkeeper = zkConnection.connect("localhost");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void closeConnection() {
try {
zkConnection.close();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
public void create(String path, byte[] data) throws KeeperException, InterruptedException {
zkeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public Object getZNodeData(String path, boolean watchFlag) {
try {
byte[] b = null;
b = zkeeper.getData(path, null, null);
String data = new String(b, "UTF-8");
System.out.println(data);
return data;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
public void update(String path, byte[] data) throws KeeperException, InterruptedException {
int version = zkeeper.exists(path, true)
.getVersion();
zkeeper.setData(path, data, version);
}
}

114
checker-plugin/pom.xml Normal file
View File

@ -0,0 +1,114 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>checker-plugin</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>checker-plugin</name>
<url>http://maven.apache.org</url>
<!-- https://checkerframework.org/manual/#maven -->
<properties>
<!-- These properties will be set by the Maven Dependency plugin -->
<annotatedJdk>${org.checkerframework:jdk8:jar}</annotatedJdk>
<!-- Uncomment to use the Type Annotations compiler. -->
<!--
<typeAnnotationsJavac>${org.checkerframework:compiler:jar}</typeAnnotationsJavac>
-->
</properties>
<dependencies>
<!-- Annotations from the Checker Framework: nullness, interning, locking, ... -->
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>jdk8</artifactId>
<version>2.3.1</version>
</dependency>
<!-- The Type Annotations compiler. Uncomment if using annotations in comments. -->
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>compiler</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- This plugin will set properties values using dependency information -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<!--
Goal that sets a property pointing to the artifact file for each project dependency.
For each dependency (direct and transitive) a project property will be set which
follows the:
groupId:artifactId:type:[classifier]
form and contains the path to the resolved artifact. -->
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- Uncomment the following line to use the type annotations compiler. -->
<!-- <fork>true</fork> -->
<compilerArguments>
<Xmaxerrs>10000</Xmaxerrs>
<Xmaxwarns>10000</Xmaxwarns>
</compilerArguments>
<annotationProcessors>
<!-- Add all the checkers you want to enable here -->
<annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
<annotationProcessor>org.checkerframework.checker.interning.InterningChecker</annotationProcessor>
<annotationProcessor>org.checkerframework.checker.fenum.FenumChecker</annotationProcessor>
<annotationProcessor>org.checkerframework.checker.formatter.FormatterChecker</annotationProcessor>
<annotationProcessor>org.checkerframework.checker.regex.RegexChecker</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-AprintErrorStack</arg>
<!-- location of the annotated JDK, which comes from a Maven dependency -->
<arg>-Xbootclasspath/p:${annotatedJdk}</arg>
<!--
-->
<!-- Uncomment the following line to use the type annotations compiler. -->
<!--
<arg>-J-Xbootclasspath/p:${typeAnnotationsJavac}</arg>
-->
<!-- Uncomment the following line to turn type-checking warnings into errors. -->
<arg>-Awarns</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,42 @@
package com.baeldung.typechecker;
import org.checkerframework.checker.fenum.qual.Fenum;
//@SuppressWarnings("fenum:assignment.type.incompatible")
public class FakeNumExample {
// Here we use some String constants to represents countries.
static final @Fenum("country") String ITALY = "IT";
static final @Fenum("country") String US = "US";
static final @Fenum("country") String UNITED_KINGDOM = "UK";
// Here we use other String constants to represent planets instead.
static final @Fenum("planet") String MARS = "Mars";
static final @Fenum("planet") String EARTH = "Earth";
static final @Fenum("planet") String VENUS = "Venus";
// Now we write this method and we want to be sure that
// the String parameter has a value of a country, not that is just a String.
void greetCountries(@Fenum("country") String country) {
System.out.println("Hello " + country);
}
// Similarly we're enforcing here that the provided
// parameter is a String that represent a planet.
void greetPlanets(@Fenum("planet") String planet) {
System.out.println("Hello " + planet);
}
public static void main(String[] args) {
FakeNumExample obj = new FakeNumExample();
// This will fail because we pass a planet-String to a method that
// accept a country-String.
obj.greetCountries(MARS);
// Here the opposite happens.
obj.greetPlanets(US);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.typechecker;
public class FormatExample {
public static void main(String[] args) {
// Just enabling org.checkerframework.checker.formatter.FormatterChecker
// we can be sure at compile time that the format strings we pass to format()
// are correct. Normally we would have those errors raised just at runtime.
// All those formats are in fact wrong.
String.format("%y", 7); // error: invalid format string
String.format("%d", "a string"); // error: invalid argument type for %d
String.format("%d %s", 7); // error: missing argument for %s
String.format("%d", 7, 3); // warning: unused argument 3
String.format("{0}", 7); // warning: unused argument 7, because {0} is wrong syntax
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.typechecker;
import org.checkerframework.checker.nullness.qual.KeyFor;
import java.util.HashMap;
import java.util.Map;
public class KeyForExample {
private final Map<String, String> config = new HashMap<>();
KeyForExample() {
// Here we initialize a map to store
// some config data.
config.put("url", "http://1.2.3.4");
config.put("name", "foobaz");
}
public void dumpPort() {
// Here, we want to dump the port value stored in the
// config, so we declare that this key has to be
// present in the config map.
// Obviously that will fail because such key is not present
// in the map.
@KeyFor("config") String key = "port";
System.out.println( config.get(key) );
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.typechecker;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import java.util.Date;
public class MonotonicNotNullExample {
// The idea is we need this field to be to lazily initialized,
// so it starts as null but once it becomes not null
// it cannot return null.
// In these cases, we can use @MonotonicNonNull
@MonotonicNonNull private Date firstCall;
public Date getFirstCall() {
if (firstCall == null) {
firstCall = new Date();
}
return firstCall;
}
public void reset() {
// This is reported as error because
// we wrongly set the field back to null.
firstCall = null;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.typechecker;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public class NonNullExample {
// This method's parameter is annotated in order
// to tell the pluggable type system that it can never be null.
private static int countArgs(@NonNull String[] args) {
return args.length;
}
// This method's parameter is annotated in order
// to tell the pluggable type system that it may be null.
public static void main(@Nullable String[] args) {
// Here lies a potential error,
// because we pass a potential null reference to a method
// that does not accept nulls.
// The Checker Framework will spot this problem at compile time
// instead of runtime.
System.out.println(countArgs(args));
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.typechecker;
import org.checkerframework.checker.regex.qual.Regex;
public class RegexExample {
// For some reason we want to be sure that this regex
// contains at least one capturing group.
// However, we do an error and we forgot to define such
// capturing group in it.
@Regex(1) private static String findNumbers = "\\d*";
public static void main(String[] args) {
String message = "My phone number is +3911223344.";
message.matches(findNumbers);
}
}

4
core-groovy/README.md Normal file
View File

@ -0,0 +1,4 @@
# Groovy
## Relevant articles:

View File

@ -54,7 +54,6 @@
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
@ -126,5 +125,5 @@
<junit.vintage.version>4.12.0</junit.vintage.version>
<junit4.version>4.12</junit4.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,229 @@
package com.baeldung.groovy.sql
import groovy.sql.GroovyResultSet
import groovy.sql.GroovyRowResult
import groovy.sql.Sql
import groovy.transform.CompileStatic
import static org.junit.Assert.*
import org.junit.Test
class SqlTest {
final Map dbConnParams = [url: 'jdbc:hsqldb:mem:testDB', user: 'sa', password: '', driver: 'org.hsqldb.jdbc.JDBCDriver']
@Test
void whenNewSqlInstance_thenDbIsAccessed() {
def sql = Sql.newInstance(dbConnParams)
sql.close()
sql.close()
}
@Test
void whenTableDoesNotExist_thenSelectFails() {
try {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.eachRow('select * from PROJECT') {}
}
fail("An exception should have been thrown")
} catch (ignored) {
//Ok
}
}
@Test
void whenTableCreated_thenSelectIsPossible() {
Sql.withInstance(dbConnParams) { Sql sql ->
def result = sql.execute 'create table PROJECT_1 (id integer not null, name varchar(50), url varchar(100))'
assertEquals(0, sql.updateCount)
assertFalse(result)
result = sql.execute('select * from PROJECT_1')
assertTrue(result)
}
}
@Test
void whenIdentityColumn_thenInsertReturnsNewId() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
def ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
assertEquals(0, ids[0][0])
ids = sql.executeInsert("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
assertEquals(1, ids[0][0])
}
}
@Test
void whenUpdate_thenNumberOfAffectedRows() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
sql.executeInsert("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
def count = sql.executeUpdate("UPDATE PROJECT_3 SET URL = 'https://' + URL")
assertEquals(2, count)
}
}
@Test
void whenEachRow_thenResultSetHasProperties() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
sql.executeInsert("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
sql.eachRow("SELECT * FROM PROJECT_4") { GroovyResultSet rs ->
assertNotNull(rs.name)
assertNotNull(rs.url)
assertNotNull(rs[0])
assertNotNull(rs[1])
assertNotNull(rs[2])
assertEquals(rs.name, rs['name'])
assertEquals(rs.url, rs['url'])
}
}
}
@Test
void whenPagination_thenSubsetIsReturned() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
def rows = sql.rows('SELECT * FROM PROJECT_5 ORDER BY NAME', 1, 1)
assertEquals(1, rows.size())
assertEquals('REST with Spring', rows[0].name)
}
}
@Test
void whenParameters_thenReplacement() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.execute('INSERT INTO PROJECT_6 (NAME, URL) VALUES (?, ?)', 'tutorials', 'github.com/eugenp/tutorials')
sql.execute("INSERT INTO PROJECT_6 (NAME, URL) VALUES (:name, :url)", [name: 'REST with Spring', url: 'github.com/eugenp/REST-With-Spring'])
def rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'tutorials'")
assertEquals(1, rows.size())
rows = sql.rows("SELECT * FROM PROJECT_6 WHERE NAME = 'REST with Spring'")
assertEquals(1, rows.size())
}
}
@Test
void whenParametersInGString_thenReplacement() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${'tutorials'}, ${'github.com/eugenp/tutorials'})"
def name = 'REST with Spring'
def url = 'github.com/eugenp/REST-With-Spring'
sql.execute "INSERT INTO PROJECT_7 (NAME, URL) VALUES (${name}, ${url})"
def rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'tutorials'")
assertEquals(1, rows.size())
rows = sql.rows("SELECT * FROM PROJECT_7 WHERE NAME = 'REST with Spring'")
assertEquals(1, rows.size())
}
}
@Test
void whenTransactionRollback_thenNoDataInserted() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.withTransaction {
sql.execute 'create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
sql.executeInsert("INSERT INTO PROJECT_8 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
sql.rollback()
def rows = sql.rows("SELECT * FROM PROJECT_8")
assertEquals(0, rows.size())
}
}
}
@Test
void whenTransactionRollbackThenCommit_thenOnlyLastInserted() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.withTransaction {
sql.execute 'create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
sql.rollback()
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
sql.rollback()
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
sql.executeInsert("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')")
}
def rows = sql.rows("SELECT * FROM PROJECT_9")
assertEquals(2, rows.size())
}
}
@Test
void whenException_thenTransactionIsRolledBack() {
Sql.withInstance(dbConnParams) { Sql sql ->
try {
sql.withTransaction {
sql.execute 'create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_10 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
throw new Exception('rollback')
}
} catch (ignored) {}
def rows = sql.rows("SELECT * FROM PROJECT_10")
assertEquals(0, rows.size())
}
}
@Test
void givenCachedConnection_whenException_thenDataIsPersisted() {
Sql.withInstance(dbConnParams) { Sql sql ->
try {
sql.cacheConnection {
sql.execute 'create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_11 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')")
throw new Exception('This does not rollback')
}
} catch (ignored) {}
def rows = sql.rows("SELECT * FROM PROJECT_11")
assertEquals(1, rows.size())
}
}
/*@Test
void whenModifyResultSet_thenDataIsChanged() {
Sql.withInstance(dbConnParams) { Sql sql ->
sql.execute 'create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))'
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')")
sql.executeInsert("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')")
sql.eachRow("SELECT * FROM PROJECT_5 FOR UPDATE ") { GroovyResultSet rs ->
rs['name'] = "The great ${rs.name}!" as String
rs.updateRow()
}
sql.eachRow("SELECT * FROM PROJECT_5") { GroovyResultSet rs ->
assertTrue(rs.name.startsWith('The great '))
}
}
}*/
}

View File

@ -40,3 +40,4 @@
- [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)
- [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection)

View File

@ -0,0 +1,70 @@
package com.baeldung.shufflingcollections;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
public class ShufflingCollectionsUnitTest {
@Test
public void whenShufflingList_thenListIsShuffled() {
List<String> students = Arrays.asList("Foo", "Bar", "Baz", "Qux");
System.out.println("List before shuffling:");
System.out.println(students);
Collections.shuffle(students);
System.out.println("List after shuffling:");
System.out.println(students);
}
@Test
public void whenShufflingMapEntries_thenValuesAreShuffled() {
Map<Integer, String> studentsById = new HashMap<>();
studentsById.put(1, "Foo");
studentsById.put(2, "Bar");
studentsById.put(3, "Baz");
studentsById.put(4, "Qux");
System.out.println("Students before shuffling:");
System.out.println(studentsById.values());
List<Map.Entry<Integer, String>> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet());
Collections.shuffle(shuffledStudentEntries);
List<String> shuffledStudents = shuffledStudentEntries.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());
System.out.println("Students after shuffling");
System.out.println(shuffledStudents);
}
@Test
public void whenShufflingSet_thenElementsAreShuffled() {
Set<String> students = new HashSet<>(Arrays.asList("Foo", "Bar", "Baz", "Qux"));
System.out.println("Set before shuffling:");
System.out.println(students);
List<String> studentList = new ArrayList<>(students);
Collections.shuffle(studentList);
System.out.println("Shuffled set elements:");
System.out.println(studentList);
}
@Test
public void whenShufflingWithSameRandomness_thenElementsAreShuffledDeterministically() {
List<String> students_1 = Arrays.asList("Foo", "Bar", "Baz", "Qux");
List<String> students_2 = Arrays.asList("Foo", "Bar", "Baz", "Qux");
Collections.shuffle(students_1, new Random(5));
Collections.shuffle(students_2, new Random(5));
assertThat(students_1).isEqualTo(students_2);
}
}

View File

@ -11,6 +11,7 @@ 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;
@ -116,18 +117,20 @@ public class HttpClientTest {
.GET()
.build();
ExecutorService executorService = Executors.newFixedThreadPool(2);
CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder()
.executor(Executors.newFixedThreadPool(2))
.executor(executorService)
.build()
.sendAsync(request, HttpResponse.BodyHandler.asString());
CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder()
.executor(Executors.newFixedThreadPool(2))
.executor(executorService)
.build()
.sendAsync(request, HttpResponse.BodyHandler.asString());
CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder()
.executor(Executors.newFixedThreadPool(2))
.executor(executorService)
.build()
.sendAsync(request, HttpResponse.BodyHandler.asString());

View File

@ -1,126 +0,0 @@
package com.baeldung.java9.httpclient;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import org.junit.Before;
import org.junit.Test;
public class SimpleHttpRequestsUnitTest {
private URI httpURI;
@Before
public void init() throws URISyntaxException {
httpURI = new URI("http://www.baeldung.com/");
}
@Test
public void quickGet() throws IOException, InterruptedException, URISyntaxException {
HttpRequest request = HttpRequest.create(httpURI).GET();
HttpResponse response = request.response();
int responseStatusCode = response.statusCode();
String responseBody = response.body(HttpResponse.asString());
assertTrue("Get response status code is bigger then 400", responseStatusCode < 400);
}
@Test
public void asynchronousGet() throws URISyntaxException, IOException, InterruptedException, ExecutionException {
HttpRequest request = HttpRequest.create(httpURI).GET();
long before = System.currentTimeMillis();
CompletableFuture<HttpResponse> futureResponse = request.responseAsync();
futureResponse.thenAccept(response -> {
String responseBody = response.body(HttpResponse.asString());
});
HttpResponse resp = futureResponse.get();
HttpHeaders hs = resp.headers();
assertTrue("There should be more then 1 header.", hs.map().size() > 1);
}
@Test
public void postMehtod() throws URISyntaxException, IOException, InterruptedException {
HttpRequest.Builder requestBuilder = HttpRequest.create(httpURI);
requestBuilder.body(HttpRequest.fromString("param1=foo,param2=bar")).followRedirects(HttpClient.Redirect.SECURE);
HttpRequest request = requestBuilder.POST();
HttpResponse response = request.response();
int statusCode = response.statusCode();
assertTrue("HTTP return code", statusCode == HTTP_OK);
}
@Test
public void configureHttpClient() throws NoSuchAlgorithmException, URISyntaxException, IOException, InterruptedException {
CookieManager cManager = new CookieManager();
cManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
SSLParameters sslParam = new SSLParameters(new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String[] { "TLSv1.2" });
HttpClient.Builder hcBuilder = HttpClient.create();
hcBuilder.cookieManager(cManager).sslContext(SSLContext.getDefault()).sslParameters(sslParam);
HttpClient httpClient = hcBuilder.build();
HttpRequest.Builder reqBuilder = httpClient.request(new URI("https://www.facebook.com"));
HttpRequest request = reqBuilder.followRedirects(HttpClient.Redirect.ALWAYS).GET();
HttpResponse response = request.response();
int statusCode = response.statusCode();
assertTrue("HTTP return code", statusCode == HTTP_OK);
}
SSLParameters getDefaultSSLParameters() throws NoSuchAlgorithmException {
SSLParameters sslP1 = SSLContext.getDefault().getSupportedSSLParameters();
String[] proto = sslP1.getApplicationProtocols();
String[] cifers = sslP1.getCipherSuites();
System.out.println(printStringArr(proto));
System.out.println(printStringArr(cifers));
return sslP1;
}
String printStringArr(String... args) {
if (args == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (String s : args) {
sb.append(s);
sb.append("\n");
}
return sb.toString();
}
String printHeaders(HttpHeaders h) {
if (h == null) {
return null;
}
StringBuilder sb = new StringBuilder();
Map<String, List<String>> hMap = h.map();
for (String k : hMap.keySet()) {
sb.append(k).append(":");
List<String> l = hMap.get(k);
if (l != null) {
l.forEach(s -> {
sb.append(s).append(",");
});
}
sb.append("\n");
}
return sb.toString();
}
}

View File

@ -0,0 +1,25 @@
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) {
}
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.concurrent.prioritytaskexecution;
public enum JobPriority {
HIGH,
MEDIUM,
LOW
}

View File

@ -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 =
Executors.newSingleThreadExecutor();
private PriorityBlockingQueue<Job> priorityQueue;
public PriorityJobScheduler(Integer poolSize, Integer queueSize) {
priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize);
priorityQueue = new PriorityBlockingQueue<Job>(queueSize,
Comparator.comparing(Job::getJobPriority));
priorityJobScheduler.execute(()->{
while (true) {
try {
priorityJobPoolExecutor.execute(priorityQueue.take());
} catch (InterruptedException e) {
// exception needs special handling
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);
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.concurrent.threadlifecycle;
public class BlockedState {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new DemoThreadB());
Thread t2 = new Thread(new DemoThreadB());
t1.start();
t2.start();
Thread.sleep(1000);
System.out.println(t2.getState());
System.exit(0);
}
}
class DemoThreadB implements Runnable {
@Override
public void run() {
commonResource();
}
public static synchronized void commonResource() {
while(true) {
// Infinite loop to mimic heavy processing
// Thread 't1' won't leave this method
// when Thread 't2' enters this
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.concurrent.threadlifecycle;
public class NewState implements Runnable {
public static void main(String[] args) {
Runnable runnable = new NewState();
Thread t = new Thread(runnable);
System.out.println(t.getState());
}
@Override
public void run() {
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.concurrent.threadlifecycle;
public class RunnableState implements Runnable {
public static void main(String[] args) {
Runnable runnable = new NewState();
Thread t = new Thread(runnable);
t.start();
System.out.println(t.getState());
}
@Override
public void run() {
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.concurrent.threadlifecycle;
public class TerminatedState implements Runnable {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new TerminatedState());
t1.start();
Thread.sleep(1000);
System.out.println(t1.getState());
}
@Override
public void run() {
// No processing in this block
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.concurrent.threadlifecycle;
public class TimedWaitingState {
public static void main(String[] args) throws InterruptedException {
DemoThread obj1 = new DemoThread();
Thread t1 = new Thread(obj1);
t1.start();
// The following sleep will give enough time for ThreadScheduler
// to start processing of thread t1
Thread.sleep(1000);
System.out.println(t1.getState());
}
}
class DemoThread implements Runnable {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.concurrent.threadlifecycle;
public class WaitingState implements Runnable {
public static Thread t1;
public static void main(String[] args) {
t1 = new Thread(new WaitingState());
t1.start();
}
public void run() {
Thread t2 = new Thread(new DemoThreadWS());
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
}
class DemoThreadWS implements Runnable {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
System.out.println(WaitingState.t1.getState());
}
}

View File

@ -1,7 +1,5 @@
package com.baeldung.concurrent.waitandnotify;
import org.slf4j.Logger;
public class Data {
private String packet;
@ -14,6 +12,7 @@ public class Data {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
}
@ -28,6 +27,7 @@ public class Data {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
}

View File

@ -20,6 +20,7 @@ public class Receiver implements Runnable {
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
}

View File

@ -25,6 +25,7 @@ public class Sender implements Runnable {
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
}

View File

@ -0,0 +1,40 @@
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 e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
pjs.closeScheduler();
}
}

View File

@ -10,7 +10,7 @@ import org.junit.Test;
import com.jayway.awaitility.Awaitility;
public class StopThreadTest {
public class StopThreadManualTest {
@Test
public void whenStoppedThreadIsStopped() throws InterruptedException {

View File

@ -57,7 +57,8 @@ public class NetworkIntegrationTest {
sender.join();
receiver.join();
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
assertEquals(expected, outContent.toString());

View File

@ -127,3 +127,4 @@
- [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)
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)

View File

@ -78,6 +78,11 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- logging -->
<dependency>
@ -468,6 +473,7 @@
<properties>
<!-- marshalling -->
<jackson.version>2.8.5</jackson.version>
<gson.version>2.8.2</gson.version>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>

View File

@ -0,0 +1,13 @@
package com.baeldung.casting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Animal {
private static final Logger LOGGER = LoggerFactory.getLogger(Animal.class);
public void eat() {
LOGGER.info("animal is eating");
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.casting;
import java.util.List;
public class AnimalFeeder {
public void feed(List<Animal> animals) {
animals.forEach(animal -> {
animal.eat();
if (animal instanceof Cat) {
((Cat) animal).meow();
}
});
}
public void uncheckedFeed(List<Animal> animals) {
animals.forEach(animal -> {
animal.eat();
((Cat) animal).meow();
});
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.casting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cat extends Animal implements Mew {
private static final Logger LOGGER = LoggerFactory.getLogger(Cat.class);
public void eat() {
LOGGER.info("cat is eating");
}
public void meow() {
LOGGER.info("meow");
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.casting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Dog extends Animal {
private static final Logger LOGGER = LoggerFactory.getLogger(Dog.class);
public void eat() {
LOGGER.info("dog is eating");
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.casting;
public interface Mew {
public void meow();
}

View File

@ -0,0 +1,29 @@
package com.baeldung.classloader;
import java.io.*;
public class CustomClassLoader extends ClassLoader {
public Class getClass(String name) throws ClassNotFoundException {
byte[] b = loadClassFromFile(name);
return defineClass(name, b, 0, b.length);
}
private byte[] loadClassFromFile(String fileName) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
fileName.replace('.', File.separatorChar) + ".class");
byte[] buffer;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int nextValue = 0;
try {
while ( (nextValue = inputStream.read()) != -1 ) {
byteStream.write(nextValue);
}
} catch (IOException e) {
e.printStackTrace();
}
buffer = byteStream.toByteArray();
return buffer;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.classloader;
import com.sun.javafx.util.Logging;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class PrintClassLoader {
public void printClassLoaders() throws ClassNotFoundException {
System.out.println("Classloader of this class:" + PrintClassLoader.class.getClassLoader());
System.out.println("Classloader of Logging:" + Logging.class.getClassLoader());
System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader());
}
}

View File

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

View File

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

View File

@ -0,0 +1,13 @@
package com.baeldung.designpatterns.chainofresponsibility;
public abstract class AuthenticationProcessor {
// next element in chain or responsibility
public AuthenticationProcessor nextProcessor;
public AuthenticationProcessor(AuthenticationProcessor nextProcessor) {
this.nextProcessor = nextProcessor;
}
public abstract boolean isAuthorized(AuthenticationProvider authProvider);
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public interface AuthenticationProvider {
}

View File

@ -0,0 +1,21 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class OAuthAuthenticationProcessor extends AuthenticationProcessor {
public OAuthAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
super(nextProcessor);
}
@Override
public boolean isAuthorized(AuthenticationProvider authProvider) {
if (authProvider instanceof OAuthTokenProvider) {
return Boolean.TRUE;
} else if (nextProcessor != null) {
return nextProcessor.isAuthorized(authProvider);
} else {
return Boolean.FALSE;
}
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class OAuthTokenProvider implements AuthenticationProvider {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class SamlAuthenticationProvider implements AuthenticationProvider {
}

View File

@ -0,0 +1,20 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class UsernamePasswordAuthenticationProcessor extends AuthenticationProcessor {
public UsernamePasswordAuthenticationProcessor(AuthenticationProcessor nextProcessor) {
super(nextProcessor);
}
@Override
public boolean isAuthorized(AuthenticationProvider authProvider) {
if (authProvider instanceof UsernamePasswordProvider) {
return Boolean.TRUE;
} else if (nextProcessor != null) {
return nextProcessor.isAuthorized(authProvider);
} else {
return Boolean.FALSE;
}
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.chainofresponsibility;
public class UsernamePasswordProvider implements AuthenticationProvider {
}

View File

@ -0,0 +1,85 @@
package com.baeldung.designpatterns.flyweight;
import java.awt.Color;
import javax.annotation.concurrent.Immutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents a car. This class is immutable.
*
* @author Donato Rimenti
*/
@Immutable
public class Car implements Vehicle {
/**
* Logger.
*/
private final static Logger LOG = LoggerFactory.getLogger(Car.class);
/**
* The car's engine.
*/
private Engine engine;
/**
* The car's color.
*/
private Color color;
/**
* Instantiates a new Car.
*
* @param engine
* the {@link #engine}
* @param color
* the {@link #color}
*/
public Car(Engine engine, Color color) {
this.engine = engine;
this.color = color;
// Building a new car is a very expensive operation!
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
LOG.error("Error while creating a new car", e);
}
}
/*
* (non-Javadoc)
*
* @see com.baeldung.designpatterns.flyweight.Vehicle#start()
*/
@Override
public void start() {
LOG.info("Car is starting!");
engine.start();
}
/*
* (non-Javadoc)
*
* @see com.baeldung.designpatterns.flyweight.Vehicle#stop()
*/
@Override
public void stop() {
LOG.info("Car is stopping!");
engine.stop();
}
/*
* (non-Javadoc)
*
* @see com.baeldung.designpatterns.flyweight.Vehicle#getColor()
*/
@Override
public Color getColor() {
return this.color;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.designpatterns.flyweight;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Engine for a vehicle.
*
* @author Donato Rimenti
*/
public class Engine {
/**
* Logger.
*/
private final static Logger LOG = LoggerFactory.getLogger(Engine.class);
/**
* Starts the engine.
*/
public void start() {
LOG.info("Engine is starting!");
}
/**
* Stops the engine.
*/
public void stop() {
LOG.info("Engine is stopping!");
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.designpatterns.flyweight;
import java.awt.Color;
/**
* Interface for a vehicle.
*
* @author Donato Rimenti
*/
public interface Vehicle {
/**
* Starts the vehicle.
*/
public void start();
/**
* Stops the vehicle.
*/
public void stop();
/**
* Gets the color of the vehicle.
*
* @return the color of the vehicle
*/
public Color getColor();
}

View File

@ -0,0 +1,45 @@
package com.baeldung.designpatterns.flyweight;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
/**
* Factory which implements the Flyweight pattern to return an existing vehicle
* if present or a new one otherwise.
*
* @author Donato Rimenti
*/
public class VehicleFactory {
/**
* Stores the already created vehicles.
*/
private static Map<Color, Vehicle> vehiclesCache = new HashMap<Color, Vehicle>();
/**
* Private constructor to prevent this class instantiation.
*/
private VehicleFactory() {
}
/**
* Returns a vehicle of the same color passed as argument. If that vehicle
* was already created by this factory, that vehicle is returned, otherwise
* a new one is created and returned.
*
* @param color
* the color of the vehicle to return
* @return a vehicle of the specified color
*/
public static Vehicle createVehicle(Color color) {
// Looks for the requested vehicle into the cache.
// If the vehicle doesn't exist, a new one is created.
Vehicle newVehicle = vehiclesCache.computeIfAbsent(color, newColor -> {
// Creates the new car.
Engine newEngine = new Engine();
return new Car(newEngine, newColor);
});
return newVehicle;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.observer;
public interface Channel {
public void update(Object o);
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.inheritance;
public class ArmoredCar extends Car implements Floatable, Flyable{
private int bulletProofWindows;
private String model;
public void remoteStartCar() {
// this vehicle can be started by using a remote control
}
public String registerModel() {
return model;
}
public String getAValue() {
return super.model; // returns value of model defined in base class Car
// return this.model; // will return value of model defined in ArmoredCar
// return model; // will return value of model defined in ArmoredCar
}
public static String msg() {
// return super.msg(); // this won't compile.
return "ArmoredCar";
}
@Override
public void floatOnWater() {
System.out.println("I can float!");
}
@Override
public void fly() {
System.out.println("I can fly!");
}
public void aMethod() {
// System.out.println(duration); // Won't compile
System.out.println(Floatable.duration); // outputs 10
System.out.println(Flyable.duration); // outputs 20
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.inheritance;
public class BMW extends Car {
public BMW() {
super(5, "BMW");
}
@Override
public String toString() {
return model;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.inheritance;
public class Car {
private final int DEFAULT_WHEEL_COUNT = 5;
private final String DEFAULT_MODEL = "Basic";
protected int wheels;
protected String model;
public Car() {
this.wheels = DEFAULT_WHEEL_COUNT;
this.model = DEFAULT_MODEL;
}
public Car(int wheels, String model) {
this.wheels = wheels;
this.model = model;
}
public void start() {
// Check essential parts
// If okay, start.
}
public static int count = 10;
public static String msg() {
return "Car";
}
public String toString() {
return model;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.inheritance;
public class Employee {
private String name;
private Car car;
public Employee(String name, Car car) {
this.name = name;
this.car = car;
}
public Car getCar() {
return car;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.inheritance;
public interface Floatable {
int duration = 10;
void floatOnWater();
default void repair() {
System.out.println("Repairing Floatable object");
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.inheritance;
public interface Flyable {
int duration = 10;
void fly();
/*
* Commented
*/
//default void repair() {
// System.out.println("Repairing Flyable object");
//}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.inheritance;
public class SpaceCar extends Car implements SpaceTraveller {
@Override
public void floatOnWater() {
System.out.println("SpaceCar floating!");
}
@Override
public void fly() {
System.out.println("SpaceCar flying!");
}
@Override
public void remoteControl() {
System.out.println("SpaceCar being controlled remotely!");
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.inheritance;
public interface SpaceTraveller extends Floatable, Flyable {
int duration = 10;
void remoteControl();
}

View File

@ -0,0 +1,229 @@
package com.baeldung.java.list;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class CustomList<E> implements List<E> {
private Object[] internal = {};
@Override
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends E> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends E> collection) {
throw new UnsupportedOperationException();
}
@Override
public E remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object object) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return internal.length;
}
@Override
public boolean isEmpty() {
return internal.length == 0;
}
@Override
public boolean add(E element) {
// the first cycle
// internal = new Object[1];
// internal[0] = element;
// return true;
Object[] temp = new Object[internal.length + 1];
System.arraycopy(internal, 0, temp, 0, internal.length);
temp[internal.length] = element;
internal = temp;
return true;
}
@SuppressWarnings("unchecked")
@Override
public E get(int index) {
return (E) internal[index];
}
@Override
public boolean contains(Object object) {
// return false
for (Object element : internal) {
if (object.equals(element)) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection<?> collection) {
// the first cycle
// for (Object element : collection) {
// if (element.equals(internal[0])) {
// return true;
// }
// }
// return false;
for (Object element : collection)
if (!contains(element)) {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public E set(int index, E element) {
E oldElement = (E) internal[index];
internal[index] = element;
return oldElement;
}
@Override
public void clear() {
internal = new Object[0];
}
@Override
public int indexOf(Object object) {
// the first cycle
// if (object.equals(internal[0])) {
// return 0;
// }
// return -1;
for (int i = 0; i < internal.length; i++) {
if (object.equals(internal[i])) {
return i;
}
}
return -1;
}
@Override
public int lastIndexOf(Object object) {
// the first cycle
// if (object.equals(internal[0])) {
// return 0;
// }
// return -1;
for (int i = internal.length - 1; i >= 0; i--) {
if (object.equals(internal[i])) {
return i;
}
}
return -1;
}
@SuppressWarnings("unchecked")
@Override
public List<E> subList(int fromIndex, int toIndex) {
// the first cycle
// return (List<E>) Arrays.asList(internal);
Object[] temp = new Object[toIndex - fromIndex];
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
return (List<E>) Arrays.asList(temp);
}
@Override
public Object[] toArray() {
return Arrays.copyOf(internal, internal.length);
}
@SuppressWarnings("unchecked")
@Override
public <T> T[] toArray(T[] array) {
// the first cycle
// array[0] = (T) internal[0];
// return array;
// the second cycle
// if (array.length < internal.length) {
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
// }
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
if (array.length < internal.length) {
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
}
System.arraycopy(internal, 0, array, 0, internal.length);
if (array.length > internal.length) {
array[internal.length] = null;
}
return array;
}
@Override
public Iterator<E> iterator() {
return new CustomIterator();
}
@Override
public ListIterator<E> listIterator() {
return null;
}
@Override
public ListIterator<E> listIterator(int index) {
// ignored for brevity
return null;
}
private class CustomIterator implements Iterator<E> {
int index;
@Override
public boolean hasNext() {
// the first cycle
// return true;
return index != internal.length;
}
@SuppressWarnings("unchecked")
@Override
public E next() {
// the first cycle
// return (E) CustomList.this.internal[0];
E element = (E) CustomList.this.internal[index];
index++;
return element;
}
}
}

View File

@ -1,28 +1,16 @@
package com.baeldung.javac;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Data implements Serializable {
static List<String> textList = new ArrayList();
public class Data {
List<String> textList = new ArrayList();
private static void addText() {
textList.add("baeldung");
textList.add(".");
textList.add("com");
public void addText(String text) {
textList.add(text);
}
public List getTextList() {
this.addText();
List<String> result = new ArrayList<String>();
String firstElement = (String) textList.get(0);
switch (firstElement) {
case "baeldung":
result.add("baeldung");
case "com":
result.add("com");
}
return result;
return this.textList;
}
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.methodoverloadingoverriding.application;
import com.baeldung.methodoverloadingoverriding.model.Car;
import com.baeldung.methodoverloadingoverriding.model.Vehicle;
import com.baeldung.methodoverloadingoverriding.util.Multiplier;
public class Application {
public static void main(String[] args) {
Multiplier multiplier = new Multiplier();
System.out.println(multiplier.multiply(10, 10));
System.out.println(multiplier.multiply(10, 10, 10));
System.out.println(multiplier.multiply(10, 10.5));
System.out.println(multiplier.multiply(10.5, 10.5));
Vehicle vehicle = new Vehicle();
System.out.println(vehicle.accelerate(100));
System.out.println(vehicle.run());
System.out.println(vehicle.stop());
Vehicle car = new Car();
System.out.println(car.accelerate(80));
System.out.println(car.run());
System.out.println(car.stop());
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.methodoverloadingoverriding.model;
public class Car extends Vehicle {
@Override
public String accelerate(long mph) {
return "The car accelerates at : " + mph + " MPH.";
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.methodoverloadingoverriding.model;
public class Vehicle {
public String accelerate(long mph) {
return "The vehicle accelerates at : " + mph + " MPH.";
}
public String stop() {
return "The vehicle has stopped.";
}
public String run() {
return "The vehicle is running.";
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.methodoverloadingoverriding.util;
public class Multiplier {
public int multiply(int a, int b) {
return a * b;
}
public int multiply(int a, int b, int c) {
return a * b * c;
}
public double multiply(double a, double b) {
return a * b;
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.recursion;
public class BinaryNode {
int value;
BinaryNode left;
BinaryNode right;
public BinaryNode(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.recursion;
public class RecursionExample {
public int sum(int n){
if (n >= 1){
return sum(n - 1) + n;
}
return n;
}
public int tailSum(int currentSum, int n){
if (n <= 1) {
return currentSum + n;
}
return tailSum(currentSum + n, n - 1);
}
public int iterativeSum(int n){
int sum = 0;
if(n < 0){
return -1;
}
for(int i=0; i<=n; i++){
sum += i;
}
return sum;
}
public int powerOf10(int n){
if (n == 0){
return 1;
}
return powerOf10(n-1)*10;
}
public int fibonacci(int n){
if (n <=1 ){
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
public String toBinary(int n){
if (n <= 1 ){
return String.valueOf(n);
}
return toBinary(n / 2) + String.valueOf(n % 2);
}
public int calculateTreeHeight(BinaryNode root){
if (root!= null){
if (root.getLeft() != null || root.getRight() != null){
return 1 + max(calculateTreeHeight(root.left) , calculateTreeHeight(root.right));
}
}
return 0;
}
public int max(int a,int b){
return a>b ? a:b;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.resourcebundle;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class ExampleControl extends ResourceBundle.Control {
@Override
public List<Locale> getCandidateLocales(String s, Locale locale) {
return Arrays.asList(new Locale("pl", "PL"));
}
}

Some files were not shown because too many files have changed in this diff Show More