added LazyValue and tuples refactoring
This commit is contained in:
@@ -29,10 +29,10 @@ public class Main {
|
||||
|
||||
public static void main(String... args) {
|
||||
Map<String, Object> valuesMap = Stream.of(
|
||||
new Tuple2<>("author", "John Doe"),
|
||||
new Tuple2<>("date", "2020-03-25 16:22"),
|
||||
new Tuple2<>("adjective", "simple"))
|
||||
.collect(CollectionUtils.toUnmodifiableTreeMap(it -> it._1, it -> it._2));
|
||||
Tuple2.newInstance("author", "John Doe"),
|
||||
Tuple2.newInstance("date", "2020-03-25 16:22"),
|
||||
Tuple2.newInstance("adjective", "simple"))
|
||||
.collect(CollectionUtils.toUnmodifiableTreeMap(it -> it.get_1(), it -> it.get_2()));
|
||||
withBenchmarkReader(
|
||||
reader -> {
|
||||
Chronometer chronometer = new Chronometer();
|
||||
|
@@ -391,7 +391,7 @@ public class JWO {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return Optional.of(
|
||||
new Tuple2<>(fileName.substring(0, index), fileName.substring(index)));
|
||||
Tuple2.newInstance(fileName.substring(0, index), fileName.substring(index)));
|
||||
}
|
||||
}
|
||||
|
||||
|
23
src/main/java/net/woggioni/jwo/LazyValue.java
Normal file
23
src/main/java/net/woggioni/jwo/LazyValue.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package net.woggioni.jwo;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class LazyValue<T> {
|
||||
private final Supplier<T> valueSupplier;
|
||||
private final MutableTuple2<T, Boolean> instance = MutableTuple2.newInstance(null, false);
|
||||
|
||||
public T get() {
|
||||
if(instance.get_2()) return instance.get_1();
|
||||
synchronized (instance) {
|
||||
if(instance.get_2()) return instance.get_1();
|
||||
else {
|
||||
T value = valueSupplier.get();
|
||||
instance.set_1(value);
|
||||
instance.set_2(true);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,29 +1,12 @@
|
||||
package net.woggioni.jwo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.woggioni.jwo.internal.MutableTuple2Impl;
|
||||
|
||||
import java.util.Comparator;
|
||||
public interface MutableTuple2<T, U> extends Tuple2<T, U> {
|
||||
void set_1(T value);
|
||||
void set_2(U value);
|
||||
|
||||
@EqualsAndHashCode
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MutableTuple2<T, U> {
|
||||
public T _1;
|
||||
public U _2;
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>>
|
||||
Comparator<MutableTuple2<X, Y>> getComparator(Class<X> cls1, Class<Y> cls2) {
|
||||
return Comparator
|
||||
.comparing((MutableTuple2<X, Y> t) -> t._1)
|
||||
.thenComparing((MutableTuple2<X, Y> t) -> t._2);
|
||||
}
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>>
|
||||
Comparator<MutableTuple2<X, Y>> getComparator(MutableTuple2<X, Y> tuple) {
|
||||
return Comparator
|
||||
.comparing((MutableTuple2<X, Y> t) -> t._1)
|
||||
.thenComparing((MutableTuple2<X, Y> t) -> t._2);
|
||||
static <T,U> MutableTuple2<T, U> newInstance(T x, U y) {
|
||||
return new MutableTuple2Impl<T, U>(x, y);
|
||||
}
|
||||
}
|
||||
|
@@ -1,33 +1,14 @@
|
||||
package net.woggioni.jwo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.woggioni.jwo.internal.MutableTuple3Impl;
|
||||
|
||||
import java.util.Comparator;
|
||||
public interface MutableTuple3<T, U, V> extends Tuple3<T, U , V> {
|
||||
void set_1(T value);
|
||||
void set_2(U value);
|
||||
void set_3(V value);
|
||||
|
||||
@EqualsAndHashCode
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MutableTuple3<T, U, V> {
|
||||
public T _1;
|
||||
public U _2;
|
||||
public V _3;
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>, Z extends Comparable<Z>>
|
||||
Comparator<MutableTuple3<X, Y, Z>> getComparator(Class<X> cls1, Class<Y> cls2, Class<Z> cls3) {
|
||||
return Comparator
|
||||
.comparing((MutableTuple3<X, Y, Z> t) -> t._1)
|
||||
.thenComparing((MutableTuple3<X, Y, Z> t) -> t._2)
|
||||
.thenComparing((MutableTuple3<X, Y, Z> t) -> t._3);
|
||||
}
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>, Z extends Comparable<Z>>
|
||||
Comparator<MutableTuple3<X, Y, Z>> getComparator(MutableTuple3<X, Y, Z> tuple) {
|
||||
return Comparator
|
||||
.comparing((MutableTuple3<X, Y, Z> t) -> t._1)
|
||||
.thenComparing((MutableTuple3<X, Y, Z> t) -> t._2)
|
||||
.thenComparing((MutableTuple3<X, Y, Z> t) -> t._3);
|
||||
static <T, U, V> MutableTuple3<T, U, V> newInstance(T x, U y, V z) {
|
||||
return new MutableTuple3Impl<>(x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,25 +1,27 @@
|
||||
package net.woggioni.jwo;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.function.Function;
|
||||
import net.woggioni.jwo.internal.Tuple2Impl;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
public class Tuple2<T, U> {
|
||||
public final T _1;
|
||||
public final U _2;
|
||||
public interface Tuple2<T, U> {
|
||||
T get_1();
|
||||
U get_2();
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>> Comparator<Tuple2<X, Y>> getComparator(Class<X> cls1, Class<Y> cls2) {
|
||||
return Comparator
|
||||
.comparing((Tuple2<X, Y> t) -> t._1)
|
||||
.thenComparing((Tuple2<X, Y> t) -> t._2);
|
||||
static <T,U> Tuple2<T, U> newInstance(T x, U y) {
|
||||
return new Tuple2Impl<>(x, y);
|
||||
}
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>> Comparator<Tuple2<X, Y>> getComparator(Tuple2<X, Y> tuple) {
|
||||
static <X extends Comparable<X>, Y extends Comparable<Y>> Comparator<Tuple2<X, Y>> getComparator(Class<X> cls1, Class<Y> cls2) {
|
||||
return Comparator
|
||||
.comparing((Tuple2<X, Y> t) -> t._1)
|
||||
.thenComparing((Tuple2<X, Y> t) -> t._2);
|
||||
.comparing((Function<Tuple2<X, Y>, X>) Tuple2::get_1)
|
||||
.thenComparing(Tuple2::get_2);
|
||||
}
|
||||
|
||||
static <X extends Comparable<X>, Y extends Comparable<Y>> Comparator<Tuple2<X, Y>> getComparator(Tuple2<X, Y> tuple) {
|
||||
return Comparator
|
||||
.comparing((Function<Tuple2<X, Y>, X>) Tuple2::get_1)
|
||||
.thenComparing(Tuple2::get_2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,30 +1,31 @@
|
||||
package net.woggioni.jwo;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Comparator;
|
||||
import net.woggioni.jwo.internal.Tuple3Impl;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
public class Tuple3<T, U, V> {
|
||||
public final T _1;
|
||||
public final U _2;
|
||||
public final V _3;
|
||||
public interface Tuple3<T, U, V> {
|
||||
T get_1();
|
||||
U get_2();
|
||||
V get_3();
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>, Z extends Comparable<Z>>
|
||||
static <T, U, V> Tuple3<T, U, V> newInstance(T x, U y, V z) {
|
||||
return new Tuple3Impl<>(x, y, z);
|
||||
}
|
||||
|
||||
static <X extends Comparable<X>, Y extends Comparable<Y>, Z extends Comparable<Z>>
|
||||
Comparator<Tuple3<X, Y, Z>> getComparator(Class<X> cls1, Class<Y> cls2, Class<Z> cls3) {
|
||||
return Comparator
|
||||
.comparing((Tuple3<X, Y, Z> t) -> t._1)
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t._2)
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t._3);
|
||||
.comparing((Tuple3<X, Y, Z> t) -> t.get_1())
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t.get_2())
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t.get_3());
|
||||
}
|
||||
|
||||
public static <X extends Comparable<X>, Y extends Comparable<Y>, Z extends Comparable<Z>>
|
||||
static <X extends Comparable<X>, Y extends Comparable<Y>, Z extends Comparable<Z>>
|
||||
Comparator<Tuple3<X, Y, Z>> getComparator(Tuple3<X, Y, Z> tuple) {
|
||||
return Comparator
|
||||
.comparing((Tuple3<X, Y, Z> t) -> t._1)
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t._2)
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t._3);
|
||||
.comparing((Tuple3<X, Y, Z> t) -> t.get_1())
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t.get_2())
|
||||
.thenComparing((Tuple3<X, Y, Z> t) -> t.get_3());
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,23 @@
|
||||
package net.woggioni.jwo.internal;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.woggioni.jwo.MutableTuple2;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MutableTuple2Impl<T, U> implements MutableTuple2<T, U> {
|
||||
@Getter
|
||||
@Setter
|
||||
private T _1;
|
||||
|
||||
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private U _2;
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package net.woggioni.jwo.internal;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.woggioni.jwo.MutableTuple3;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MutableTuple3Impl<T, U, V> implements MutableTuple3<T, U, V> {
|
||||
@Getter
|
||||
@Setter
|
||||
private T _1;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private U _2;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private V _3;
|
||||
}
|
17
src/main/java/net/woggioni/jwo/internal/Tuple2Impl.java
Normal file
17
src/main/java/net/woggioni/jwo/internal/Tuple2Impl.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package net.woggioni.jwo.internal;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.woggioni.jwo.Tuple2;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
public class Tuple2Impl<T, U> implements Tuple2<T, U> {
|
||||
@Getter
|
||||
private final T _1;
|
||||
|
||||
@Getter
|
||||
private final U _2;
|
||||
}
|
||||
|
11
src/main/java/net/woggioni/jwo/internal/Tuple3Impl.java
Normal file
11
src/main/java/net/woggioni/jwo/internal/Tuple3Impl.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package net.woggioni.jwo.internal;
|
||||
|
||||
import lombok.Data;
|
||||
import net.woggioni.jwo.Tuple3;
|
||||
|
||||
@Data
|
||||
public class Tuple3Impl<T, U, V> implements Tuple3<T, U, V> {
|
||||
private final T _1;
|
||||
private final U _2;
|
||||
private final V _3;
|
||||
}
|
@@ -27,14 +27,14 @@ public class TreeWalkerTest {
|
||||
|
||||
private Map<Integer, List<Integer>> parentChildRelationshipMap =
|
||||
Stream.of(
|
||||
new Tuple2<>(1, Collections.singletonList(2)),
|
||||
new Tuple2<>(2, Collections.singletonList(3)),
|
||||
new Tuple2<>(3, Arrays.asList(4, 5)),
|
||||
new Tuple2<>(4, Arrays.asList(6, 7)),
|
||||
new Tuple2<>(5, Collections.singletonList(8))
|
||||
).collect(Collectors.toMap(t -> t._1, t -> t._2));
|
||||
Tuple2.newInstance(1, Collections.singletonList(2)),
|
||||
Tuple2.newInstance(2, Collections.singletonList(3)),
|
||||
Tuple2.newInstance(3, Arrays.asList(4, 5)),
|
||||
Tuple2.newInstance(4, Arrays.asList(6, 7)),
|
||||
Tuple2.newInstance(5, Collections.singletonList(8))
|
||||
).collect(Collectors.toMap(Tuple2::get_1, Tuple2::get_2));
|
||||
|
||||
private Map<Integer, Node> testNodeMap = parentChildRelationshipMap.entrySet().stream()
|
||||
private final Map<Integer, Node> testNodeMap = parentChildRelationshipMap.entrySet().stream()
|
||||
.map(entry -> newNode(entry.getKey(), entry.getValue()))
|
||||
.collect(Collectors.toMap(Node::getId, Function.identity()));
|
||||
|
||||
|
Reference in New Issue
Block a user