Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/test/java/com/benjiweber/recordmixins/RecordMixinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,26 @@ public record EnhancedList<T>(List<T> inner) implements
ForwardingList<T>,
Mappable<T>,
Filterable<T, EnhancedList<T>>,
Groupable<T> {}
Groupable<T> {
@Override
public EnhancedList<T> build(List<T> ts) {
return new EnhancedList<>(ts);
}
}

public interface Mappable<T> extends Forwarding<List<T>> {
default <R> List<R> map(Function<T, R> f) {
return inner().stream().map(f).collect(toList());
}
}

public interface Filterable<T, R extends Collection<T>> extends ForwardingAllTheWayDown<List<T>, R> {
public interface Buildable<T, R> {
R build(T t);
}

public interface Filterable<T, R extends Collection<T>> extends Forwarding<List<T>>, Buildable<List<T>, R> {
default R where(Predicate<T> p) {
return forwarding(inner().stream().filter(p).collect(toList()));
return build(inner().stream().filter(p).collect(toList()));
}
}

Expand All @@ -94,24 +103,6 @@ interface Forwarding<T> {
T inner();
}

interface ForwardingAllTheWayDown<T, R> extends Forwarding<T> {
default R forwarding(T t) {
try {
return (R) compatibleConstructor(getClass().getConstructors(), t)
.newInstance(t);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

default Constructor<?> compatibleConstructor(Constructor<?>[] constructors, T t) {
return Stream.of(constructors)
.filter(ctor -> ctor.getParameterCount() == 1)
.filter(ctor -> ctor.getParameters()[0].getType().isAssignableFrom(t.getClass()))
.findAny().orElseThrow(IllegalStateException::new);
}
}

interface ForwardingList<T> extends List<T>, Forwarding<List<T>> {
List<T> inner();

Expand Down