Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the
  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.BoundType.CLOSED;
 import static com.google.common.collect.BoundType.OPEN;
 
 
 
 import  javax.annotation.Nullable;

A generalized interval on any ordering, for internal use. Supports null. Unlike Range, this allows the use of an arbitrary comparator. This is designed for use in the implementation of subcollections of sorted collection types.

Whenever possible, use Range instead, which is better supported.

Author(s):
Louis Wasserman
 
 @GwtCompatible(serializable = true)
 final class GeneralRange<T> implements Serializable {
  
Converts a Range to a GeneralRange.
 
   static <T extends ComparableGeneralRange<T> from(Range<T> range) {
     @Nullable
     T lowerEndpoint = range.hasLowerBound() ? range.lowerEndpoint() : null;
     BoundType lowerBoundType = range.hasLowerBound() ? range.lowerBoundType() : ;
 
     @Nullable
     T upperEndpoint = range.hasUpperBound() ? range.upperEndpoint() : null;
     BoundType upperBoundType = range.hasUpperBound() ? range.upperBoundType() : ;
     return new GeneralRange<T>(Ordering.natural(), range.hasLowerBound(), lowerEndpoint,
         lowerBoundTyperange.hasUpperBound(), upperEndpointupperBoundType);
   }

  
Returns the whole range relative to the specified comparator.
 
   static <T> GeneralRange<T> all(Comparator<? super T> comparator) {
     return new GeneralRange<T>(comparatorfalsenullfalsenull);
   }

  
Returns everything above the endpoint relative to the specified comparator, with the specified endpoint behavior.
 
   static <T> GeneralRange<T> downTo(Comparator<? super T> comparator, @Nullable T endpoint,
       BoundType boundType) {
     return new GeneralRange<T>(comparatortrueendpointboundTypefalsenull);
   }

  
Returns everything below the endpoint relative to the specified comparator, with the specified endpoint behavior.
 
   static <T> GeneralRange<T> upTo(Comparator<? super T> comparator, @Nullable T endpoint,
       BoundType boundType) {
     return new GeneralRange<T>(comparatorfalsenulltrueendpointboundType);
   }

  
Returns everything between the endpoints relative to the specified comparator, with the specified endpoint behavior.
 
   static <T> GeneralRange<T> range(Comparator<? super T> comparator, @Nullable T lower,
       BoundType lowerType, @Nullable T upperBoundType upperType) {
     return new GeneralRange<T>(comparatortruelowerlowerTypetrueupperupperType);
   }
 
   private final Comparator<? super T> comparator;
   private final boolean hasLowerBound;
   @Nullable
   private final T lowerEndpoint;
   private final BoundType lowerBoundType;
   private final boolean hasUpperBound;
   @Nullable
   private final T upperEndpoint;
   private final BoundType upperBoundType;
 
  private GeneralRange(Comparator<? super T> comparatorboolean hasLowerBound,
      @Nullable T lowerEndpointBoundType lowerBoundTypeboolean hasUpperBound,
      @Nullable T upperEndpointBoundType upperBoundType) {
    this. = checkNotNull(comparator);
    this. = hasLowerBound;
    this. = hasUpperBound;
    this. = lowerEndpoint;
    this. = checkNotNull(lowerBoundType);
    this. = upperEndpoint;
    this. = checkNotNull(upperBoundType);
    if (hasLowerBound) {
      comparator.compare(lowerEndpointlowerEndpoint);
    }
    if (hasUpperBound) {
      comparator.compare(upperEndpointupperEndpoint);
    }
    if (hasLowerBound && hasUpperBound) {
      int cmp = comparator.compare(lowerEndpointupperEndpoint);
      // be consistent with Range
      checkArgument(cmp <= 0, "lowerEndpoint (%s) > upperEndpoint (%s)"lowerEndpoint,
          upperEndpoint);
      if (cmp == 0) {
        checkArgument(lowerBoundType !=  | upperBoundType != );
      }
    }
  }
  Comparator<? super T> comparator() {
    return ;
  }
  boolean hasLowerBound() {
    return ;
  }
  boolean hasUpperBound() {
    return ;
  }
  boolean isEmpty() {
    return (hasUpperBound() && tooLow(getUpperEndpoint()))
        || (hasLowerBound() && tooHigh(getLowerEndpoint()));
  }
  boolean tooLow(@Nullable T t) {
    if (!hasLowerBound()) {
      return false;
    }
    T lbound = getLowerEndpoint();
    int cmp = .compare(tlbound);
    return cmp < 0 | (cmp == 0 & getLowerBoundType() == );
  }
  boolean tooHigh(@Nullable T t) {
    if (!hasUpperBound()) {
      return false;
    }
    T ubound = getUpperEndpoint();
    int cmp = .compare(tubound);
    return cmp > 0 | (cmp == 0 & getUpperBoundType() == );
  }
  boolean contains(@Nullable T t) {
    return !tooLow(t) && !tooHigh(t);
  }

  
Returns the intersection of the two ranges, or an empty range if their intersection is empty.
  GeneralRange<T> intersect(GeneralRange<T> other) {
    checkNotNull(other);
    checkArgument(.equals(other.comparator));
    boolean hasLowBound = this.;
    @Nullable
    T lowEnd = getLowerEndpoint();
    BoundType lowType = getLowerBoundType();
    if (!hasLowerBound()) {
      hasLowBound = other.hasLowerBound;
      lowEnd = other.getLowerEndpoint();
      lowType = other.getLowerBoundType();
    } else if (other.hasLowerBound()) {
      int cmp = .compare(getLowerEndpoint(), other.getLowerEndpoint());
      if (cmp < 0 || (cmp == 0 && other.getLowerBoundType() == )) {
        lowEnd = other.getLowerEndpoint();
        lowType = other.getLowerBoundType();
      }
    }
    boolean hasUpBound = this.;
    @Nullable
    T upEnd = getUpperEndpoint();
    BoundType upType = getUpperBoundType();
    if (!hasUpperBound()) {
      hasUpBound = other.hasUpperBound;
      upEnd = other.getUpperEndpoint();
      upType = other.getUpperBoundType();
    } else if (other.hasUpperBound()) {
      int cmp = .compare(getUpperEndpoint(), other.getUpperEndpoint());
      if (cmp > 0 || (cmp == 0 && other.getUpperBoundType() == )) {
        upEnd = other.getUpperEndpoint();
        upType = other.getUpperBoundType();
      }
    }
    if (hasLowBound && hasUpBound) {
      int cmp = .compare(lowEndupEnd);
      if (cmp > 0 || (cmp == 0 && lowType ==  && upType == )) {
        // force allowed empty range
        lowEnd = upEnd;
        lowType = ;
        upType = ;
      }
    }
    return new GeneralRange<T>(hasLowBoundlowEndlowTypehasUpBoundupEndupType);
  }
  public boolean equals(@Nullable Object obj) {
    if (obj instanceof GeneralRange) {
      GeneralRange<?> r = (GeneralRange<?>) obj;
      return .equals(r.comparator) &&  == r.hasLowerBound
          &&  == r.hasUpperBound && getLowerBoundType().equals(r.getLowerBoundType())
          && getUpperBoundType().equals(r.getUpperBoundType())
          && Objects.equal(getLowerEndpoint(), r.getLowerEndpoint())
          && Objects.equal(getUpperEndpoint(), r.getUpperEndpoint());
    }
    return false;
  }
  public int hashCode() {
        getUpperBoundType());
  }
  private transient GeneralRange<T> reverse;

  
Returns the same range relative to the reversed comparator.
    GeneralRange<T> result = ;
    if (result == null) {
      result = new GeneralRange<T>(
          Ordering.from().reverse(), getUpperEndpoint(),
      result.reverse = this;
      return this. = result;
    }
    return result;
  }
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append().append(":");
    switch (getLowerBoundType()) {
      case :
        builder.append('[');
        break;
      case :
        builder.append('(');
        break;
    }
    if (hasLowerBound()) {
      builder.append(getLowerEndpoint());
    } else {
      builder.append("-\u221e");
    }
    builder.append(',');
    if (hasUpperBound()) {
      builder.append(getUpperEndpoint());
    } else {
      builder.append("\u221e");
    }
    switch (getUpperBoundType()) {
      case :
        builder.append(']');
        break;
      case :
        builder.append(')');
        break;
    }
    return builder.toString();
  }
    return ;
  }
    return ;
  }
    return ;
  }
    return ;
  }
New to GrepCode? Check out our FAQ X