cleanup and testing work
This commit is contained in:
parent
ffd17c1b21
commit
791142c67e
@ -8,29 +8,28 @@ import com.baeldung.algorithms.slope_one.SlopeOne;
|
|||||||
|
|
||||||
public class RunAlgorithm {
|
public class RunAlgorithm {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
System.out.println("Run algorithm:");
|
System.out.println("Run algorithm:");
|
||||||
System.out.println("1 - Simulated Annealing");
|
System.out.println("1 - Simulated Annealing");
|
||||||
System.out.println("2 - Slope One");
|
System.out.println("2 - Slope One");
|
||||||
System.out.println("3 - Simple Genetic Algorithm");
|
System.out.println("3 - Simple Genetic Algorithm");
|
||||||
int decision = in.nextInt();
|
int decision = in.nextInt();
|
||||||
switch (decision) {
|
switch (decision) {
|
||||||
case 1:
|
case 1:
|
||||||
System.out.println(
|
System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
||||||
"Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
break;
|
||||||
break;
|
case 2:
|
||||||
case 2:
|
SlopeOne.slopeOne(3);
|
||||||
SlopeOne.slopeOne(3);
|
break;
|
||||||
break;
|
case 3:
|
||||||
case 3:
|
SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
||||||
SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
break;
|
||||||
break;
|
default:
|
||||||
default:
|
System.out.println("Unknown option");
|
||||||
System.out.println("Unknown option");
|
break;
|
||||||
break;
|
}
|
||||||
}
|
in.close();
|
||||||
in.close();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,40 +5,40 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class Individual {
|
public class Individual {
|
||||||
|
|
||||||
protected int defaultGeneLength = 64;
|
protected int defaultGeneLength = 64;
|
||||||
private byte[] genes = new byte[defaultGeneLength];
|
private byte[] genes = new byte[defaultGeneLength];
|
||||||
private int fitness = 0;
|
private int fitness = 0;
|
||||||
|
|
||||||
public Individual() {
|
public Individual() {
|
||||||
for (int i = 0; i < genes.length; i++) {
|
for (int i = 0; i < genes.length; i++) {
|
||||||
byte gene = (byte) Math.round(Math.random());
|
byte gene = (byte) Math.round(Math.random());
|
||||||
genes[i] = gene;
|
genes[i] = gene;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected byte getSingleGene(int index) {
|
protected byte getSingleGene(int index) {
|
||||||
return genes[index];
|
return genes[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setSingleGene(int index, byte value) {
|
protected void setSingleGene(int index, byte value) {
|
||||||
genes[index] = value;
|
genes[index] = value;
|
||||||
fitness = 0;
|
fitness = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFitness() {
|
public int getFitness() {
|
||||||
if (fitness == 0) {
|
if (fitness == 0) {
|
||||||
fitness = SimpleGeneticAlgorithm.getFitness(this);
|
fitness = SimpleGeneticAlgorithm.getFitness(this);
|
||||||
}
|
}
|
||||||
return fitness;
|
return fitness;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String geneString = "";
|
String geneString = "";
|
||||||
for (int i = 0; i < genes.length; i++) {
|
for (int i = 0; i < genes.length; i++) {
|
||||||
geneString += getSingleGene(i);
|
geneString += getSingleGene(i);
|
||||||
}
|
}
|
||||||
return geneString;
|
return geneString;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,33 +8,33 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class Population {
|
public class Population {
|
||||||
|
|
||||||
private List<Individual> individuals;
|
private List<Individual> individuals;
|
||||||
|
|
||||||
public Population(int size, boolean createNew) {
|
public Population(int size, boolean createNew) {
|
||||||
individuals = new ArrayList<>();
|
individuals = new ArrayList<>();
|
||||||
if (createNew) {
|
if (createNew) {
|
||||||
createNewPopulation(size);
|
createNewPopulation(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Individual getIndividual(int index) {
|
protected Individual getIndividual(int index) {
|
||||||
return individuals.get(index);
|
return individuals.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Individual getFittest() {
|
protected Individual getFittest() {
|
||||||
Individual fittest = individuals.get(0);
|
Individual fittest = individuals.get(0);
|
||||||
for (int i = 0; i < individuals.size(); i++) {
|
for (int i = 0; i < individuals.size(); i++) {
|
||||||
if (fittest.getFitness() <= getIndividual(i).getFitness()) {
|
if (fittest.getFitness() <= getIndividual(i).getFitness()) {
|
||||||
fittest = getIndividual(i);
|
fittest = getIndividual(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fittest;
|
return fittest;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createNewPopulation(int size) {
|
private void createNewPopulation(int size) {
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
Individual newIndividual = new Individual();
|
Individual newIndividual = new Individual();
|
||||||
individuals.add(i, newIndividual);
|
individuals.add(i, newIndividual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,115 +5,113 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class SimpleGeneticAlgorithm {
|
public class SimpleGeneticAlgorithm {
|
||||||
|
|
||||||
private static final double uniformRate = 0.5;
|
private static final double uniformRate = 0.5;
|
||||||
private static final double mutationRate = 0.025;
|
private static final double mutationRate = 0.025;
|
||||||
private static final int tournamentSize = 5;
|
private static final int tournamentSize = 5;
|
||||||
private static final boolean elitism = true;
|
private static final boolean elitism = true;
|
||||||
private static byte[] solution = new byte[64];
|
private static byte[] solution = new byte[64];
|
||||||
|
|
||||||
public static boolean runAlgorithm(int populationSize, String solution) {
|
public static boolean runAlgorithm(int populationSize, String solution) {
|
||||||
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
|
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
|
||||||
"The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
|
}
|
||||||
}
|
SimpleGeneticAlgorithm.setSolution(solution);
|
||||||
SimpleGeneticAlgorithm.setSolution(solution);
|
Population myPop = new Population(populationSize, true);
|
||||||
Population myPop = new Population(populationSize, true);
|
|
||||||
|
|
||||||
int generationCount = 1;
|
int generationCount = 1;
|
||||||
while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
|
while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
|
||||||
System.out.println(
|
System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
||||||
"Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
|
||||||
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
|
generationCount++;
|
||||||
generationCount++;
|
}
|
||||||
}
|
System.out.println("Solution found!");
|
||||||
System.out.println("Solution found!");
|
System.out.println("Generation: " + generationCount);
|
||||||
System.out.println("Generation: " + generationCount);
|
System.out.println("Genes: ");
|
||||||
System.out.println("Genes: ");
|
System.out.println(myPop.getFittest());
|
||||||
System.out.println(myPop.getFittest());
|
return true;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static Population evolvePopulation(Population pop) {
|
public static Population evolvePopulation(Population pop) {
|
||||||
int elitismOffset;
|
int elitismOffset;
|
||||||
Population newPopulation = new Population(pop.getIndividuals().size(), false);
|
Population newPopulation = new Population(pop.getIndividuals().size(), false);
|
||||||
|
|
||||||
if (elitism) {
|
if (elitism) {
|
||||||
newPopulation.getIndividuals().add(0, pop.getFittest());
|
newPopulation.getIndividuals().add(0, pop.getFittest());
|
||||||
elitismOffset = 1;
|
elitismOffset = 1;
|
||||||
} else {
|
} else {
|
||||||
elitismOffset = 0;
|
elitismOffset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
|
for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
|
||||||
Individual indiv1 = tournamentSelection(pop);
|
Individual indiv1 = tournamentSelection(pop);
|
||||||
Individual indiv2 = tournamentSelection(pop);
|
Individual indiv2 = tournamentSelection(pop);
|
||||||
Individual newIndiv = crossover(indiv1, indiv2);
|
Individual newIndiv = crossover(indiv1, indiv2);
|
||||||
newPopulation.getIndividuals().add(i, newIndiv);
|
newPopulation.getIndividuals().add(i, newIndiv);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
|
for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
|
||||||
mutate(newPopulation.getIndividual(i));
|
mutate(newPopulation.getIndividual(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return newPopulation;
|
return newPopulation;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Individual crossover(Individual indiv1, Individual indiv2) {
|
private static Individual crossover(Individual indiv1, Individual indiv2) {
|
||||||
Individual newSol = new Individual();
|
Individual newSol = new Individual();
|
||||||
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
|
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
|
||||||
if (Math.random() <= uniformRate) {
|
if (Math.random() <= uniformRate) {
|
||||||
newSol.setSingleGene(i, indiv1.getSingleGene(i));
|
newSol.setSingleGene(i, indiv1.getSingleGene(i));
|
||||||
} else {
|
} else {
|
||||||
newSol.setSingleGene(i, indiv2.getSingleGene(i));
|
newSol.setSingleGene(i, indiv2.getSingleGene(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newSol;
|
return newSol;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void mutate(Individual indiv) {
|
private static void mutate(Individual indiv) {
|
||||||
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
|
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
|
||||||
if (Math.random() <= mutationRate) {
|
if (Math.random() <= mutationRate) {
|
||||||
byte gene = (byte) Math.round(Math.random());
|
byte gene = (byte) Math.round(Math.random());
|
||||||
indiv.setSingleGene(i, gene);
|
indiv.setSingleGene(i, gene);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Individual tournamentSelection(Population pop) {
|
private static Individual tournamentSelection(Population pop) {
|
||||||
Population tournament = new Population(tournamentSize, false);
|
Population tournament = new Population(tournamentSize, false);
|
||||||
for (int i = 0; i < tournamentSize; i++) {
|
for (int i = 0; i < tournamentSize; i++) {
|
||||||
int randomId = (int) (Math.random() * pop.getIndividuals().size());
|
int randomId = (int) (Math.random() * pop.getIndividuals().size());
|
||||||
tournament.getIndividuals().add(i, pop.getIndividual(randomId));
|
tournament.getIndividuals().add(i, pop.getIndividual(randomId));
|
||||||
}
|
}
|
||||||
Individual fittest = tournament.getFittest();
|
Individual fittest = tournament.getFittest();
|
||||||
return fittest;
|
return fittest;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static int getFitness(Individual individual) {
|
protected static int getFitness(Individual individual) {
|
||||||
int fitness = 0;
|
int fitness = 0;
|
||||||
for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) {
|
for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) {
|
||||||
if (individual.getSingleGene(i) == solution[i]) {
|
if (individual.getSingleGene(i) == solution[i]) {
|
||||||
fitness++;
|
fitness++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fitness;
|
return fitness;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static int getMaxFitness() {
|
protected static int getMaxFitness() {
|
||||||
int maxFitness = solution.length;
|
int maxFitness = solution.length;
|
||||||
return maxFitness;
|
return maxFitness;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void setSolution(String newSolution) {
|
protected static void setSolution(String newSolution) {
|
||||||
solution = new byte[newSolution.length()];
|
solution = new byte[newSolution.length()];
|
||||||
for (int i = 0; i < newSolution.length(); i++) {
|
for (int i = 0; i < newSolution.length(); i++) {
|
||||||
String character = newSolution.substring(i, i + 1);
|
String character = newSolution.substring(i, i + 1);
|
||||||
if (character.contains("0") || character.contains("1")) {
|
if (character.contains("0") || character.contains("1")) {
|
||||||
solution[i] = Byte.parseByte(character);
|
solution[i] = Byte.parseByte(character);
|
||||||
} else {
|
} else {
|
||||||
solution[i] = 0;
|
solution[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,6 @@ public class SquareCalculator {
|
|||||||
return executor.submit(() -> {
|
return executor.submit(() -> {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
return input * input;
|
return input * input;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,55 +8,55 @@ public class CharArrayToStringUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() {
|
public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() {
|
||||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||||
String result = new String(charArray);
|
String result = new String(charArray);
|
||||||
String expectedValue = "character";
|
String expectedValue = "character";
|
||||||
|
|
||||||
assertEquals(expectedValue, result);
|
assertEquals(expectedValue, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString(){
|
public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString() {
|
||||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||||
String result = new String(charArray, 4, 3);
|
String result = new String(charArray, 4, 3);
|
||||||
String expectedValue = "act";
|
String expectedValue = "act";
|
||||||
|
|
||||||
assertEquals(expectedValue, result);
|
assertEquals(expectedValue, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString(){
|
public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString() {
|
||||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||||
String result = String.copyValueOf(charArray);
|
String result = String.copyValueOf(charArray);
|
||||||
String expectedValue = "character";
|
String expectedValue = "character";
|
||||||
|
|
||||||
assertEquals(expectedValue, result);
|
assertEquals(expectedValue, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString(){
|
public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString() {
|
||||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||||
String result = String.copyValueOf(charArray, 0, 4);
|
String result = String.copyValueOf(charArray, 0, 4);
|
||||||
String expectedValue = "char";
|
String expectedValue = "char";
|
||||||
|
|
||||||
assertEquals(expectedValue, result);
|
assertEquals(expectedValue, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCharArray_whenCallingStringValueOf_shouldConvertToString(){
|
public void givenCharArray_whenCallingStringValueOf_shouldConvertToString() {
|
||||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||||
String result = String.valueOf(charArray);
|
String result = String.valueOf(charArray);
|
||||||
String expectedValue = "character";
|
String expectedValue = "character";
|
||||||
|
|
||||||
assertEquals(expectedValue, result);
|
assertEquals(expectedValue, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString(){
|
public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString() {
|
||||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||||
String result = String.valueOf(charArray, 3, 4);
|
String result = String.valueOf(charArray, 3, 4);
|
||||||
String expectedValue = "ract";
|
String expectedValue = "ract";
|
||||||
|
|
||||||
assertEquals(expectedValue, result);
|
assertEquals(expectedValue, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,15 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class StringToCharArrayUnitTest {
|
public class StringToCharArrayUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
|
public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
|
||||||
String givenString = "characters";
|
String givenString = "characters";
|
||||||
|
|
||||||
char[] result = givenString.toCharArray();
|
char[] result = givenString.toCharArray();
|
||||||
|
|
||||||
char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
|
char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
|
||||||
|
|
||||||
assertArrayEquals(expectedCharArray, result);
|
assertArrayEquals(expectedCharArray, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package com.baeldung.algorithms;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
|
||||||
|
|
||||||
public class BinaryGeneticAlgorithmTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGA() {
|
|
||||||
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50,
|
|
||||||
"1011000100000100010000100000100111001000000100000100000000001111"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.algorithms;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
||||||
|
|
||||||
|
public class BinaryGeneticAlgorithmUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGA() {
|
||||||
|
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -32,13 +32,14 @@ public class PriorityBlockingQueueUnitTest {
|
|||||||
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
||||||
|
|
||||||
final Thread thread = new Thread(() -> {
|
final Thread thread = new Thread(() -> {
|
||||||
System.out.println("Polling...");
|
System.out.println("Polling...");
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
Integer poll = queue.take();
|
Integer poll = queue.take();
|
||||||
System.out.println("Polled: " + poll);
|
System.out.println("Polled: " + poll);
|
||||||
} catch (InterruptedException e) {}
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
||||||
|
@ -18,9 +18,7 @@ public class ConcurrentNavigableMapTests {
|
|||||||
|
|
||||||
updateMapConcurrently(skipListMap, 4);
|
updateMapConcurrently(skipListMap, 4);
|
||||||
|
|
||||||
Iterator<Integer> skipListIter = skipListMap
|
Iterator<Integer> skipListIter = skipListMap.keySet().iterator();
|
||||||
.keySet()
|
|
||||||
.iterator();
|
|
||||||
int previous = skipListIter.next();
|
int previous = skipListIter.next();
|
||||||
while (skipListIter.hasNext()) {
|
while (skipListIter.hasNext()) {
|
||||||
int current = skipListIter.next();
|
int current = skipListIter.next();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package org.baeldung.java.streams;
|
package org.baeldung.java.streams;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -14,28 +14,25 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class ThreadPoolInParallelStreamTest {
|
public class ThreadPoolInParallelStreamTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal()
|
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
|
||||||
throws InterruptedException, ExecutionException {
|
|
||||||
long firstNum = 1;
|
long firstNum = 1;
|
||||||
long lastNum = 1_000_000;
|
long lastNum = 1_000_000;
|
||||||
|
|
||||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed()
|
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream()
|
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
|
||||||
.reduce(0L, Long::sum)).get();
|
|
||||||
|
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream(){
|
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
|
||||||
List<Long> aList = new ArrayList<>();
|
List<Long> aList = new ArrayList<>();
|
||||||
Stream<Long> parallelStream = aList.parallelStream();
|
Stream<Long> parallelStream = aList.parallelStream();
|
||||||
|
|
||||||
assertTrue(parallelStream.isParallel());
|
assertTrue(parallelStream.isParallel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>logmdc</artifactId>
|
<artifactId>log-mdc</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>logmdc</name>
|
<name>log-mdc</name>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<description>tutorial on logging with MDC and NDC</description>
|
<description>tutorial on logging with MDC and NDC</description>
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
@Ignore("manual only")
|
@Ignore("manual only")
|
||||||
public class ExternalPropertiesWithXmlIntegrationTest {
|
public class ExternalPropertiesWithXmlManualTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
@ -12,7 +12,7 @@ import java.sql.SQLException;
|
|||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
|
||||||
public class SpringRetryTest {
|
public class SpringRetryIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MyService myService;
|
private MyService myService;
|
@ -8,7 +8,8 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class ThreadPoolTaskSchedulerTest {
|
public class ThreadPoolTaskSchedulerIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
|
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
|
||||||
Thread.sleep(2550);
|
Thread.sleep(2550);
|
@ -5,7 +5,7 @@ import org.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
|
|||||||
import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
|
import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
|
||||||
import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
|
import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
|
||||||
import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
|
import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
|
||||||
import org.baeldung.properties.external.ExternalPropertiesWithXmlIntegrationTest;
|
import org.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Suite;
|
import org.junit.runners.Suite;
|
||||||
import org.junit.runners.Suite.SuiteClasses;
|
import org.junit.runners.Suite.SuiteClasses;
|
||||||
@ -15,7 +15,7 @@ import org.junit.runners.Suite.SuiteClasses;
|
|||||||
PropertiesWithXmlIntegrationTest.class,
|
PropertiesWithXmlIntegrationTest.class,
|
||||||
ExternalPropertiesWithJavaIntegrationTest.class,
|
ExternalPropertiesWithJavaIntegrationTest.class,
|
||||||
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
|
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
|
||||||
ExternalPropertiesWithXmlIntegrationTest.class,
|
ExternalPropertiesWithXmlManualTest.class,
|
||||||
ExtendedPropertiesWithJavaIntegrationTest.class,
|
ExtendedPropertiesWithJavaIntegrationTest.class,
|
||||||
PropertiesWithMultipleXmlsIntegrationTest.class,
|
PropertiesWithMultipleXmlsIntegrationTest.class,
|
||||||
})// @formatter:on
|
})// @formatter:on
|
||||||
|
@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class)
|
@ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class)
|
||||||
public class BeanNameMappingConfigTest {
|
public class BeanNameMappingConfigIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webAppContext;
|
private WebApplicationContext webAppContext;
|
@ -22,7 +22,7 @@ import com.baeldung.config.ControllerClassNameHandlerMappingConfig;
|
|||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class)
|
@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class)
|
||||||
public class ControllerClassNameHandlerMappingTest {
|
public class ControllerClassNameHandlerMappingIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webAppContext;
|
private WebApplicationContext webAppContext;
|
@ -1,7 +1,10 @@
|
|||||||
package com.baeldung.handlermappings;
|
package com.baeldung.handlermappings;
|
||||||
|
|
||||||
import com.baeldung.config.HandlerMappingDefaultConfig;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import com.baeldung.config.HandlerMappingPrioritiesConfig;
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -14,15 +17,12 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import com.baeldung.config.HandlerMappingDefaultConfig;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = HandlerMappingDefaultConfig.class)
|
@ContextConfiguration(classes = HandlerMappingDefaultConfig.class)
|
||||||
public class HandlerMappingDefaultConfigTest {
|
public class HandlerMappingDefaultConfigIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webAppContext;
|
private WebApplicationContext webAppContext;
|
@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class)
|
@ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class)
|
||||||
public class HandlerMappingPriorityConfigTest {
|
public class HandlerMappingPriorityConfigIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webAppContext;
|
private WebApplicationContext webAppContext;
|
@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
|
@ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
|
||||||
public class SimpleUrlMappingConfigTest {
|
public class SimpleUrlMappingConfigIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webAppContext;
|
private WebApplicationContext webAppContext;
|
Loading…
x
Reference in New Issue
Block a user