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.SortedSet; 13 14import com.sleepycat.bind.EntryBinding; 15import com.sleepycat.db.Database; 16 17/** 18 * The SortedSet returned by Map.keySet() and which can also be constructed 19 * directly if a Map is not needed. 20 * Since this collection is a set it only contains one element for each key, 21 * even when duplicates are allowed. Key set iterators are therefore 22 * particularly useful for enumerating the unique keys of a store or index that 23 * allows duplicates. 24 * 25 * <p>In addition to the standard SortedSet methods, this class provides the 26 * following methods for stored sorted sets only. Note that the use of these 27 * methods is not compatible with the standard Java collections interface.</p> 28 * <ul> 29 * <li>{@link #headSet(Object, boolean)}</li> 30 * <li>{@link #tailSet(Object, boolean)}</li> 31 * <li>{@link #subSet(Object, boolean, Object, boolean)}</li> 32 * </ul> 33 * 34 * @author Mark Hayes 35 */ 36public class StoredSortedKeySet<K> 37 extends StoredKeySet<K> 38 implements SortedSet<K> { 39 40 /** 41 * Creates a sorted key set view of a {@link Database}. 42 * 43 * @param database is the Database underlying the new collection. 44 * 45 * @param keyBinding is the binding used to translate between key buffers 46 * and key objects. 47 * 48 * @param writeAllowed is true to create a read-write collection or false 49 * to create a read-only collection. 50 * 51 * @throws IllegalArgumentException if formats are not consistently 52 * defined or a parameter is invalid. 53 * 54 * @throws RuntimeExceptionWrapper if a {@link 55 * com.sleepycat.db.DatabaseException} is thrown. 56 */ 57 public StoredSortedKeySet(Database database, 58 EntryBinding<K> keyBinding, 59 boolean writeAllowed) { 60 61 super(new DataView(database, keyBinding, null, null, 62 writeAllowed, null)); 63 } 64 65 StoredSortedKeySet(DataView keySetView) { 66 67 super(keySetView); 68 } 69 70 /** 71 * Returns null since comparators are not supported. The natural ordering 72 * of a stored collection is data byte order, whether the data classes 73 * implement the {@link java.lang.Comparable} interface or not. 74 * This method does not conform to the {@link SortedSet#comparator} 75 * interface. 76 * 77 * @return null. 78 */ 79 public Comparator<? super K> comparator() { 80 81 return null; 82 } 83 84 /** 85 * Returns the first (lowest) element currently in this sorted set. 86 * This method conforms to the {@link SortedSet#first} interface. 87 * 88 * @return the first element. 89 * 90 * @throws RuntimeExceptionWrapper if a {@link 91 * com.sleepycat.db.DatabaseException} is thrown. 92 */ 93 public K first() { 94 95 return getFirstOrLast(true); 96 } 97 98 /** 99 * Returns the last (highest) element currently in this sorted set. 100 * This method conforms to the {@link SortedSet#last} interface. 101 * 102 * @return the last element. 103 * 104 * @throws RuntimeExceptionWrapper if a {@link 105 * com.sleepycat.db.DatabaseException} is thrown. 106 */ 107 public K last() { 108 109 return getFirstOrLast(false); 110 } 111 112 /** 113 * Returns a view of the portion of this sorted set whose elements are 114 * strictly less than toKey. 115 * This method conforms to the {@link SortedSet#headSet} interface. 116 * 117 * <p>Note that the return value is a StoredCollection and must be treated 118 * as such; for example, its iterators must be explicitly closed.</p> 119 * 120 * @param toKey is the upper bound. 121 * 122 * @return the subset. 123 * 124 * @throws RuntimeExceptionWrapper if a {@link 125 * com.sleepycat.db.DatabaseException} is thrown. 126 */ 127 public SortedSet<K> headSet(K toKey) { 128 129 return subSet(null, false, toKey, false); 130 } 131 132 /** 133 * Returns a view of the portion of this sorted set whose elements are 134 * strictly less than toKey, optionally including toKey. 135 * This method does not exist in the standard {@link SortedSet} interface. 136 * 137 * <p>Note that the return value is a StoredCollection and must be treated 138 * as such; for example, its iterators must be explicitly closed.</p> 139 * 140 * @param toKey is the upper bound. 141 * 142 * @param toInclusive is true to include toKey. 143 * 144 * @return the subset. 145 * 146 * @throws RuntimeExceptionWrapper if a {@link 147 * com.sleepycat.db.DatabaseException} is thrown. 148 */ 149 public SortedSet<K> headSet(K toKey, boolean toInclusive) { 150 151 return subSet(null, false, toKey, toInclusive); 152 } 153 154 /** 155 * Returns a view of the portion of this sorted set whose elements are 156 * greater than or equal to fromKey. 157 * This method conforms to the {@link SortedSet#tailSet} interface. 158 * 159 * <p>Note that the return value is a StoredCollection and must be treated 160 * as such; for example, its iterators must be explicitly closed.</p> 161 * 162 * @param fromKey is the lower bound. 163 * 164 * @return the subset. 165 * 166 * @throws RuntimeExceptionWrapper if a {@link 167 * com.sleepycat.db.DatabaseException} is thrown. 168 */ 169 public SortedSet<K> tailSet(K fromKey) { 170 171 return subSet(fromKey, true, null, false); 172 } 173 174 /** 175 * Returns a view of the portion of this sorted set whose elements are 176 * strictly greater than fromKey, optionally including fromKey. 177 * This method does not exist in the standard {@link SortedSet} interface. 178 * 179 * <p>Note that the return value is a StoredCollection and must be treated 180 * as such; for example, its iterators must be explicitly closed.</p> 181 * 182 * @param fromKey is the lower bound. 183 * 184 * @param fromInclusive is true to include fromKey. 185 * 186 * @return the subset. 187 * 188 * @throws RuntimeExceptionWrapper if a {@link 189 * com.sleepycat.db.DatabaseException} is thrown. 190 */ 191 public SortedSet<K> tailSet(K fromKey, boolean fromInclusive) { 192 193 return subSet(fromKey, fromInclusive, null, false); 194 } 195 196 /** 197 * Returns a view of the portion of this sorted set whose elements range 198 * from fromKey, inclusive, to toKey, exclusive. 199 * This method conforms to the {@link SortedSet#subSet} 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 fromKey is the lower bound. 205 * 206 * @param toKey is the upper bound. 207 * 208 * @return the subset. 209 * 210 * @throws RuntimeExceptionWrapper if a {@link 211 * com.sleepycat.db.DatabaseException} is thrown. 212 */ 213 public SortedSet<K> subSet(K fromKey, K toKey) { 214 215 return subSet(fromKey, true, toKey, false); 216 } 217 218 /** 219 * Returns a view of the portion of this sorted set whose elements are 220 * strictly greater than fromKey and strictly less than toKey, 221 * optionally including fromKey and toKey. 222 * This method does not exist in the standard {@link SortedSet} interface. 223 * 224 * <p>Note that the return value is a StoredCollection and must be treated 225 * as such; for example, its iterators must be explicitly closed.</p> 226 * 227 * @param fromKey is the lower bound. 228 * 229 * @param fromInclusive is true to include fromKey. 230 * 231 * @param toKey is the upper bound. 232 * 233 * @param toInclusive is true to include toKey. 234 * 235 * @return the subset. 236 * 237 * @throws RuntimeExceptionWrapper if a {@link 238 * com.sleepycat.db.DatabaseException} is thrown. 239 */ 240 public SortedSet<K> subSet(K fromKey, 241 boolean fromInclusive, 242 K toKey, 243 boolean toInclusive) { 244 try { 245 return new StoredSortedKeySet( 246 view.subView(fromKey, fromInclusive, toKey, toInclusive, null)); 247 } catch (Exception e) { 248 throw StoredContainer.convertException(e); 249 } 250 } 251} 252