1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.collections; 10 11import java.util.Comparator; 12import java.util.Map; 13import java.util.SortedSet; 14 15/** 16 * The SortedSet returned by Map.entrySet(). This class may not be 17 * instantiated directly. Contrary to what is stated by {@link Map#entrySet} 18 * this class does support the {@link #add} and {@link #addAll} methods. 19 * 20 * <p>The {@link java.util.Map.Entry#setValue} method of the Map.Entry objects 21 * that are returned by this class and its iterators behaves just as the {@link 22 * StoredIterator#set} method does.</p> 23 * 24 * <p>In addition to the standard SortedSet methods, this class provides the 25 * following methods for stored sorted sets only. Note that the use of these 26 * methods is not compatible with the standard Java collections interface.</p> 27 * <ul> 28 * <li>{@link #headSet(Map.Entry, boolean)}</li> 29 * <li>{@link #tailSet(Map.Entry, boolean)}</li> 30 * <li>{@link #subSet(Map.Entry, boolean, Map.Entry, boolean)}</li> 31 * </ul> 32 * 33 * @author Mark Hayes 34 */ 35public class StoredSortedEntrySet<K,V> 36 extends StoredEntrySet<K,V> 37 implements SortedSet<Map.Entry<K,V>> { 38 39 StoredSortedEntrySet(DataView mapView) { 40 41 super(mapView); 42 } 43 44 /** 45 * Returns null since comparators are not supported. The natural ordering 46 * of a stored collection is data byte order, whether the data classes 47 * implement the {@link java.lang.Comparable} interface or not. 48 * This method does not conform to the {@link SortedSet#comparator} 49 * interface. 50 * 51 * @return null. 52 */ 53 public Comparator<? super Map.Entry<K,V>> comparator() { 54 55 return null; 56 } 57 58 /** 59 * Returns the first (lowest) element currently in this sorted set. 60 * This method conforms to the {@link SortedSet#first} interface. 61 * 62 * @return the first element. 63 * 64 * @throws RuntimeExceptionWrapper if a {@link 65 * com.sleepycat.db.DatabaseException} is thrown. 66 */ 67 public Map.Entry<K,V> first() { 68 69 return getFirstOrLast(true); 70 } 71 72 /** 73 * Returns the last (highest) element currently in this sorted set. 74 * This method conforms to the {@link SortedSet#last} interface. 75 * 76 * @return the last element. 77 * 78 * @throws RuntimeExceptionWrapper if a {@link 79 * com.sleepycat.db.DatabaseException} is thrown. 80 */ 81 public Map.Entry<K,V> last() { 82 83 return getFirstOrLast(false); 84 } 85 86 /** 87 * Returns a view of the portion of this sorted set whose elements are 88 * strictly less than toMapEntry. 89 * This method conforms to the {@link SortedSet#headSet} interface. 90 * 91 * <p>Note that the return value is a StoredCollection and must be treated 92 * as such; for example, its iterators must be explicitly closed.</p> 93 * 94 * @param toMapEntry the upper bound. 95 * 96 * @return the subset. 97 * 98 * @throws RuntimeExceptionWrapper if a {@link 99 * com.sleepycat.db.DatabaseException} is thrown. 100 */ 101 public SortedSet<Map.Entry<K,V>> headSet(Map.Entry<K,V> toMapEntry) { 102 103 return subSet(null, false, toMapEntry, false); 104 } 105 106 /** 107 * Returns a view of the portion of this sorted set whose elements are 108 * strictly less than toMapEntry, optionally including toMapEntry. 109 * This method does not exist in the standard {@link SortedSet} interface. 110 * 111 * <p>Note that the return value is a StoredCollection and must be treated 112 * as such; for example, its iterators must be explicitly closed.</p> 113 * 114 * @param toMapEntry is the upper bound. 115 * 116 * @param toInclusive is true to include toMapEntry. 117 * 118 * @return the subset. 119 * 120 * @throws RuntimeExceptionWrapper if a {@link 121 * com.sleepycat.db.DatabaseException} is thrown. 122 */ 123 public SortedSet<Map.Entry<K,V>> headSet(Map.Entry<K,V> toMapEntry, 124 boolean toInclusive) { 125 126 return subSet(null, false, toMapEntry, toInclusive); 127 } 128 129 /** 130 * Returns a view of the portion of this sorted set whose elements are 131 * greater than or equal to fromMapEntry. 132 * This method conforms to the {@link SortedSet#tailSet} interface. 133 * 134 * <p>Note that the return value is a StoredCollection and must be treated 135 * as such; for example, its iterators must be explicitly closed.</p> 136 * 137 * @param fromMapEntry is the lower bound. 138 * 139 * @return the subset. 140 * 141 * @throws RuntimeExceptionWrapper if a {@link 142 * com.sleepycat.db.DatabaseException} is thrown. 143 */ 144 public SortedSet<Map.Entry<K,V>> tailSet(Map.Entry<K,V> fromMapEntry) { 145 146 return subSet(fromMapEntry, true, null, false); 147 } 148 149 /** 150 * Returns a view of the portion of this sorted set whose elements are 151 * strictly greater than fromMapEntry, optionally including fromMapEntry. 152 * This method does not exist in the standard {@link SortedSet} interface. 153 * 154 * <p>Note that the return value is a StoredCollection and must be treated 155 * as such; for example, its iterators must be explicitly closed.</p> 156 * 157 * @param fromMapEntry is the lower bound. 158 * 159 * @param fromInclusive is true to include fromMapEntry. 160 * 161 * @return the subset. 162 * 163 * @throws RuntimeExceptionWrapper if a {@link 164 * com.sleepycat.db.DatabaseException} is thrown. 165 */ 166 public SortedSet<Map.Entry<K,V>> tailSet(Map.Entry<K,V> fromMapEntry, 167 boolean fromInclusive) { 168 169 return subSet(fromMapEntry, fromInclusive, null, false); 170 } 171 172 /** 173 * Returns a view of the portion of this sorted set whose elements range 174 * from fromMapEntry, inclusive, to toMapEntry, exclusive. 175 * This method conforms to the {@link SortedSet#subSet} interface. 176 * 177 * <p>Note that the return value is a StoredCollection and must be treated 178 * as such; for example, its iterators must be explicitly closed.</p> 179 * 180 * @param fromMapEntry is the lower bound. 181 * 182 * @param toMapEntry is the upper bound. 183 * 184 * @return the subset. 185 * 186 * @throws RuntimeExceptionWrapper if a {@link 187 * com.sleepycat.db.DatabaseException} is thrown. 188 */ 189 public SortedSet<Map.Entry<K,V>> subSet(Map.Entry<K,V> fromMapEntry, 190 Map.Entry<K,V> toMapEntry) { 191 192 return subSet(fromMapEntry, true, toMapEntry, false); 193 } 194 195 /** 196 * Returns a view of the portion of this sorted set whose elements are 197 * strictly greater than fromMapEntry and strictly less than toMapEntry, 198 * optionally including fromMapEntry and toMapEntry. 199 * This method does not exist in the standard {@link SortedSet} interface. 200 * 201 * <p>Note that the return value is a StoredCollection and must be treated 202 * as such; for example, its iterators must be explicitly closed.</p> 203 * 204 * @param fromMapEntry is the lower bound. 205 * 206 * @param fromInclusive is true to include fromMapEntry. 207 * 208 * @param toMapEntry is the upper bound. 209 * 210 * @param toInclusive is true to include toMapEntry. 211 * 212 * @return the subset. 213 * 214 * @throws RuntimeExceptionWrapper if a {@link 215 * com.sleepycat.db.DatabaseException} is thrown. 216 */ 217 public SortedSet<Map.Entry<K,V>> subSet(Map.Entry<K,V> fromMapEntry, 218 boolean fromInclusive, 219 Map.Entry<K,V> toMapEntry, 220 boolean toInclusive) { 221 222 Object fromKey = (fromMapEntry != null) ? fromMapEntry.getKey() : null; 223 Object toKey = (toMapEntry != null) ? toMapEntry.getKey() : null; 224 try { 225 return new StoredSortedEntrySet<K,V>( 226 view.subView(fromKey, fromInclusive, toKey, toInclusive, null)); 227 } catch (Exception e) { 228 throw StoredContainer.convertException(e); 229 } 230 } 231} 232