Merge branch 'master' into BAEL-6334-check-list-element-in-other-list
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-5</artifactId>
|
||||
<name>core-java-collections-5</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
@@ -54,5 +66,5 @@
|
||||
<roaringbitmap.version>0.9.38</roaringbitmap.version>
|
||||
<jmh.version>1.36</jmh.version>
|
||||
</properties>
|
||||
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.customiterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class CustomMovieIterator implements Iterator<Movie> {
|
||||
private int currentIndex;
|
||||
private final List<Movie> list;
|
||||
|
||||
public CustomMovieIterator(List<Movie> list) {
|
||||
this.list = list;
|
||||
this.currentIndex = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
while (currentIndex < list.size()) {
|
||||
Movie currentMovie = list.get(currentIndex);
|
||||
if (isMovieEligible(currentMovie)) {
|
||||
return true;
|
||||
}
|
||||
currentIndex++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movie next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return list.get(currentIndex++);
|
||||
}
|
||||
|
||||
private boolean isMovieEligible(Movie movie) {
|
||||
return movie.getRating() >= 8;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.customiterators;
|
||||
|
||||
public class Movie {
|
||||
private String name;
|
||||
private String director;
|
||||
private float rating;
|
||||
|
||||
public Movie(String name, String director, float rating) {
|
||||
this.name = name;
|
||||
this.director = director;
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(String director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public float getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(float rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.baeldung.customiterators;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
/**
|
||||
* Please note that this class has been added for representation purposes of how a custom collection and its iterator would be.
|
||||
* This does not have working code.
|
||||
*/
|
||||
public class MyList<E> implements List<E> {
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new MyListIterator();
|
||||
}
|
||||
|
||||
private class MyListIterator implements Iterator<E> {
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return new Object[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends E> c) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator(int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> subList(int fromIndex, int toIndex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.customiterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class PalindromIterator implements Iterator<String> {
|
||||
private final List<String> list;
|
||||
private int currentIndex;
|
||||
|
||||
public PalindromIterator(List<String> list) {
|
||||
this.list = list;
|
||||
this.currentIndex = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
while (currentIndex < list.size()) {
|
||||
String currString = list.get(currentIndex);
|
||||
if (isPalindrome(currString)) {
|
||||
return true;
|
||||
}
|
||||
currentIndex++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return list.get(currentIndex++);
|
||||
}
|
||||
|
||||
private boolean isPalindrome(String input) {
|
||||
for (int i = 0; i < input.length() / 2; i++) {
|
||||
if (input.charAt(i) != input.charAt(input.length() - i - 1)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.customiterators;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IteratorsUnitTest {
|
||||
@Test
|
||||
public void givenListOfStrings_whenIteratedWithDefaultIterator() {
|
||||
List<String> listOfStrings = List.of("hello", "world", "this", "is", "a", "test");
|
||||
Iterator<String> iterator = listOfStrings.iterator();
|
||||
Assert.assertTrue(iterator.hasNext());
|
||||
assertEquals(iterator.next(), "hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfStrings_whenPalindromIterator_thenOnlyPalindromes() {
|
||||
List<String> listOfStrings = List.of("oslo", "madam", "car", "deed", "wow", "test");
|
||||
PalindromIterator palindromIterator = new PalindromIterator(listOfStrings);
|
||||
int count = 0;
|
||||
while (palindromIterator.hasNext()) {
|
||||
palindromIterator.next();
|
||||
count++;
|
||||
}
|
||||
assertEquals(count, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMovieList_whenMovieIteratorUsed_thenOnlyHighRatedMovies() {
|
||||
List<Movie> movies = getMovies();
|
||||
CustomMovieIterator movieIterator = new CustomMovieIterator(movies);
|
||||
int count = 0;
|
||||
while (movieIterator.hasNext()) {
|
||||
movieIterator.next();
|
||||
count++;
|
||||
}
|
||||
assertEquals(4, movies.size());
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
private List<Movie> getMovies() {
|
||||
Movie movie1 = new Movie("The Dark Knight", "Nolan", 10);
|
||||
Movie movie2 = new Movie("Avatar", "Cameron", 9);
|
||||
Movie movie3 = new Movie("Tenet", "Nolan", 7);
|
||||
Movie movie4 = new Movie("Extraction", "Hargrave", 5);
|
||||
return List.of(movie1, movie2, movie3, movie4);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,6 +14,11 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.36</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
@@ -40,8 +45,8 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jmh.version>1.21</jmh.version>
|
||||
<commons-lang.version>2.2</commons-lang.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.arrayandlistperformance;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public class ArrayAndArrayListPerformance {
|
||||
|
||||
@Benchmark
|
||||
public void arrayCreation() {
|
||||
int[] array = new int[1000000];
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListCreation() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayItemSetting() {
|
||||
int[] array = new int[1000000];
|
||||
array[0] = 10;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListItemSetting() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
list.add(0, 10);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayItemRetrieval() {
|
||||
int[] array = new int[1000000];
|
||||
array[0] = 10;
|
||||
int item = array[0];
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListItemRetrieval() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
list.add(0, 10);
|
||||
int item2 = list.get(0);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayCloning() {
|
||||
int[] array = new int[1000000];
|
||||
int[] newArray = array.clone();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void arrayListCloning() {
|
||||
ArrayList<Integer> list = new ArrayList<>(1000000);
|
||||
ArrayList<Integer> newList = new ArrayList<>(list);
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.runner.Runner runner = new org.openjdk.jmh.runner.Runner(new OptionsBuilder()
|
||||
.include(ArrayAndArrayListPerformance.class.getSimpleName())
|
||||
.forks(1)
|
||||
.build());
|
||||
runner.run();
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,5 @@
|
||||
- [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue)
|
||||
- [Java Concurrent HashSet Equivalent to ConcurrentHashMap](https://www.baeldung.com/java-concurrent-hashset-concurrenthashmap)
|
||||
- [Reading and Writing With a ConcurrentHashMap](https://www.baeldung.com/concurrenthashmap-reading-and-writing)
|
||||
- [ArrayBlockingQueue vs. LinkedBlockingQueue](https://www.baeldung.com/java-arrayblockingqueue-vs-linkedblockingqueue)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-concurrency-collections)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.concurrent.queue;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BlockingQueueUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenArrayBlockingQueue_whenAddedElements_thenReturnQueueRemainingCapacity() {
|
||||
BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(10);
|
||||
arrayBlockingQueue.add("TestString1");
|
||||
arrayBlockingQueue.add("TestString2");
|
||||
assertEquals(8, arrayBlockingQueue.remainingCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedBlockingQueue_whenAddedElements_thenReturnQueueRemainingCapacity() {
|
||||
BlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>(10);
|
||||
linkedBlockingQueue.add("TestString1");
|
||||
assertEquals(9, linkedBlockingQueue.remainingCapacity());
|
||||
}
|
||||
}
|
||||
@@ -8,3 +8,5 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
|
||||
- [Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java](https://www.baeldung.com/java-scanner-integer)
|
||||
- [Difference Between FileReader and BufferedReader in Java](https://www.baeldung.com/java-filereader-vs-bufferedreader)
|
||||
- [Java: Read Multiple Inputs on Same Line](https://www.baeldung.com/java-read-multiple-inputs-same-line)
|
||||
- [Storing Java Scanner Input in an Array](https://www.baeldung.com/java-store-scanner-input-in-array)
|
||||
|
||||
@@ -10,4 +10,6 @@ This module contains articles about core Java input/output(IO) conversions.
|
||||
- [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array)
|
||||
- [How to Write to a CSV File in Java](https://www.baeldung.com/java-csv)
|
||||
- [How to Convert InputStream to Base64 String](https://www.baeldung.com/java-inputstream-to-base64-string)
|
||||
- [Convert an OutputStream to an InputStream](https://www.baeldung.com/java-convert-outputstream-to-inputstream)
|
||||
- [Java PrintStream to String](https://www.baeldung.com/java-printstream-to-string)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions)
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.printstreamtostring;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class PrintStreamToStringUtil {
|
||||
|
||||
public static String usingByteArrayOutputStreamClass(String input) throws IOException {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String output;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(outputStream)) {
|
||||
printStream.print(input);
|
||||
|
||||
output = outputStream.toString();
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static String usingApacheCommonsIO(String input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
org.apache.commons.io.output.ByteArrayOutputStream outputStream = new org.apache.commons.io.output.ByteArrayOutputStream();
|
||||
try (PrintStream printStream = new PrintStream(outputStream)) {
|
||||
printStream.print(input);
|
||||
}
|
||||
|
||||
return new String(outputStream.toByteArray());
|
||||
}
|
||||
|
||||
public static String usingCustomOutputStream(String input) throws IOException {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String output;
|
||||
try (CustomOutputStream outputStream = new CustomOutputStream(); PrintStream printStream = new PrintStream(outputStream)) {
|
||||
printStream.print(input);
|
||||
|
||||
output = outputStream.toString();
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
private static class CustomOutputStream extends OutputStream {
|
||||
|
||||
private StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
this.stringBuilder.append((char) b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.outputstreamtoinputstream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ConvertOutputStreamToInputStreamUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingByteArray_thenGetExpectedInputStream() throws IOException {
|
||||
String content = "I'm an important message.";
|
||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
out.write(content.getBytes());
|
||||
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
|
||||
String inContent = new String(in.readAllBytes());
|
||||
|
||||
assertEquals(content, inContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingPipeStream_thenGetExpectedInputStream() throws IOException {
|
||||
String content = "I'm going through the pipe.";
|
||||
|
||||
ByteArrayOutputStream originOut = new ByteArrayOutputStream();
|
||||
originOut.write(content.getBytes());
|
||||
|
||||
//connect the pipe
|
||||
PipedInputStream in = new PipedInputStream();
|
||||
PipedOutputStream out = new PipedOutputStream(in);
|
||||
|
||||
try (in) {
|
||||
new Thread(() -> {
|
||||
try (out) {
|
||||
originOut.writeTo(out);
|
||||
} catch (IOException iox) {
|
||||
// handle IOExceptions
|
||||
}
|
||||
}).start();
|
||||
|
||||
String inContent = new String(in.readAllBytes());
|
||||
assertEquals(content, inContent);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.printstreamtostring;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PrintStreamToStringUtilUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingByteArrayOutputStreamClass_thenConvert() throws IOException {
|
||||
assertEquals("test", PrintStreamToStringUtil.usingByteArrayOutputStreamClass("test"));
|
||||
assertEquals("", PrintStreamToStringUtil.usingByteArrayOutputStreamClass(""));
|
||||
assertNull(PrintStreamToStringUtil.usingByteArrayOutputStreamClass(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCustomOutputStream_thenConvert() throws IOException {
|
||||
assertEquals("world", PrintStreamToStringUtil.usingCustomOutputStream("world"));
|
||||
assertEquals("", PrintStreamToStringUtil.usingCustomOutputStream(""));
|
||||
assertNull(PrintStreamToStringUtil.usingCustomOutputStream(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingApacheCommonsIO_thenConvert() {
|
||||
assertEquals("hello", PrintStreamToStringUtil.usingApacheCommonsIO("hello"));
|
||||
assertEquals("", PrintStreamToStringUtil.usingApacheCommonsIO(""));
|
||||
assertNull(PrintStreamToStringUtil.usingApacheCommonsIO(null));
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<h2.version>1.4.197</h2.version> <!-- needs to be specified as it fails with parent's 1.4.200 -->
|
||||
<h2.version>2.1.214</h2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -8,3 +8,4 @@ This module contains articles about Object-oriented programming (OOP) patterns i
|
||||
- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object)
|
||||
- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy)
|
||||
- [Using an Interface vs. Abstract Class in Java](https://www.baeldung.com/java-interface-vs-abstract-class)
|
||||
- [Should We Create an Interface for Only One Implementation?](https://www.baeldung.com/java-interface-single-implementation)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public interface Animal {
|
||||
String makeSound();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class AnimalCare {
|
||||
private Animal animal;
|
||||
|
||||
public AnimalCare(Animal animal) {
|
||||
this.animal = animal;
|
||||
}
|
||||
|
||||
public String animalSound() {
|
||||
return animal.makeSound();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class Cat {
|
||||
private String name;
|
||||
|
||||
public Cat(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String makeSound() {
|
||||
return "Meow! My name is " + name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class Dog implements Animal {
|
||||
private String name;
|
||||
|
||||
public Dog(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeSound() {
|
||||
return "Woof! My name is " + name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class InterfaceSingleImplUnitTest {
|
||||
@Test
|
||||
public void whenUsingMockAnimal_thenAnimalSoundIsCorrect() {
|
||||
MockAnimal mockAnimal = new MockAnimal();
|
||||
String expected = "Mock animal sound!";
|
||||
AnimalCare animalCare = new AnimalCare(mockAnimal);
|
||||
assertThat(animalCare.animalSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingDog_thenDogMakesWoofSound() {
|
||||
Dog dog = new Dog("Buddy");
|
||||
String expected = "Woof! My name is Buddy";
|
||||
assertThat(dog.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCat_thenCatMakesMeowSound() {
|
||||
Cat cat = new Cat("FuzzBall");
|
||||
String expected = "Meow! My name is FuzzBall";
|
||||
assertThat(cat.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingAnimalCareWithDog_thenDogMakesWoofSound() {
|
||||
Animal dog = new Dog("Ham");
|
||||
AnimalCare animalCare = new AnimalCare(dog);
|
||||
String expected = "Woof! My name is Ham";
|
||||
assertThat(animalCare.animalSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCatCareWithCat_thenCatMakesMeowSound() {
|
||||
Cat cat = new Cat("Grumpy");
|
||||
String expected = "Meow! My name is Grumpy";
|
||||
assertThat(cat.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingMockAnimal_thenMockAnimalMakesMockAnimalSound() {
|
||||
MockAnimal mockAnimal = new MockAnimal();
|
||||
String expected = "Mock animal sound!";
|
||||
assertThat(mockAnimal.makeSound()).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.interfacesingleimpl;
|
||||
|
||||
public class MockAnimal implements Animal {
|
||||
@Override
|
||||
public String makeSound() {
|
||||
return "Mock animal sound!";
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,5 @@
|
||||
- [How to Use Regular Expressions to Replace Tokens in Strings in Java](https://www.baeldung.com/java-regex-token-replacement)
|
||||
- [Creating a Java Array from Regular Expression Matches](https://www.baeldung.com/java-array-regex-matches)
|
||||
- [Getting the Text That Follows After the Regex Match in Java](https://www.baeldung.com/java-regex-text-after-match)
|
||||
- [Regular Expression: \z vs \Z Anchors in Java](https://www.baeldung.com/java-regular-expression-z-vs-z-anchors)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)
|
||||
|
||||
@@ -30,12 +30,18 @@
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>${jaxb-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
<version>${spring-security-crypto.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<bouncycastle.version>1.60</bouncycastle.version>
|
||||
<bouncycastle.version>1.70</bouncycastle.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
||||
<spring-security-crypto.version>6.0.3</spring-security-crypto.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.hash.argon;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import org.bouncycastle.crypto.generators.Argon2BytesGenerator;
|
||||
import org.bouncycastle.crypto.params.Argon2Parameters;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
|
||||
public class HashPasswordUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenRawPassword_whenEncodedWithArgon2_thenMatchesEncodedPassword() {
|
||||
String rawPassword = "Baeldung";
|
||||
|
||||
Argon2PasswordEncoder arg2SpringSecurity = new Argon2PasswordEncoder(16, 32, 1, 60000, 10);
|
||||
String hashPassword = arg2SpringSecurity.encode(rawPassword);
|
||||
|
||||
assertTrue(arg2SpringSecurity.matches(rawPassword, hashPassword));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRawPasswordAndSalt_whenArgon2AlgorithmIsUsed_thenHashIsCorrect() {
|
||||
byte[] salt = generateSalt16Byte();
|
||||
String password = "Baeldung";
|
||||
|
||||
int iterations = 2;
|
||||
int memLimit = 66536;
|
||||
int hashLength = 32;
|
||||
int parallelism = 1;
|
||||
Argon2Parameters.Builder builder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).withVersion(Argon2Parameters.ARGON2_VERSION_13)
|
||||
.withIterations(iterations)
|
||||
.withMemoryAsKB(memLimit)
|
||||
.withParallelism(parallelism)
|
||||
.withSalt(salt);
|
||||
|
||||
Argon2BytesGenerator generate = new Argon2BytesGenerator();
|
||||
generate.init(builder.build());
|
||||
byte[] result = new byte[hashLength];
|
||||
generate.generateBytes(password.getBytes(StandardCharsets.UTF_8), result, 0, result.length);
|
||||
|
||||
Argon2BytesGenerator verifier = new Argon2BytesGenerator();
|
||||
verifier.init(builder.build());
|
||||
byte[] testHash = new byte[hashLength];
|
||||
verifier.generateBytes(password.getBytes(StandardCharsets.UTF_8), testHash, 0, testHash.length);
|
||||
|
||||
assertTrue(Arrays.equals(result, testHash));
|
||||
}
|
||||
|
||||
private static byte[] generateSalt16Byte() {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
byte[] salt = new byte[16];
|
||||
secureRandom.nextBytes(salt);
|
||||
return salt;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,3 +4,4 @@ This module contains articles about string APIs.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Retain Only Digits and Decimal Separator in String](https://www.baeldung.com/java-string-retain-digits-decimal)
|
||||
- [Difference Between null and Empty String in Java](https://www.baeldung.com/java-string-null-vs-empty)
|
||||
|
||||
@@ -97,7 +97,7 @@
|
||||
<module>core-java-lang-oop-constructors</module>
|
||||
<module>core-java-lang-oop-patterns</module>
|
||||
<module>core-java-lang-oop-generics</module>
|
||||
<!-- <module>core-java-lang-oop-modifiers</module>-->
|
||||
<module>core-java-lang-oop-modifiers</module>
|
||||
<module>core-java-lang-oop-types</module>
|
||||
<module>core-java-lang-oop-types-2</module>
|
||||
<module>core-java-lang-oop-inheritance</module>
|
||||
|
||||
Reference in New Issue
Block a user