Skip to content

Commit

Permalink
Minor cleanup in aggregator framework (AggFinish now has proper order…
Browse files Browse the repository at this point in the history
… of generics), added domain methods to TupleBrige, added a merge variant method to ObjectUtils.
  • Loading branch information
Aklakan committed Jul 31, 2024
1 parent 939082f commit 50def81
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aksw.commons.collector.core;

import java.io.Serializable;
import java.util.Objects;
import java.util.function.Function;

import org.aksw.commons.collector.domain.Accumulator;
Expand All @@ -14,6 +15,8 @@
public class AggFinish<B, I, E, O, C extends Aggregator<B, E, I>>
implements Aggregator<B, E, O>, Serializable
{
private static final long serialVersionUID = 1L;

private C subAgg;
private Function<? super I, O> transform;

Expand All @@ -26,10 +29,18 @@ public AggFinish(C subAgg, Function<? super I, O> transform) {
@Override
public Accumulator<B, E, O> createAccumulator() {
Accumulator<B, E, I> baseAcc = subAgg.createAccumulator();
Accumulator<B, E, O> result = new AccFinish(baseAcc);
Accumulator<B, E, O> result = new AccFinish<>(baseAcc, transform);
return result;
}

public C getSubAgg() {
return subAgg;
}

public Function<? super I, O> getTransform() {
return transform;
}

public static <B, I, E, O, C extends Aggregator<B, E, I>> AggFinish<B, I, E, O, C> create(C subAgg, Function<? super I, O> transform) {
AggFinish<B, I, E, O, C> result = new AggFinish<>(subAgg, transform);
return result;
Expand Down Expand Up @@ -73,36 +84,43 @@ public boolean equals(Object obj) {
}


public class AccFinish
implements Accumulator<B, E, O>, Serializable
public static class AccFinish<I, E, O, V>
implements Accumulator<I, E, V>, Serializable
{
private static final long serialVersionUID = 1L;

protected Accumulator<B, E, I> subAcc;
protected Accumulator<I, E, O> subAcc;
protected Function<? super O, V> transform;

public AccFinish(Accumulator<B, E, I> subAcc) {
public AccFinish(Accumulator<I, E, O> subAcc, Function<? super O, V> transform) {
this.subAcc = subAcc;
this.transform = transform;
}

@Override
public void accumulate(B binding, E env) {
subAcc.accumulate(binding, env);
public void accumulate(I input, E env) {
subAcc.accumulate(input, env);
}

public Accumulator<I, E, O> getSubAcc() {
return subAcc;
}

@Override
public O getValue() {
I input = subAcc.getValue();
O result = transform.apply(input);
public V getValue() {
O accValue = subAcc.getValue();
V result = transform.apply(accValue);
return result;
}

/** Shortcut for {@code getEnclosingInstance().getTransform()} */
public Function<? super O, V> getTransform() {
return transform;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getEnclosingInstance().hashCode();
result = prime * result + ((subAcc == null) ? 0 : subAcc.hashCode());
return result;
return Objects.hash(subAcc, transform);
}

@Override
Expand All @@ -114,18 +132,7 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
AccFinish other = (AccFinish) obj;
if (!getEnclosingInstance().equals(other.getEnclosingInstance()))
return false;
if (subAcc == null) {
if (other.subAcc != null)
return false;
} else if (!subAcc.equals(other.subAcc))
return false;
return true;
}

private AggFinish getEnclosingInstance() {
return AggFinish.this;
return Objects.equals(subAcc, other.subAcc) && Objects.equals(transform, other.transform);
}

// public static <B, I, O> Accumulator<B, O> create(Accumulator<B, I> subAcc, Function<? super I, O> transform) {
Expand All @@ -134,4 +141,4 @@ private AggFinish getEnclosingInstance() {
// }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import org.aksw.commons.lambda.serializable.SerializableFunction;

/**
* Interface for agregators.
*
* @author raven
*
*
* @param <B> The type of bindings being accumulated by the accumulator
* @param <T> The result object of the accumulation
* @param <B> The type of bindings being accumulated by the accumulator.
* @param <E> The environment/context to be passed to accumulator.
* @param <T> The result object of the accumulation.
*/
@FunctionalInterface
public interface Aggregator<B, E, T> {
Expand Down Expand Up @@ -50,4 +49,4 @@ default Optional<T> accumulateAll(Iterator<? extends B> it, E env) {
return Optional.ofNullable(acc.getValue());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public interface Path<T>

Path<T> subpath(int beginIndex, int endIndex);

// default Path<T> subpath(int beginIndex) {
// return subpath(beginIndex, getNameCount());
// }

boolean startsWith(Path<T> other);
boolean endsWith(Path<T> other);

Expand All @@ -53,6 +57,9 @@ default T toSegment() {
return result;
}

/** May return e.g. the file system underlying a path. Return null if not applicable. */
/**
* Returns an object such as the file system underlying this path. Returns null if not applicable.
* Experimental.
*/
Object getSystem();
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ default void validateBuildArg(TupleBridge<?, ?> bridge) {
int r = getDimension();

if (cl != r) {
throw new IllegalArgumentException("components.length must equal rank but " + cl + " != " + r);
throw new IllegalArgumentException("components.length must equal dimension but " + cl + " != " + r);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ default int getDimension() {
return 3;
}

default C getArg0(D obj) {
return get(obj, 0);
}

default C getArg1(D obj) {
return get(obj, 1);
}

default C getArg2(D obj) {
return get(obj, 2);
}

@Override
default <T> D build(T obj, TupleAccessor<? super T, ? extends C> accessor) {
C s = accessor.get(obj, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ default int getDimension() {
return 4;
}

default C getArg0(D obj) {
return get(obj, 0);
}

default C getArg1(D obj) {
return get(obj, 1);
}

default C getArg2(D obj) {
return get(obj, 2);
}

default C getArg3(D obj) {
return get(obj, 3);
}

@Override
default <T> D build(T obj, TupleAccessor<? super T, ? extends C> accessor) {
C g = accessor.get(obj, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public static <T> T mergeNonNull(T a, T b, BinaryOperator<T> merger) {
return result;
}

/** Similar to {@link #mergeNonNull(Object, Object, BinaryOperator)}
* but with an additional supplier if both arguments are null. */
public static <T> T mergeNonNull(T a, T b, BinaryOperator<T> combiner, Supplier<T> nullCase) {
T result = a == null
? b == null
? nullCase.get()
: b
: b == null
? a
: combiner.apply(a, b);
return result;
}


/**
* Supplier-based coalesce function as described in
* https://benjiweber.co.uk/blog/2013/12/08/null-coalescing-in-java-8/
Expand Down

0 comments on commit 50def81

Please sign in to comment.