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