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.GwtCompatible;
020import java.util.Collection;
021import java.util.Comparator;
022import java.util.Iterator;
023import java.util.NavigableSet;
024import java.util.Set;
025import org.jspecify.annotations.Nullable;
026
027/**
028 * A {@link Multiset} which maintains the ordering of its elements, according to either their
029 * natural order or an explicit {@link Comparator}. This order is reflected when iterating over the
030 * sorted multiset, either directly, or through its {@code elementSet} or {@code entrySet} views. In
031 * all cases, this implementation uses {@link Comparable#compareTo} or {@link Comparator#compare}
032 * instead of {@link Object#equals} to determine equivalence of instances.
033 *
034 * <p><b>Warning:</b> The comparison must be <i>consistent with equals</i> as explained by the
035 * {@link Comparable} class specification. Otherwise, the resulting multiset will violate the {@link
036 * Collection} contract, which is specified in terms of {@link Object#equals}.
037 *
038 * <p>See the Guava User Guide article on <a href=
039 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset">{@code Multiset}</a>.
040 *
041 * @author Louis Wasserman
042 * @since 11.0
043 */
044@GwtCompatible(emulated = true)
045public interface SortedMultiset<E extends @Nullable Object>
046    extends SortedMultisetBridge<E>, SortedIterable<E> {
047  /**
048   * Returns the comparator that orders this multiset, or {@link Ordering#natural()} if the natural
049   * ordering of the elements is used.
050   */
051  @Override
052  Comparator<? super E> comparator();
053
054  /**
055   * Returns the entry of the first element in this multiset, or {@code null} if this multiset is
056   * empty.
057   */
058  @Nullable Entry<E> firstEntry();
059
060  /**
061   * Returns the entry of the last element in this multiset, or {@code null} if this multiset is
062   * empty.
063   */
064  @Nullable Entry<E> lastEntry();
065
066  /**
067   * Returns and removes the entry associated with the lowest element in this multiset, or returns
068   * {@code null} if this multiset is empty.
069   */
070  @Nullable Entry<E> pollFirstEntry();
071
072  /**
073   * Returns and removes the entry associated with the greatest element in this multiset, or returns
074   * {@code null} if this multiset is empty.
075   */
076  @Nullable Entry<E> pollLastEntry();
077
078  /**
079   * Returns a {@link NavigableSet} view of the distinct elements in this multiset.
080   *
081   * @since 14.0 (present with return type {@code SortedSet} since 11.0)
082   */
083  @Override
084  NavigableSet<E> elementSet();
085
086  /**
087   * {@inheritDoc}
088   *
089   * <p>The {@code entrySet}'s iterator returns entries in ascending element order according to this
090   * multiset's comparator.
091   */
092  @Override
093  Set<Entry<E>> entrySet();
094
095  /**
096   * {@inheritDoc}
097   *
098   * <p>The iterator returns the elements in ascending order according to this multiset's
099   * comparator.
100   */
101  @Override
102  Iterator<E> iterator();
103
104  /**
105   * Returns a descending view of this multiset. Modifications made to either map will be reflected
106   * in the other.
107   */
108  SortedMultiset<E> descendingMultiset();
109
110  /**
111   * Returns a view of this multiset restricted to the elements less than {@code upperBound},
112   * optionally including {@code upperBound} itself. The returned multiset is a view of this
113   * multiset, so changes to one will be reflected in the other. The returned multiset supports all
114   * operations that this multiset supports.
115   *
116   * <p>The returned multiset will throw an {@link IllegalArgumentException} on attempts to add
117   * elements outside its range.
118   */
119  SortedMultiset<E> headMultiset(@ParametricNullness E upperBound, BoundType boundType);
120
121  /**
122   * Returns a view of this multiset restricted to the range between {@code lowerBound} and {@code
123   * upperBound}. The returned multiset is a view of this multiset, so changes to one will be
124   * reflected in the other. The returned multiset supports all operations that this multiset
125   * supports.
126   *
127   * <p>The returned multiset will throw an {@link IllegalArgumentException} on attempts to add
128   * elements outside its range.
129   *
130   * <p>This method is equivalent to {@code tailMultiset(lowerBound,
131   * lowerBoundType).headMultiset(upperBound, upperBoundType)}.
132   */
133  SortedMultiset<E> subMultiset(
134      @ParametricNullness E lowerBound,
135      BoundType lowerBoundType,
136      @ParametricNullness E upperBound,
137      BoundType upperBoundType);
138
139  /**
140   * Returns a view of this multiset restricted to the elements greater than {@code lowerBound},
141   * optionally including {@code lowerBound} itself. The returned multiset is a view of this
142   * multiset, so changes to one will be reflected in the other. The returned multiset supports all
143   * operations that this multiset supports.
144   *
145   * <p>The returned multiset will throw an {@link IllegalArgumentException} on attempts to add
146   * elements outside its range.
147   */
148  SortedMultiset<E> tailMultiset(@ParametricNullness E lowerBound, BoundType boundType);
149}