Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2012 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import com.google.common.annotations.GwtIncompatible;
     20 
     21 import javax.annotation.Nullable;
     22 
     23 /**
     24  * Skeletal implementation of {@link ImmutableSortedSet#descendingSet()}.
     25  *
     26  * @author Louis Wasserman
     27  */
     28 class DescendingImmutableSortedSet<E> extends ImmutableSortedSet<E> {
     29   private final ImmutableSortedSet<E> forward;
     30 
     31   DescendingImmutableSortedSet(ImmutableSortedSet<E> forward) {
     32     super(Ordering.from(forward.comparator()).reverse());
     33     this.forward = forward;
     34   }
     35 
     36   @Override
     37   public int size() {
     38     return forward.size();
     39   }
     40 
     41   @Override
     42   public UnmodifiableIterator<E> iterator() {
     43     return forward.descendingIterator();
     44   }
     45 
     46   @Override
     47   ImmutableSortedSet<E> headSetImpl(E toElement, boolean inclusive) {
     48     return forward.tailSet(toElement, inclusive).descendingSet();
     49   }
     50 
     51   @Override
     52   ImmutableSortedSet<E> subSetImpl(
     53       E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
     54     return forward.subSet(toElement, toInclusive, fromElement, fromInclusive).descendingSet();
     55   }
     56 
     57   @Override
     58   ImmutableSortedSet<E> tailSetImpl(E fromElement, boolean inclusive) {
     59     return forward.headSet(fromElement, inclusive).descendingSet();
     60   }
     61 
     62   @Override
     63   @GwtIncompatible("NavigableSet")
     64   public ImmutableSortedSet<E> descendingSet() {
     65     return forward;
     66   }
     67 
     68   @Override
     69   @GwtIncompatible("NavigableSet")
     70   public UnmodifiableIterator<E> descendingIterator() {
     71     return forward.iterator();
     72   }
     73 
     74   @Override
     75   @GwtIncompatible("NavigableSet")
     76   ImmutableSortedSet<E> createDescendingSet() {
     77     throw new AssertionError("should never be called");
     78   }
     79 
     80   @Override
     81   public E lower(E element) {
     82     return forward.higher(element);
     83   }
     84 
     85   @Override
     86   public E floor(E element) {
     87     return forward.ceiling(element);
     88   }
     89 
     90   @Override
     91   public E ceiling(E element) {
     92     return forward.floor(element);
     93   }
     94 
     95   @Override
     96   public E higher(E element) {
     97     return forward.lower(element);
     98   }
     99 
    100   @Override
    101   int indexOf(@Nullable Object target) {
    102     int index = forward.indexOf(target);
    103     if (index == -1) {
    104       return index;
    105     } else {
    106       return size() - 1 - index;
    107     }
    108   }
    109 
    110   @Override
    111   boolean isPartialView() {
    112     return forward.isPartialView();
    113   }
    114 }
    115