📄 _basetreemap_submapimpl.java
字号:
/* * _BaseTreeSet_SubMap.java * $Id: _BaseTreeMap_SubMapImpl.java,v 1.5 2003/11/20 23:18:41 per_nyfelt Exp $ * This file is based on TreeMap.java from GNU Classpath. Quote:TreeMap.java -- a class providing a basic Red-Black Tree data structure,mapping Object --> ObjectCopyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. * end quote. * * This file is licenced under the same conditions as its original (GPL + * "special exception"). */package org.ozoneDB.collections;import java.util.Collection;import java.util.Comparator;import java.util.Iterator;import java.util.Map;import java.util.NoSuchElementException;import java.util.Set;import java.util.SortedMap;import java.util.TreeMap;/** * <p>Do not use this class directly. * This should be an inner class; unfortunately ozone does not support those yet.</p> * <p>Implementation of {@link #subMap(Object, Object)} and other map * ranges. This class provides a view of a portion of the original backing * map, and throws {@link IllegalArgumentException} for attempts to * access beyond that range.</p> * * @author Eric Blake <ebb9@email.byu.edu> * @author <a href="mailto:ozoneATmekenkampD0Tcom">Leo Mekenkamp (mind the anti-sp@m)</a> (adaptation for ozone) */public class _BaseTreeMap_SubMapImpl extends AbstractOzoneMap implements _BaseTreeMap_SubMap { private static final long serialVersionUID = 1L; /** * underlying BaseTreeMap. */ protected BaseTreeMap owner; /** * The lower range of this view, inclusive, or nil for unbounded. * Package visible for use by nested classes. */ final Object minKey; /** * The upper range of this view, exclusive, or nil for unbounded. * Package visible for use by nested classes. */ final Object maxKey; /** * The cache for {@link #entrySet()}. */ private OzoneSet entries; /** * Create a SubMap representing the elements between minKey (inclusive) * and maxKey (exclusive). If minKey is nil, SubMap has no lower bound * (headMap). If maxKey is nil, the SubMap has no upper bound (tailMap). * * @param minKey the lower bound * @param maxKey the upper bound * @throws IllegalArgumentException if minKey > maxKey */ public _BaseTreeMap_SubMapImpl(BaseTreeMap owner, Object minKey, Object maxKey) { this.owner = owner; if ((minKey instanceof BaseTreeMap.Node) && (maxKey instanceof BaseTreeMap.Node) && !((BaseTreeMap.Node) minKey).isNil() && !((BaseTreeMap.Node) maxKey).isNil() && owner._org_ozoneDB_compare(minKey, maxKey) > 0) throw new IllegalArgumentException("fromKey > toKey"); this.minKey = minKey; this.maxKey = maxKey; } /** * Check if "key" is in within the range bounds for this SubMap. The * lower ("from") SubMap range is inclusive, and the upper ("to") bound * is exclusive. Package visible for use by nested classes. * * @param key the key to check * @return true if the key is in range */ public final boolean keyInRange(Object key) { return (((minKey instanceof BaseTreeMap.Node && ((BaseTreeMap.Node) minKey).isNil()) || owner._org_ozoneDB_compare(key, minKey) >= 0) && ((maxKey instanceof BaseTreeMap.Node && ((BaseTreeMap.Node) maxKey).isNil()) || owner._org_ozoneDB_compare(key, maxKey) < 0)); } public void clear() { BaseTreeMap.Node next = owner._org_ozoneDB_lowestGreaterThan(minKey, true); BaseTreeMap.Node max = owner._org_ozoneDB_lowestGreaterThan(maxKey, false); while (!next.equals(max)) { BaseTreeMap.Node current = next; next = owner._org_ozoneDB_successor(current); owner._org_ozoneDB_removeNode(current); } } public Comparator comparator() { return owner.comparator(); } public boolean containsKey(Object key) { return keyInRange(key) && owner.containsKey(key); } public boolean containsValue(Object value) { BaseTreeMap.Node node = owner._org_ozoneDB_lowestGreaterThan(minKey, true); BaseTreeMap.Node max = owner._org_ozoneDB_lowestGreaterThan(maxKey, false); while (node != max) { if (equals(value, node.getValue())) return true; node = owner._org_ozoneDB_successor(node); } return false; } public Set entrySet() { if (entries == null) {// TODO: when FakeFactoryGenerator is finished replace with// entries = BaseTreeMap_SubMap_entrySetFactory.getDefault.create(self()); entries = (OzoneSet) database().createObject( _BaseTreeMap_SubMap_entrySet.class, new Class[] {_BaseTreeMap_SubMap.class}, new Object[] {self()}); } return entries; } public Object firstKey() { BaseTreeMap.Node node = owner._org_ozoneDB_lowestGreaterThan(minKey, true); if (node.isNil() || ! keyInRange(node.getKey())) throw new NoSuchElementException(); return node.getKey(); } public Object get(Object key) { if (keyInRange(key)) return owner.get(key); return null; } public SortedMap headMap(Object toKey) { if (! keyInRange(toKey)) throw new IllegalArgumentException("key outside range");// TODO: replace when FakeFactoryGenerator is ready// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, minKey, toKey); return (SortedMap) database().createObject( _BaseTreeMap_SubMap.class, new Class[] {BaseTreeMap.class, Object.class, Object.class}, new Object[] {owner, minKey, toKey} ); } public Set keySet() { if (keys == null) {// TODO: replace when FakeFactoryGenerator is ready// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, minKey, toKey); keys = (Set) database().createObject( _BaseTreeMap_SubMap_keySet.class, new Class[] {_BaseTreeMap_SubMap.class}, new Object[] {self()} ); } return keys; } public Object lastKey() { BaseTreeMap.Node node = owner._org_ozoneDB_highestLessThan(maxKey); if (node.isNil() || ! keyInRange(node.getKey())) throw new NoSuchElementException(); return node.getKey(); } public Object put(Object key, Object value) { if (! keyInRange(key)) throw new IllegalArgumentException("Key outside range"); return owner.put(key, value); } public Object remove(Object key) { if (keyInRange(key)) return owner.remove(key); return null; } public int size() { BaseTreeMap.Node node = owner._org_ozoneDB_lowestGreaterThan(minKey, true); BaseTreeMap.Node max = owner._org_ozoneDB_lowestGreaterThan(maxKey, false); int count = 0; while (!node.equals(max)) { count++; node = owner._org_ozoneDB_successor(node); } return count; } public SortedMap subMap(Object fromKey, Object toKey) { if (! keyInRange(fromKey) || ! keyInRange(toKey)) throw new IllegalArgumentException("key outside range");// TODO: replace when FakeFactoryGenerator is ready// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, fromKey, toKey); return (SortedMap) database().createObject( _BaseTreeMap_SubMap.class, new Class[] {BaseTreeMap.class, Object.class, Object.class}, new Object[] {owner, fromKey, toKey} ); } public SortedMap tailMap(Object fromKey) { if (! keyInRange(fromKey)) throw new IllegalArgumentException("key outside range");// TODO: replace when FakeFactoryGenerator is ready// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, fromKey, maxKey); return (SortedMap) database().createObject( _BaseTreeMap_SubMap.class, new Class[] {BaseTreeMap.class, Object.class, Object.class}, new Object[] {owner, fromKey, maxKey} ); } public Collection values() { if (values == null) {// TODO: replace when FakeFactoryGenerator is ready// BaseTreeMapImpl_SubMapFactory.getDefault.create(owner, minKey, toKey); values = (OzoneCollection) database().createObject( _BaseTreeMap_SubMap_values.class, new Class[] {_BaseTreeMap_SubMap.class}, new Object[] {self()} ); } return values; } /** <p>Returns a <code>Map</code> that contains the same entries as this * persistent one; it is (by nature of the client-server enviromnent) always * a 'deep' copy of this <code>OzoneMap</code>. I.e. the contents of * this <code>OzoneMap</code> instance are always copied to the client * by use of serialization.</p> * */ public Map getClientMap() { return getClientSortedMap(); } /** <p>Returns a <code>SortedMap</code> that contains the same entries as this * persistent one; it is (by nature of the client-server enviromnent) always * a 'deep' copy of this <code>OzoneSortedMap</code>. I.e. the contents of * this <code>OzoneSortedMap</code> instance are always copied to the client * by use of serialization.</p> * */ public SortedMap getClientSortedMap() { TreeMap result = comparator() == null ? new TreeMap() : new TreeMap(comparator()); for (Iterator i = ((OzoneSet) entrySet())._org_ozoneDB_internalIterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry) i.next(); result.put(entry.getKey(), entry.getValue()); } return result; } public Object getMinKey() { return minKey; } public Object getMaxKey() { return maxKey; } public BaseTreeMap getOwner() { return owner; } /** <p>Basically nothing more than a typecasted <code>HeadMap</code> method. * Because subsets are also <code>OzoneSortedMap</code>s, this method is * provided to do away with the need for a typecast.</p> * */ public OzoneSortedMap ozoneHeadMap(Object toKey) { return (OzoneSortedMap) headMap(toKey); } /** <p>Basically nothing more than a typecasted <code>SubMap</code> method. * Because subsets are also <code>OzoneSortedMap</code>s, this method is * provided to do away with the need for a typecast.</p> * */ public OzoneSortedMap ozoneSubMap(Object fromKey, Object toKey) { return (OzoneSortedMap) subMap(fromKey, toKey); } /** <p>Basically nothing more than a typecasted <code>TailMap</code> method.</p> * Because subsets are also <code>OzoneSortedMap</code>s, this method is * provided to do away with the need for a typecast.</p> * */ public OzoneSortedMap ozoneTailMap(Object toKey) { return (OzoneSortedMap) tailMap(toKey); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -