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