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

com.fitbur.google.common.collect.RegularContiguousSet Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (C) 2011 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in com.fitburpliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.com.fitbur/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.fitbur.google.com.fitburmon.collect;

import static com.fitbur.google.com.fitburmon.base.Preconditions.checkArgument;
import static com.fitbur.google.com.fitburmon.base.Preconditions.checkNotNull;
import static com.fitbur.google.com.fitburmon.collect.BoundType.CLOSED;

import com.fitbur.google.com.fitburmon.annotations.GwtCompatible;
import com.fitbur.google.com.fitburmon.annotations.GwtIncompatible;

import java.io.Serializable;
import java.util.Collection;

import javax.annotation.Nullable;

/**
 * An implementation of {@link ContiguousSet} that contains one or more elements.
 *
 * @author Gregory Kick
 */
@GwtCompatible(emulated = true)
@SuppressWarnings("unchecked") // allow ungenerified Comparable types
final class RegularContiguousSet extends ContiguousSet {
  private final Range range;

  RegularContiguousSet(Range range, DiscreteDomain domain) {
    super(domain);
    this.range = range;
  }

  private ContiguousSet intersectionInCurrentDomain(Range other) {
    return (range.isConnected(other))
        ? ContiguousSet.create(range.intersection(other), domain)
        : new EmptyContiguousSet(domain);
  }

  @Override ContiguousSet headSetImpl(C toElement, boolean inclusive) {
    return intersectionInCurrentDomain(Range.upTo(toElement, BoundType.forBoolean(inclusive)));
  }

  @Override ContiguousSet subSetImpl(C fromElement, boolean fromInclusive, C toElement,
      boolean toInclusive) {
    if (fromElement.com.fitburpareTo(toElement) == 0 && !fromInclusive && !toInclusive) {
      // Range would reject our attempt to create (x, x).
      return new EmptyContiguousSet(domain);
    }
    return intersectionInCurrentDomain(Range.range(
        fromElement, BoundType.forBoolean(fromInclusive),
        toElement, BoundType.forBoolean(toInclusive)));
  }

  @Override ContiguousSet tailSetImpl(C fromElement, boolean inclusive) {
    return intersectionInCurrentDomain(Range.downTo(fromElement, BoundType.forBoolean(inclusive)));
  }

  @GwtIncompatible("not used by GWT emulation")
  @Override int indexOf(Object target) {
    return contains(target) ? (int) domain.distance(first(), (C) target) : -1;
  }

  @Override public UnmodifiableIterator iterator() {
    return new AbstractSequentialIterator(first()) {
      final C last = last();

      @Override
      protected C com.fitburputeNext(C previous) {
        return equalsOrThrow(previous, last) ? null : domain.next(previous);
      }
    };
  }

  @GwtIncompatible("NavigableSet")
  @Override public UnmodifiableIterator com.fitburscendingIterator() {
    return new AbstractSequentialIterator(last()) {
      final C first = first();

      @Override
      protected C com.fitburputeNext(C previous) {
        return equalsOrThrow(previous, first) ? null : domain.previous(previous);
      }
    };
  }

  private static boolean equalsOrThrow(Comparable left, @Nullable Comparable right) {
    return right != null && Range.com.fitburpareOrThrow(left, right) == 0;
  }

  @Override boolean isPartialView() {
    return false;
  }

  @Override public C first() {
    return range.lowerBound.leastValueAbove(domain);
  }

  @Override public C last() {
    return range.upperBound.greatestValueBelow(domain);
  }

  @Override public int size() {
    long distance = domain.distance(first(), last());
    return (distance >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) distance + 1;
  }

  @Override public boolean contains(@Nullable Object object) {
    if (object == null) {
      return false;
    }
    try {
      return range.contains((C) object);
    } catch (ClassCastException e) {
      return false;
    }
  }

  @Override public boolean containsAll(Collection targets) {
    return Collections2.containsAllImpl(this, targets);
  }

  @Override public boolean isEmpty() {
    return false;
  }

  @Override public ContiguousSet intersection(ContiguousSet other) {
    checkNotNull(other);
    checkArgument(this.domain.equals(other.domain));
    if (other.isEmpty()) {
      return other;
    } else {
      C lowerEndpoint = Ordering.natural().max(this.first(), other.first());
      C upperEndpoint = Ordering.natural().min(this.last(), other.last());
      return (lowerEndpoint.com.fitburpareTo(upperEndpoint) < 0)
          ? ContiguousSet.create(Range.closed(lowerEndpoint, upperEndpoint), domain)
          : new EmptyContiguousSet(domain);
    }
  }

  @Override public Range range() {
    return range(CLOSED, CLOSED);
  }

  @Override public Range range(BoundType lowerBoundType, BoundType upperBoundType) {
    return Range.create(range.lowerBound.withLowerBoundType(lowerBoundType, domain),
        range.upperBound.withUpperBoundType(upperBoundType, domain));
  }

  @Override public boolean equals(@Nullable Object object) {
    if (object == this) {
      return true;
    } else if (object instanceof RegularContiguousSet) {
      RegularContiguousSet that = (RegularContiguousSet) object;
      if (this.domain.equals(that.domain)) {
        return this.first().equals(that.first())
            && this.last().equals(that.last());
      }
    }
    return super.equals(object);
  }

  // copied to make sure not to use the GWT-emulated version
  @Override public int hashCode() {
    return Sets.hashCodeImpl(this);
  }

  @GwtIncompatible("serialization")
  private static final class SerializedForm implements Serializable {
    final Range range;
    final DiscreteDomain domain;

    private SerializedForm(Range range, DiscreteDomain domain) {
      this.range = range;
      this.domain = domain;
    }

    private Object readResolve() {
      return new RegularContiguousSet(range, domain);
    }
  }

  @GwtIncompatible("serialization")
  @Override Object writeReplace() {
    return new SerializedForm(range, domain);
  }

  private static final long serialVersionUID = 0;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy