All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.isi.nlp.collections.RangeUtils Maven / Gradle / Ivy

The newest version!
package edu.isi.nlp.collections;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import java.util.Iterator;

/**
 * Utility methods for dealing with Guava {@link Range}s.
 *
 * @author Jay DeYoung, Ryan Gabbard
 */
public final class RangeUtils {
  private RangeUtils() {
    throw new UnsupportedOperationException();
  }

  /**
   * Returns true iff {@code range} is bounded and closed on both sides. {@code range} may not be
   * null.
   */
  public static boolean isClosed(final Range range) {
    return range.hasUpperBound()
        && BoundType.CLOSED.equals(range.upperBoundType())
        && range.hasLowerBound()
        && BoundType.CLOSED.equals(range.lowerBoundType());
  }

  /**
   * Returns the minimal range that {@link Range#encloseAll(Iterable)} {@code ranges}.
   *
   * 

You will want to do something smarter if you have many ranges. Will return {@link * Optional#absent()} if {@code ranges} is empty. * *

Warning: the current implementation is targeted for small numbers of ranges only. See * https://github.com/google/guava/issues/2088 */ public static > Optional> span(Iterable> ranges) { final Iterator> it = ranges.iterator(); if (it.hasNext()) { Range ret = it.next(); while (it.hasNext()) { ret = ret.span(it.next()); } return Optional.of(ret); } else { return Optional.absent(); } } public static > Function, T> lowerEndPointFunction() { return new Function, T>() { @Override public T apply(final Range input) { return input.lowerEndpoint(); } }; } public static > Function, T> upperEndPointFunction() { return new Function, T>() { @Override public T apply(final Range input) { return input.upperEndpoint(); } }; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy