BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
### Relevant Articles
- [Guide to FastUtil](https://www.baeldung.com/fastutil)
- [Primitive Collections in Eclipse Collections](https://www.baeldung.com/java-eclipse-primitive-collections)
+55
View File
@@ -0,0 +1,55 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>libraries-primitive</artifactId>
<version>1.0-SNAPSHOT</version>
<name>libraries-primitive</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/it.unimi.dsi/fastutil -->
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.19</version>
<scope>test</scope>
</dependency>
<!-- Eclipse Collections -->
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>10.0.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
@@ -0,0 +1,23 @@
package com.baeldung;
import it.unimi.dsi.fastutil.ints.IntBigArrays;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class BigArraysUnitTest {
@Test
public void givenValidAray_whenWrapped_checkAccessFromIntBigArraysMethodsCorrect() {
int[] oneDArray = new int[] { 2, 1, 5, 2, 1, 7 };
int[][] twoDArray = IntBigArrays.wrap(oneDArray.clone());
int firstIndex = IntBigArrays.get(twoDArray, 0);
int lastIndex = IntBigArrays.get(twoDArray, IntBigArrays.length(twoDArray)-1);
assertEquals(2, firstIndex);
assertEquals(7, lastIndex);
}
}
@@ -0,0 +1,58 @@
package com.baeldung;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class FastUtilTypeSpecificBenchmarkUnitTest {
@Param({"100", "1000", "10000", "100000"})
public int setSize;
@Benchmark
public IntSet givenFastUtilsIntSetWithInitialSizeSet_whenPopulated_checkTimeTaken() {
IntSet intSet = new IntOpenHashSet(setSize);
for(int i = 0; i < setSize; i++){
intSet.add(i);
}
return intSet;
}
@Benchmark
public Set<Integer> givenCollectionsHashSetWithInitialSizeSet_whenPopulated_checkTimeTaken() {
Set<Integer> intSet = new HashSet<Integer>(setSize);
for(int i = 0; i < setSize; i++){
intSet.add(i);
}
return intSet;
}
public static void main(String... args) throws RunnerException {
Options opts = new OptionsBuilder()
.include(".*")
.warmupIterations(1)
.measurementIterations(2)
.jvmArgs("-Xms2g", "-Xmx2g")
.shouldDoGC(true)
.forks(1)
.build();
new Runner(opts).run();
}
}
@@ -0,0 +1,20 @@
package com.baeldung;
import it.unimi.dsi.fastutil.doubles.Double2DoubleMap;
import it.unimi.dsi.fastutil.doubles.Double2DoubleOpenHashMap;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class FastUtilTypeSpecificUnitTest {
@Test
public void givenValidDouble2DoubleMap_whenContentsQueried_checkCorrect(){
Double2DoubleMap d2dMap = new Double2DoubleOpenHashMap();
d2dMap.put(2.0, 5.5);
d2dMap.put(3.0, 6.6);
assertEquals(5.5, d2dMap.get(2.0));
}
}
@@ -0,0 +1,70 @@
package com.baeldung;
import org.eclipse.collections.api.list.primitive.ImmutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.impl.factory.primitive.*;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.primitive.IntInterval;
import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap;
import org.junit.Test;
import java.util.stream.DoubleStream;
import static org.junit.Assert.assertEquals;
public class PrimitiveCollectionsUnitTest {
@Test
public void whenListOfLongHasOneTwoThree_thenSumIsSix() {
MutableLongList longList = LongLists.mutable.of(1L, 2L, 3L);
assertEquals(6, longList.sum());
}
@Test
public void whenListOfIntHasOneTwoThree_thenMaxIsThree() {
ImmutableIntList intList = IntLists.immutable.of(1, 2, 3);
assertEquals(3, intList.max());
}
@Test
public void whenConvertFromIterableToPrimitive_thenValuesAreEquals() {
Iterable<Integer> iterable = Interval.oneTo(3);
MutableIntSet intSet = IntSets.mutable.withAll(iterable);
IntInterval intInterval = IntInterval.oneTo(3);
assertEquals(intInterval.toSet(), intSet);
}
@Test
public void testOperationsOnIntIntMap() {
MutableIntIntMap map = new IntIntHashMap();
assertEquals(5, map.addToValue(0, 5));
assertEquals(5, map.get(0));
assertEquals(3, map.getIfAbsentPut(1, 3));
}
@Test
public void whenCreateDoubleStream_thenAverageIsThree() {
DoubleStream doubleStream = DoubleLists
.mutable.with(1.0, 2.0, 3.0, 4.0, 5.0)
.primitiveStream();
assertEquals(3, doubleStream.average().getAsDouble(), 0.001);
}
@Test
public void whenCreateMapFromStream_thenValuesMustMatch() {
Iterable<Integer> integers = Interval.oneTo(3);
MutableIntIntMap map =
IntIntMaps.mutable.from(
integers,
key -> key,
value -> value * value);
MutableIntIntMap expected = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 4)
.withKeyValue(3, 9);
assertEquals(expected, map);
}
}