Skip to content

Commit

Permalink
Merge pull request #208 from HannesWell/cleanup-Iterables2
Browse files Browse the repository at this point in the history
Remove unused or obsolete methods from Iterables2
  • Loading branch information
NiklasRentzCAU authored Oct 21, 2024
2 parents 44d08e1 + 22497a4 commit 6ccc10d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Objects;

import org.eclipse.elk.core.util.Pair;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;

import de.cau.cs.kieler.klighd.DiagramExportConfig;
Expand Down Expand Up @@ -163,26 +161,11 @@ protected final Trim getMaximumDiagramTileTrim(final Iterable<IExportBranding> e
private Trim getCumulatedTrim(final boolean tileTrim, final Iterable<IExportBranding> brandings,
final Rectangle2D bounds, final Trim deviceTrim, final Point dotsPerInch) {

final Trim res = Iterables2.fold(brandings, new Function<Pair<Trim, IExportBranding>, Trim>() {

public Trim apply(final Pair<Trim, IExportBranding> input) {
final Trim previousResult = input.getFirst();
final IExportBranding branding = input.getSecond();
final Trim trim;

if (tileTrim) {
trim = branding.getDiagramTileTrimm(bounds, dotsPerInch, deviceTrim);
} else {
trim = branding.getDiagramTrim(bounds);
}

if (previousResult == null) {
return trim;

} else if (trim == null) {
return previousResult;

} else {
return Iterables2.stream(brandings)
.map(branding -> tileTrim
? branding.getDiagramTileTrimm(bounds, dotsPerInch, deviceTrim)
: branding.getDiagramTrim(bounds))
.filter(Objects::nonNull).reduce((previousResult, trim) -> {
final float maxLeft = Math.max(previousResult.left, trim.left);
final float maxRight = Math.max(previousResult.right, trim.right);
final float maxTop = Math.max(previousResult.top, trim.top);
Expand All @@ -194,11 +177,7 @@ public Trim apply(final Pair<Trim, IExportBranding> input) {
} else {
return previousResult;
}
}
}
});

return res != null ? res : new Trim(0, 0, 0, 0);
}).orElseGet(() -> new Trim(0, 0, 0, 0));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package de.cau.cs.kieler.klighd.util;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.eclipse.elk.core.util.Pair;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;

/**
Expand Down Expand Up @@ -77,39 +75,6 @@ public Iterator<T> iterator() {
};
}

/**
* Returns an {@link Iterable} containing only {@code value}.<br>
* This is a counterpart to {@link com.google.common.collect.Iterators#singletonIterator(Object)
* Iterators#singletonIterator(Object)}.<br>
* In case {@code value} is {@code null} an empty {@link Iterable} is returned.
*
* @param <T>
* the type of value
* @param value
* the value to be encapsulated
* @return the required {@link Iterable}
*/
public static <T> Iterable<T> singletonIterable(final T value) {
return singletonList(value);
}

/**
* Returns a {@link List} containing only {@code value}.<br>
* This is a counterpart to
* {@link com.google.common.collect.Iterators#singletonIterator(Object)
* Iterators#singletonIterator(Object)}.<br>
* In case {@code value} is {@code null} an empty {@link List} is returned.
*
* @param <T>
* the type of value
* @param value
* the value to be encapsulated, may be null
* @return the required {@link Iterable}
*/
public static <T> List<T> singletonList(final T value) {
return value != null ? ImmutableList.of(value) : ImmutableList.<T>of();
}

/**
* Constructs a new {@link Iterable} containing all elements of <code>iterable</code> except the
* last <code>count</code> ones. The provided <code>iterable</code> is lazily evaluated, the
Expand Down Expand Up @@ -266,34 +231,15 @@ public String toString() {
}

/**
* Performs a <i>fold</i> operation on the elements of the provided {@link Iterable} by applying
* the provided {@link Function Function&lt;Pair&lt;R, E&gt;, R&gt;} onto each element.
* Returns a sequential {@code Stream} with the given {@link Iterable} as its source.
*
* @param <E>
* type of {@code iterable}'s elements
* @param <R>
* type of the result determined by {@code function}
* @param iterable
* the {@link Iterable} containing the particular elements
* @param function
* the {@link Function} to be applied, is called with a {@link Pair} providing the
* value of the previous application ({@link Pair#getFirst() first}, is {@code null}
* for first time), and the particular element of {@code iterable} (
* {@link Pair#getSecond() second})
* @return the accumulated result obtained by consecutively applying {@code function} on the
* elements of {@code iterable} and the preliminary result of previous application,
* starting with {@code null}
* @param <T> the type of elements
* @param iterable the iterable
* @return a stream backed by the iterable
*/
public static <E, R> R fold(final Iterable<E> iterable, final Function<Pair<R, E>, R> function) {

final Pair<R, E> pair = new Pair<R, E>();
R result = null;

for (final E element : iterable) {
pair.setFirst(result);
pair.setSecond(element);
result = function.apply(pair);
}
return result;
public static <T> Stream<T> stream(Iterable<T> iterable) {
return iterable instanceof Collection //
? ((Collection<T>) iterable).stream()
: StreamSupport.stream(iterable.spliterator(), false);
}
}

0 comments on commit 6ccc10d

Please sign in to comment.