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