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