Coverage Summary for Class: RegularContiguousSet (com.google.common.collect)

Class Method, % Line, %
RegularContiguousSet 0% (0/24) 0% (0/55)
RegularContiguousSet$1 0% (0/2) 0% (0/3)
RegularContiguousSet$2 0% (0/2) 0% (0/3)
RegularContiguousSet$3 0% (0/3) 0% (0/4)
RegularContiguousSet$SerializedForm 0% (0/3) 0% (0/5)
Total 0% (0/34) 0% (0/70)


1 /* 2  * Copyright (C) 2011 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.collect; 16  17 import static com.google.common.base.Preconditions.checkArgument; 18 import static com.google.common.base.Preconditions.checkElementIndex; 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.collect.BoundType.CLOSED; 21  22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import java.io.Serializable; 25 import java.util.Collection; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27  28 /** 29  * An implementation of {@link ContiguousSet} that contains one or more elements. 30  * 31  * @author Gregory Kick 32  */ 33 @GwtCompatible(emulated = true) 34 @SuppressWarnings("unchecked") // allow ungenerified Comparable types 35 final class RegularContiguousSet<C extends Comparable> extends ContiguousSet<C> { 36  private final Range<C> range; 37  38  RegularContiguousSet(Range<C> range, DiscreteDomain<C> domain) { 39  super(domain); 40  this.range = range; 41  } 42  43  private ContiguousSet<C> intersectionInCurrentDomain(Range<C> other) { 44  return range.isConnected(other) 45  ? ContiguousSet.create(range.intersection(other), domain) 46  : new EmptyContiguousSet<C>(domain); 47  } 48  49  @Override 50  ContiguousSet<C> headSetImpl(C toElement, boolean inclusive) { 51  return intersectionInCurrentDomain(Range.upTo(toElement, BoundType.forBoolean(inclusive))); 52  } 53  54  @Override 55  ContiguousSet<C> subSetImpl( 56  C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) { 57  if (fromElement.compareTo(toElement) == 0 && !fromInclusive && !toInclusive) { 58  // Range would reject our attempt to create (x, x). 59  return new EmptyContiguousSet<C>(domain); 60  } 61  return intersectionInCurrentDomain( 62  Range.range( 63  fromElement, BoundType.forBoolean(fromInclusive), 64  toElement, BoundType.forBoolean(toInclusive))); 65  } 66  67  @Override 68  ContiguousSet<C> tailSetImpl(C fromElement, boolean inclusive) { 69  return intersectionInCurrentDomain(Range.downTo(fromElement, BoundType.forBoolean(inclusive))); 70  } 71  72  @GwtIncompatible // not used by GWT emulation 73  @Override 74  int indexOf(Object target) { 75  return contains(target) ? (int) domain.distance(first(), (C) target) : -1; 76  } 77  78  @Override 79  public UnmodifiableIterator<C> iterator() { 80  return new AbstractSequentialIterator<C>(first()) { 81  final C last = last(); 82  83  @Override 84  protected C computeNext(C previous) { 85  return equalsOrThrow(previous, last) ? null : domain.next(previous); 86  } 87  }; 88  } 89  90  @GwtIncompatible // NavigableSet 91  @Override 92  public UnmodifiableIterator<C> descendingIterator() { 93  return new AbstractSequentialIterator<C>(last()) { 94  final C first = first(); 95  96  @Override 97  protected C computeNext(C previous) { 98  return equalsOrThrow(previous, first) ? null : domain.previous(previous); 99  } 100  }; 101  } 102  103  private static boolean equalsOrThrow(Comparable<?> left, @Nullable Comparable<?> right) { 104  return right != null && Range.compareOrThrow(left, right) == 0; 105  } 106  107  @Override 108  boolean isPartialView() { 109  return false; 110  } 111  112  @Override 113  public C first() { 114  return range.lowerBound.leastValueAbove(domain); 115  } 116  117  @Override 118  public C last() { 119  return range.upperBound.greatestValueBelow(domain); 120  } 121  122  @Override 123  ImmutableList<C> createAsList() { 124  if (domain.supportsFastOffset) { 125  return new ImmutableAsList<C>() { 126  @Override 127  ImmutableSortedSet<C> delegateCollection() { 128  return RegularContiguousSet.this; 129  } 130  131  @Override 132  public C get(int i) { 133  checkElementIndex(i, size()); 134  return domain.offset(first(), i); 135  } 136  }; 137  } else { 138  return super.createAsList(); 139  } 140  } 141  142  @Override 143  public int size() { 144  long distance = domain.distance(first(), last()); 145  return (distance >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) distance + 1; 146  } 147  148  @Override 149  public boolean contains(@Nullable Object object) { 150  if (object == null) { 151  return false; 152  } 153  try { 154  return range.contains((C) object); 155  } catch (ClassCastException e) { 156  return false; 157  } 158  } 159  160  @Override 161  public boolean containsAll(Collection<?> targets) { 162  return Collections2.containsAllImpl(this, targets); 163  } 164  165  @Override 166  public boolean isEmpty() { 167  return false; 168  } 169  170  @Override 171  public ContiguousSet<C> intersection(ContiguousSet<C> other) { 172  checkNotNull(other); 173  checkArgument(this.domain.equals(other.domain)); 174  if (other.isEmpty()) { 175  return other; 176  } else { 177  C lowerEndpoint = Ordering.natural().max(this.first(), other.first()); 178  C upperEndpoint = Ordering.natural().min(this.last(), other.last()); 179  return (lowerEndpoint.compareTo(upperEndpoint) <= 0) 180  ? ContiguousSet.create(Range.closed(lowerEndpoint, upperEndpoint), domain) 181  : new EmptyContiguousSet<C>(domain); 182  } 183  } 184  185  @Override 186  public Range<C> range() { 187  return range(CLOSED, CLOSED); 188  } 189  190  @Override 191  public Range<C> range(BoundType lowerBoundType, BoundType upperBoundType) { 192  return Range.create( 193  range.lowerBound.withLowerBoundType(lowerBoundType, domain), 194  range.upperBound.withUpperBoundType(upperBoundType, domain)); 195  } 196  197  @Override 198  public boolean equals(@Nullable Object object) { 199  if (object == this) { 200  return true; 201  } else if (object instanceof RegularContiguousSet) { 202  RegularContiguousSet<?> that = (RegularContiguousSet<?>) object; 203  if (this.domain.equals(that.domain)) { 204  return this.first().equals(that.first()) && this.last().equals(that.last()); 205  } 206  } 207  return super.equals(object); 208  } 209  210  // copied to make sure not to use the GWT-emulated version 211  @Override 212  public int hashCode() { 213  return Sets.hashCodeImpl(this); 214  } 215  216  @GwtIncompatible // serialization 217  private static final class SerializedForm<C extends Comparable> implements Serializable { 218  final Range<C> range; 219  final DiscreteDomain<C> domain; 220  221  private SerializedForm(Range<C> range, DiscreteDomain<C> domain) { 222  this.range = range; 223  this.domain = domain; 224  } 225  226  private Object readResolve() { 227  return new RegularContiguousSet<C>(range, domain); 228  } 229  } 230  231  @GwtIncompatible // serialization 232  @Override 233  Object writeReplace() { 234  return new SerializedForm<C>(range, domain); 235  } 236  237  private static final long serialVersionUID = 0; 238 }