Skip to content

Commit

Permalink
runtime-field: Remove isReversed()-method from CombinedComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Jun 1, 2017
1 parent c1183f5 commit b16e9e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 65 deletions.
Expand Up @@ -6,9 +6,6 @@
/**
* A combined {@link Comparator} that compares a number of
* {@link FieldComparator FieldComparators} in sequence.
* <p>
* Equality is determined by checking if every comparator in the sequence is
* equal and that the {@link #isReversed()}-flag is the same.
*
* @param <ENTITY> the entity type
*
Expand All @@ -20,15 +17,6 @@ public interface CombinedComparator<ENTITY> extends Comparator<ENTITY> {
@Override
CombinedComparator<ENTITY> reversed();

/**
* Returns {@code true} if the result of applying the comparators in the
* {@link #stream()} should be reversed to produce the same result as
* {@link #compare(Object, Object)}.
*
* @return if this comparator is reversed
*/
boolean isReversed();

/**
* Returns the comparators ordered so that the first comparator is
* the most significant, and if that evaluates to {@code 0}, continue on to
Expand Down
Expand Up @@ -16,6 +16,7 @@
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;
Expand All @@ -30,20 +31,11 @@ public final class CombinedComparatorImpl<ENTITY>
implements CombinedComparator<ENTITY> {

private final List<FieldComparator<? super ENTITY>> comparators;
private final boolean reversed;

public CombinedComparatorImpl(
final List<FieldComparator<? super ENTITY>> comparators) {

this(comparators, false);
}

private CombinedComparatorImpl(
final List<FieldComparator<? super ENTITY>> comparators,
final boolean reversed) {

this.comparators = requireNonNull(comparators);
this.reversed = reversed;
}

@Override
Expand All @@ -60,20 +52,18 @@ public int size() {
public int compare(ENTITY o1, ENTITY o2) {
for (final FieldComparator<? super ENTITY> comp : comparators) {
final int c = comp.compare(o1, o2);
if (c != 0) return isReversed() ? -c : c;
if (c != 0) return c;
}

return 0;
}

@Override
public boolean isReversed() {
return reversed;
}

@Override
public CombinedComparator<ENTITY> reversed() {
return new CombinedComparatorImpl<>(comparators, !reversed);
return new CombinedComparatorImpl<>(comparators.stream()
.map(FieldComparator::reversed)
.collect(Collectors.toList())
);
}

@Override
Expand All @@ -83,8 +73,8 @@ public Comparator<ENTITY> thenComparing(Comparator<? super ENTITY> other) {

@Override
public <U> Comparator<ENTITY> thenComparing(
Function<? super ENTITY, ? extends U> keyExtractor,
Comparator<? super U> keyComparator) {
final Function<? super ENTITY, ? extends U> keyExtractor,
final Comparator<? super U> keyComparator) {

if (keyExtractor instanceof Getter) {
@SuppressWarnings("unchecked")
Expand All @@ -98,12 +88,10 @@ public <U> Comparator<ENTITY> thenComparing(
return (a, b) -> {
final int c = compare(a, b);
if (c == 0) {
final int c2 = keyComparator.compare(
return keyComparator.compare(
keyExtractor.apply(a),
keyExtractor.apply(b)
);

return reversed ? -c2 : c2;
} else return c;
};
}
Expand All @@ -128,12 +116,11 @@ public <U> Comparator<ENTITY> thenComparing(
if (oa == null && ob == null) {
return 0;
} else if (oa == null) {
return reversed ? -1 : 1;
return 1;
} else if (ob == null) {
return reversed ? 1 : -1;
return -1;
} else {
final int c2 = oa.compareTo(ob);
return reversed ? -c2 : c2;
return oa.compareTo(ob);
}
} else return c;
};
Expand All @@ -156,8 +143,7 @@ public <U> Comparator<ENTITY> thenComparing(
if (c == 0) {
final int oa = keyExtractor.applyAsInt(a);
final int ob = keyExtractor.applyAsInt(b);
final int c2 = Integer.compare(oa, ob);
return reversed ? -c2 : c2;
return Integer.compare(oa, ob);
} else return c;
};
}
Expand All @@ -179,8 +165,7 @@ public <U> Comparator<ENTITY> thenComparing(
if (c == 0) {
final long oa = keyExtractor.applyAsLong(a);
final long ob = keyExtractor.applyAsLong(b);
final int c2 = Long.compare(oa, ob);
return reversed ? -c2 : c2;
return Long.compare(oa, ob);
} else return c;
};
}
Expand All @@ -202,8 +187,7 @@ public <U> Comparator<ENTITY> thenComparing(
if (c == 0) {
final double oa = keyExtractor.applyAsDouble(a);
final double ob = keyExtractor.applyAsDouble(b);
final int c2 = Double.compare(oa, ob);
return reversed ? -c2 : c2;
return Double.compare(oa, ob);
} else return c;
};
}
Expand All @@ -214,8 +198,6 @@ public boolean equals(Object o) {
if (!(o instanceof CombinedComparator)) return false;

final CombinedComparator<?> that = (CombinedComparator<?>) o;
if (isReversed() != that.isReversed()) return false;

final Iterator<FieldComparator<? super ENTITY>> it =
comparators.iterator();

Expand All @@ -226,15 +208,12 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return 31 * comparators.hashCode() + (isReversed() ? 1 : 0);
return 1299827 * comparators.hashCode();
}

@Override
public String toString() {
return "CombinedComparatorImpl{" +
"comparators=" + comparators +
", reversed=" + reversed +
'}';
return "CombinedComparatorImpl" + comparators;
}

private Comparator<ENTITY> then(Comparator<? super ENTITY> other) {
Expand All @@ -245,16 +224,14 @@ private Comparator<ENTITY> then(Comparator<? super ENTITY> other) {

final List<FieldComparator<? super ENTITY>> copy =
new ArrayList<>(comparators);
copy.add(reversed ? fc.reversed() : fc);

return new CombinedComparatorImpl<>(copy, reversed);
copy.add(fc);

return new CombinedComparatorImpl<>(copy);
} else {
return (a, b) -> {
final int c = compare(a, b);
if (c == 0) {
final int c2 = other.compare(a, b);
return reversed ? -c2 : c2;
} else return c;
return c == 0 ? other.compare(a, b) : c;
};
}
}
Expand All @@ -266,43 +243,43 @@ Optional<Comparator<ENTITY>> then(Getter<? super ENTITY> getter) {
@SuppressWarnings("unchecked")
final GetByte<ENTITY, D> casted = (GetByte<ENTITY, D>) getter;
return Optional.of(then(new ByteFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetShort) {
@SuppressWarnings("unchecked")
final GetShort<ENTITY, D> casted = (GetShort<ENTITY, D>) getter;
return Optional.of(then(new ShortFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetInt) {
@SuppressWarnings("unchecked")
final GetInt<ENTITY, D> casted = (GetInt<ENTITY, D>) getter;
return Optional.of(then(new IntFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetLong) {
@SuppressWarnings("unchecked")
final GetLong<ENTITY, D> casted = (GetLong<ENTITY, D>) getter;
return Optional.of(then(new LongFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetFloat) {
@SuppressWarnings("unchecked")
final GetFloat<ENTITY, D> casted = (GetFloat<ENTITY, D>) getter;
return Optional.of(then(new FloatFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetDouble) {
@SuppressWarnings("unchecked")
final GetDouble<ENTITY, D> casted = (GetDouble<ENTITY, D>) getter;
return Optional.of(then(new DoubleFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetChar) {
@SuppressWarnings("unchecked")
final GetChar<ENTITY, D> casted = (GetChar<ENTITY, D>) getter;
return Optional.of(then(new CharFieldComparatorImpl<>(
casted.getField(), reversed)
casted.getField())
));
} else if (getter instanceof GetReference) {
@SuppressWarnings("unchecked")
Expand All @@ -311,8 +288,7 @@ Optional<Comparator<ENTITY>> then(Getter<? super ENTITY> getter) {
if (field instanceof ComparableField) {
return Optional.of(then(new ReferenceFieldComparatorImpl<>(
(ComparableField<ENTITY, D, V>) casted.getField(),
NullOrder.LAST,
reversed
NullOrder.LAST
)));
}
}
Expand Down

0 comments on commit b16e9e9

Please sign in to comment.