001/*
002 * Copyright (C) 2011 The Guava Authors
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 * 
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtCompatible;
021
022import java.util.Collection;
023import java.util.Comparator;
024import java.util.Iterator;
025import java.util.NavigableSet;
026import java.util.Set;
027
028/**
029 * A {@link Multiset} which maintains the ordering of its elements, according to
030 * either their natural order or an explicit {@link Comparator}. This order is
031 * reflected when iterating over the sorted multiset, either directly, or through
032 * its {@code elementSet} or {@code entrySet} views.  In all cases,
033 * this implementation uses {@link Comparable#compareTo} or
034 * {@link Comparator#compare} instead of {@link Object#equals} to determine
035 * equivalence of instances.
036 * 
037 * <p><b>Warning:</b> The comparison must be <i>consistent with equals</i> as
038 * explained by the {@link Comparable} class specification. Otherwise, the
039 * resulting multiset will violate the {@link Collection} contract, which it is
040 * specified in terms of {@link Object#equals}.
041 * 
042 * <p>See the Guava User Guide article on <a href=
043 * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multiset">
044 * {@code Multiset}</a>.
045 * 
046 * @author Louis Wasserman
047 * @since 11.0
048 */
049@Beta
050@GwtCompatible(emulated = true)
051public interface SortedMultiset<E> extends SortedMultisetBridge<E>, SortedIterable<E> {
052  /**
053   * Returns the comparator that orders this multiset, or
054   * {@link Ordering#natural()} if the natural ordering of the elements is used.
055   */
056  Comparator<? super E> comparator();
057
058  /**
059   * Returns the entry of the first element in this multiset, or {@code null} if
060   * this multiset is empty.
061   */
062  Entry<E> firstEntry();
063
064  /**
065   * Returns the entry of the last element in this multiset, or {@code null} if
066   * this multiset is empty.
067   */
068  Entry<E> lastEntry();
069
070  /**
071   * Returns and removes the entry associated with the lowest element in this
072   * multiset, or returns {@code null} if this multiset is empty.
073   */
074  Entry<E> pollFirstEntry();
075
076  /**
077   * Returns and removes the entry associated with the greatest element in this
078   * multiset, or returns {@code null} if this multiset is empty.
079   */
080  Entry<E> pollLastEntry();
081
082  /**
083   * Returns a {@link NavigableSet} view of the distinct elements in this multiset.
084   * 
085   * @since 14.0 (present with return type {@code SortedSet} since 11.0)
086   */
087  @Override NavigableSet<E> elementSet();
088  
089  /**
090   * {@inheritDoc}
091   *
092   * <p>The {@code entrySet}'s iterator returns entries in ascending element
093   * order according to the this multiset's comparator.
094   */
095  @Override Set<Entry<E>> entrySet();
096
097  /**
098   * {@inheritDoc}
099   *
100   * <p>The iterator returns the elements in ascending order according to this
101   * multiset's comparator.
102   */
103  @Override Iterator<E> iterator();
104
105  /**
106   * Returns a descending view of this multiset. Modifications made to either
107   * map will be reflected in the other.
108   */
109  SortedMultiset<E> descendingMultiset();
110
111  /**
112   * Returns a view of this multiset restricted to the elements less than
113   * {@code upperBound}, optionally including {@code upperBound} itself. The
114   * returned multiset is a view of this multiset, so changes to one will be
115   * reflected in the other. The returned multiset supports all operations that
116   * this multiset supports.
117   * 
118   * <p>The returned multiset will throw an {@link IllegalArgumentException} on
119   * attempts to add elements outside its range.
120   */
121  SortedMultiset<E> headMultiset(E upperBound, BoundType boundType);
122
123  /**
124   * Returns a view of this multiset restricted to the range between
125   * {@code lowerBound} and {@code upperBound}. The returned multiset is a view
126   * of this multiset, so changes to one will be reflected in the other. The
127   * returned multiset supports all operations that this multiset supports.
128   * 
129   * <p>The returned multiset will throw an {@link IllegalArgumentException} on
130   * attempts to add elements outside its range.
131   * 
132   * <p>This method is equivalent to
133   * {@code tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound,
134   * upperBoundType)}.
135   */
136  SortedMultiset<E> subMultiset(E lowerBound, BoundType lowerBoundType,
137      E upperBound, BoundType upperBoundType);
138
139  /**
140   * Returns a view of this multiset restricted to the elements greater than
141   * {@code lowerBound}, optionally including {@code lowerBound} itself. The
142   * returned multiset is a view of this multiset, so changes to one will be
143   * reflected in the other. The returned multiset supports all operations that
144   * this multiset supports.
145   * 
146   * <p>The returned multiset will throw an {@link IllegalArgumentException} on
147   * attempts to add elements outside its range.
148   */
149  SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType);
150}