[JAVA-22519] Move articles from generic libraries module to specific library modules (#15416)

This commit is contained in:
panos-kakos
2023-12-21 09:30:26 +01:00
committed by GitHub
parent 51e9f6cf37
commit 5e4ca88972
121 changed files with 729 additions and 518 deletions
@@ -1,12 +0,0 @@
package com.baeldung.jmh;
import org.openjdk.jmh.annotations.Benchmark;
public class BenchMark {
@Benchmark
public void init() {
}
}
@@ -1,12 +0,0 @@
package com.baeldung.jmh;
import java.io.IOException;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.runner.RunnerException;
public class JmhDemo {
public static void main(String[] args) throws RunnerException, IOException {
Main.main(args);
}
}
@@ -1,19 +0,0 @@
package com.baeldung.jmh.warmup;
public class MainApplication {
static {
long start = System.nanoTime();
ManualClassLoader.load();
long end = System.nanoTime();
System.out.println("Warm Up time : " + (end - start));
}
public static void main(String[] args) {
long start = System.nanoTime();
ManualClassLoader.load();
long end = System.nanoTime();
System.out.println("Total time taken : " + (end - start));
}
}
@@ -1,17 +0,0 @@
package com.baeldung.jmh.warmup;
import com.baeldung.jmh.warmup.dummy.Dummy;
public class ManualClassLoader {
public static void load() {
for (int i = 0; i < 100000; i++) {
// load all(or most) important classes
Dummy dummy = new Dummy();
dummy.m();
}
}
}
@@ -1,9 +0,0 @@
package com.baeldung.jmh.warmup.dummy;
public class Dummy {
public void m() {
}
}
@@ -1,73 +0,0 @@
package com.baeldung.neuroph;
import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.Neuron;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.learning.BackPropagation;
import org.neuroph.util.ConnectionFactory;
import org.neuroph.util.NeuralNetworkType;
public class NeurophXOR {
public static NeuralNetwork assembleNeuralNetwork() {
Layer inputLayer = new Layer();
inputLayer.addNeuron(new Neuron());
inputLayer.addNeuron(new Neuron());
Layer hiddenLayerOne = new Layer();
hiddenLayerOne.addNeuron(new Neuron());
hiddenLayerOne.addNeuron(new Neuron());
hiddenLayerOne.addNeuron(new Neuron());
hiddenLayerOne.addNeuron(new Neuron());
Layer hiddenLayerTwo = new Layer();
hiddenLayerTwo.addNeuron(new Neuron());
hiddenLayerTwo.addNeuron(new Neuron());
hiddenLayerTwo.addNeuron(new Neuron());
hiddenLayerTwo.addNeuron(new Neuron());
Layer outputLayer = new Layer();
outputLayer.addNeuron(new Neuron());
NeuralNetwork ann = new NeuralNetwork();
ann.addLayer(0, inputLayer);
ann.addLayer(1, hiddenLayerOne);
ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(1));
ann.addLayer(2, hiddenLayerTwo);
ConnectionFactory.fullConnect(ann.getLayerAt(1), ann.getLayerAt(2));
ann.addLayer(3, outputLayer);
ConnectionFactory.fullConnect(ann.getLayerAt(2), ann.getLayerAt(3));
ConnectionFactory.fullConnect(ann.getLayerAt(0), ann.getLayerAt(ann.getLayersCount() - 1), false);
ann.setInputNeurons(inputLayer.getNeurons());
ann.setOutputNeurons(outputLayer.getNeurons());
ann.setNetworkType(NeuralNetworkType.MULTI_LAYER_PERCEPTRON);
return ann;
}
public static NeuralNetwork trainNeuralNetwork(NeuralNetwork ann) {
int inputSize = 2;
int outputSize = 1;
DataSet ds = new DataSet(inputSize, outputSize);
DataSetRow rOne = new DataSetRow(new double[] { 0, 1 }, new double[] { 1 });
ds.addRow(rOne);
DataSetRow rTwo = new DataSetRow(new double[] { 1, 1 }, new double[] { 0 });
ds.addRow(rTwo);
DataSetRow rThree = new DataSetRow(new double[] { 0, 0 }, new double[] { 0 });
ds.addRow(rThree);
DataSetRow rFour = new DataSetRow(new double[] { 1, 0 }, new double[] { 1 });
ds.addRow(rFour);
BackPropagation backPropagation = new BackPropagation();
backPropagation.setMaxIterations(1000);
ann.learn(ds, backPropagation);
return ann;
}
}
@@ -1,49 +0,0 @@
package com.baeldung.stm;
import org.multiverse.api.StmUtils;
import org.multiverse.api.callables.TxnCallable;
import org.multiverse.api.references.TxnInteger;
import org.multiverse.api.references.TxnLong;
public class Account {
private final TxnLong lastUpdate;
private final TxnInteger balance;
Account(final int balance) {
this.lastUpdate = StmUtils.newTxnLong(System.currentTimeMillis());
this.balance = StmUtils.newTxnInteger(balance);
}
Integer getBalance() {
return balance.atomicGet();
}
void adjustBy(final int amount) {
adjustBy(amount, System.currentTimeMillis());
}
private void adjustBy(final int amount, final long date) {
StmUtils.atomic(() -> {
balance.increment(amount);
lastUpdate.set(date);
if (balance.get() < 0) {
throw new IllegalArgumentException("Not enough money");
}
});
}
void transferTo(final Account other, final int amount) {
StmUtils.atomic(() -> {
final long date = System.currentTimeMillis();
adjustBy(-amount, date);
other.adjustBy(amount, date);
});
}
@Override
public String toString() {
return StmUtils.atomic((TxnCallable<String>) txn -> "Balance: " + balance.get(txn) + " lastUpdateDate: " + lastUpdate.get(txn));
}
}
@@ -1,118 +0,0 @@
package com.baeldung.javatuples;
import org.javatuples.KeyValue;
import org.javatuples.LabelValue;
import org.javatuples.Pair;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
import org.javatuples.Unit;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class JavaTuplesUnitTest {
@SuppressWarnings("unused")
@Test
public void whenCreatingTuples_thenCreateTuples() {
Pair<String, Integer> pair = new Pair<String, Integer>("This is a pair", 55);
Triplet<String, Integer, Double> triplet = Triplet.with("hello", 23, 33.2);
List<String> collectionOfNames = Arrays.asList("john", "doe", "anne", "alex");
Quartet<String, String, String, String> quartet = Quartet.fromCollection(collectionOfNames);
Pair<String, String> pairFromList = Pair.fromIterable(collectionOfNames, 2);
String[] names = new String[] { "john", "doe", "anne" };
Triplet<String, String, String> triplet2 = Triplet.fromArray(names);
}
@Test
public void whenGetValuexFromTuples_thenRetriveValueWithType() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
String name = quartet.getValue0();
Integer age = quartet.getValue2();
assertThat(name).isEqualTo("john");
assertThat(age).isEqualTo(32);
}
@Test
public void whenGetKeyValue_thenGetKeyValue() {
KeyValue<Integer, String> keyValue = KeyValue.with(5, "F");
Integer key = keyValue.getKey();
String value = keyValue.getValue();
assertThat(key).isEqualTo(5);
assertThat(value).isEqualTo("F");
}
@Test
public void whenGetLabelValue_thenGetLabelValue() {
LabelValue<Integer, String> labelValue = LabelValue.with(5, "F");
Integer key = labelValue.getLabel();
String value = labelValue.getValue();
assertThat(key).isEqualTo(5);
assertThat(value).isEqualTo("F");
}
@Test
public void whenGetValueFromTuples_thenRetriveValueWithoutType() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
String name = (String) quartet.getValue(0);
Integer age = (Integer) quartet.getValue(2);
assertThat(name).isEqualTo("john");
assertThat(age).isEqualTo(32);
}
@Test
public void whenSetValueInTuple_thenGetANewTuple() {
Pair<String, Integer> john = Pair.with("john", 32);
Pair<String, Integer> alex = john.setAt0("alex");
assertThat(john.toString()).isNotEqualTo(alex.toString());
}
@Test
public void whenAddNewElement_thenCreateNewTuple() {
Pair<String, Integer> pair1 = Pair.with("john", 32);
Triplet<String, Integer, String> triplet1 = pair1.add("1051 SW");
assertThat(triplet1.contains("john"));
assertThat(triplet1.contains(32));
assertThat(triplet1.contains("1051 SW"));
Pair<String, Integer> pair2 = Pair.with("alex", 45);
Quartet<String, Integer, String, Integer> quartet2 = pair1.add(pair2);
assertThat(quartet2.containsAll(pair1));
assertThat(quartet2.containsAll(pair2));
Quartet<String, Integer, String, Integer> quartet1 = pair1.add("alex", 45);
assertThat(quartet1.containsAll("alex", "john", 32, 45));
Triplet<String, String, Integer> triplet2 = pair1.addAt1("1051 SW");
assertThat(triplet2.indexOf("john")).isEqualTo(0);
assertThat(triplet2.indexOf("1051 SW")).isEqualTo(1);
assertThat(triplet2.indexOf(32)).isEqualTo(2);
Unit<Integer> unit = pair1.removeFrom0();
assertThat(unit.contains(32));
}
@Test
public void whenCallingToList_thenReturnList() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
List<Object> list = quartet.toList();
assertThat(list.size()).isEqualTo(4);
}
@Test
public void whenCallingToArray_thenReturnArray() {
Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");
Object[] array = quartet.toArray();
assertThat(array.length).isEqualTo(4);
}
}
@@ -1,58 +0,0 @@
package com.baeldung.neuroph;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neuroph.core.NeuralNetwork;
import static org.junit.Assert.*;
public class XORIntegrationTest {
private NeuralNetwork ann = null;
private void print(String input, double output, double actual) {
System.out.println("Testing: " + input + " Expected: " + actual + " Result: " + output);
}
@Before
public void annInit() {
ann = NeurophXOR.trainNeuralNetwork(NeurophXOR.assembleNeuralNetwork());
}
@Test
public void leftDisjunctTest() {
ann.setInput(0, 1);
ann.calculate();
print("0, 1", ann.getOutput()[0], 1.0);
assertEquals(ann.getOutput()[0], 1.0, 0.0);
}
@Test
public void rightDisjunctTest() {
ann.setInput(1, 0);
ann.calculate();
print("1, 0", ann.getOutput()[0], 1.0);
assertEquals(ann.getOutput()[0], 1.0, 0.0);
}
@Test
public void bothFalseConjunctTest() {
ann.setInput(0, 0);
ann.calculate();
print("0, 0", ann.getOutput()[0], 0.0);
assertEquals(ann.getOutput()[0], 0.0, 0.0);
}
@Test
public void bothTrueConjunctTest() {
ann.setInput(1, 1);
ann.calculate();
print("1, 1", ann.getOutput()[0], 0.0);
assertEquals(ann.getOutput()[0], 0.0, 0.0);
}
@After
public void annClose() {
ann = null;
}
}
@@ -1,141 +0,0 @@
package com.baeldung.stm;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.junit.Assert.assertTrue;
public class AccountUnitTest {
@Test
public void givenAccount_whenDecrement_thenShouldReturnProperValue() {
// given
Account a = new Account(10);
// when
a.adjustBy(-5);
// then
assertThat(a.getBalance()).isEqualTo(5);
}
@Test(expected = IllegalArgumentException.class)
public void givenAccount_whenDecrementTooMuch_thenShouldThrow() {
// given
Account a = new Account(10);
// when
a.adjustBy(-11);
}
@Test
public void givenTwoThreads_whenBothApplyOperation_thenShouldThrow() throws InterruptedException {
// given
ExecutorService ex = Executors.newFixedThreadPool(2);
Account a = new Account(10);
CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicBoolean exceptionThrown = new AtomicBoolean(false);
// when
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
a.adjustBy(-6);
} catch (IllegalArgumentException e) {
exceptionThrown.set(true);
}
});
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
a.adjustBy(-5);
} catch (IllegalArgumentException e) {
exceptionThrown.set(true);
}
});
countDownLatch.countDown();
ex.awaitTermination(1, TimeUnit.SECONDS);
ex.shutdown();
// then
assertTrue(exceptionThrown.get());
}
@Test
public void givenTwoAccounts_whenFailedWhileTransferring_thenShouldRollbackTransaction() {
// given
final Account a = new Account(10);
final Account b = new Account(10);
// when
a.transferTo(b, 5);
// then
assertThat(a.getBalance()).isEqualTo(5);
assertThat(b.getBalance()).isEqualTo(15);
// and
try {
a.transferTo(b, 20);
} catch (final IllegalArgumentException e) {
System.out.println("failed to transfer money");
}
// then
assertThat(a.getBalance()).isEqualTo(5);
assertThat(b.getBalance()).isEqualTo(15);
}
@Test
public void givenTwoThreads_whenBothTryToTransfer_thenShouldNotDeadlock() throws InterruptedException {
// given
ExecutorService ex = Executors.newFixedThreadPool(2);
final Account a = new Account(10);
final Account b = new Account(10);
CountDownLatch countDownLatch = new CountDownLatch(1);
// when
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
a.transferTo(b, 10);
});
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
b.transferTo(a, 1);
});
countDownLatch.countDown();
ex.awaitTermination(1, TimeUnit.SECONDS);
ex.shutdown();
// then
assertThat(a.getBalance()).isEqualTo(1);
assertThat(b.getBalance()).isEqualTo(19);
}
}