📄 persistentmapimpl.java
字号:
return false;
}
} else {
if (!value.equals(t.get(key))) {
return false;
}
}
}
} catch(ClassCastException unused) {
return false;
} catch(NullPointerException unused) {
return false;
}
return true;
}
public int hashCode() {
int h = 0;
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext()) {
h += i.next().hashCode();
}
return h;
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("{");
Iterator<Entry<K,V>> i = entrySet().iterator();
boolean hasNext = i.hasNext();
while (hasNext) {
Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
if (key == this) {
buf.append("(this Map)");
} else {
buf.append(key);
}
buf.append("=");
if (value == this) {
buf.append("(this Map)");
} else {
buf.append(value);
}
hasNext = i.hasNext();
if (hasNext) {
buf.append(", ");
}
}
buf.append("}");
return buf.toString();
}
final Key generateKey(Object key) {
return generateKey(key, true);
}
final Key generateKey(Object key, boolean inclusive) {
if (key instanceof Integer) {
return new Key(((Integer)key).intValue(), inclusive);
} else if (key instanceof Byte) {
return new Key(((Byte)key).byteValue(), inclusive);
} else if (key instanceof Character) {
return new Key(((Character)key).charValue(), inclusive);
} else if (key instanceof Short) {
return new Key(((Short)key).shortValue(), inclusive);
} else if (key instanceof Long) {
return new Key(((Long)key).longValue(), inclusive);
} else if (key instanceof Float) {
return new Key(((Float)key).floatValue(), inclusive);
} else if (key instanceof Double) {
return new Key(((Double)key).doubleValue(), inclusive);
} else if (key instanceof String) {
return new Key((String)key, inclusive);
} else if (key instanceof Enum) {
return new Key((Enum)key, inclusive);
} else if (key instanceof java.util.Date) {
return new Key((java.util.Date)key, inclusive);
} else if (key instanceof IPersistent) {
return new Key((IPersistent)key, inclusive);
} else if (key instanceof Comparable) {
return new Key((Comparable)key, inclusive);
} else if (key == null) {
return new Key((IPersistent)key, inclusive);
} else {
throw new StorageError(StorageError.UNSUPPORTED_INDEX_TYPE, key.getClass());
}
}
public Comparator<? super K> comparator() {
return null;
}
public SortedMap<K,V> subMap(K from, K to) {
if (from.compareTo(to) > 0) {
throw new IllegalArgumentException("from > to");
}
return new SubMap(from, to);
}
public SortedMap<K,V> headMap(K to) {
return new SubMap(null, to);
}
public SortedMap<K,V> tailMap(K from) {
return new SubMap(from, null);
}
private class SubMap extends AbstractMap<K,V> implements SortedMap<K,V> {
private Key fromKey;
private Key toKey;
private K from;
private K to;
volatile Set<Entry<K,V>> entrySet;
SubMap(K from, K to) {
this.from = from;
this.to = to;
this.fromKey = from != null ? generateKey(from, true) : null;
this.toKey = to != null ? generateKey(to, false) : null;
}
public boolean isEmpty() {
return entrySet().isEmpty();
}
public boolean containsKey(Object key) {
return inRange((K)key) && PersistentMapImpl.this.containsKey(key);
}
public V get(Object key) {
if (!inRange((K)key)) {
return null;
}
return PersistentMapImpl.this.get(key);
}
public V put(K key, V value) {
if (!inRange(key)) {
throw new IllegalArgumentException("key out of range");
}
return PersistentMapImpl.this.put(key, value);
}
public Comparator<? super K> comparator() {
return null;
}
public K firstKey() {
return entryIterator(Index.ASCENT_ORDER).next().getKey();
}
public K lastKey() {
return entryIterator(Index.DESCENT_ORDER).next().getKey();
}
protected Iterator<Entry<K,V>> entryIterator(final int order) {
if (index != null) {
return new Iterator<Entry<K,V>>() {
private Iterator<Entry<Object,V>> i = index.entryIterator(fromKey, toKey, order);
public boolean hasNext() {
return i.hasNext();
}
public Entry<K,V> next() {
final Entry<Object,V> e = i.next();
return new Entry<K,V>() {
public K getKey() {
return (K)e.getKey();
}
public V getValue() {
return e.getValue();
}
public V setValue(V value) {
throw new UnsupportedOperationException("Entry.Map.setValue");
}
};
}
public void remove() {
i.remove();
}
};
} else {
if (order == Index.ASCENT_ORDER) {
final int beg = (from != null ? binarySearch(from) : 0) - 1;
final int end = values.size();
return new Iterator<Entry<K,V>>() {
private int i = beg;
public boolean hasNext() {
return i+1 < end && (to == null || ((Comparable[])keys)[i+1].compareTo(to) < 0);
}
public Entry<K,V> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
i += 1;
return new Entry<K,V>() {
public K getKey() {
return (K)((Comparable[])keys)[i];
}
public V getValue() {
return values.get(i);
}
public V setValue(V value) {
throw new UnsupportedOperationException("Entry.Map.setValue");
}
};
}
public void remove() {
if (i < 0) {
throw new IllegalStateException();
}
int size = values.size();
System.arraycopy(keys, i+1, keys, i, size-i-1);
((Comparable[])keys)[size-1] = null;
values.removeObject(i);
i -= 1;
}
};
} else {
final int beg = (to != null ? binarySearch(to) : 0) - 1;
return new Iterator<Entry<K,V>>() {
private int i = beg;
public boolean hasNext() {
return i > 0 && (from == null || ((Comparable[])keys)[i-1].compareTo(from) >= 0);
}
public Entry<K,V> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
i -= 1;
return new Entry<K,V>() {
public K getKey() {
return (K)((Comparable[])keys)[i];
}
public V getValue() {
return values.get(i);
}
public V setValue(V value) {
throw new UnsupportedOperationException("Entry.Map.setValue");
}
};
}
public void remove() {
if (i < 0) {
throw new IllegalStateException();
}
int size = values.size();
System.arraycopy(keys, i+1, keys, i, size-i-1);
((Comparable[])keys)[size-1] = null;
values.removeObject(i);
}
};
}
}
}
public Set<Entry<K,V>> entrySet() {
if (entrySet == null) {
entrySet = new AbstractSet<Entry<K,V>>() {
public Iterator<Entry<K,V>> iterator() {
return entryIterator(Index.ASCENT_ORDER);
}
public int size() {
Iterator<Entry<K,V>> i = iterator();
int n;
for (n = 0; i.hasNext(); i.next()) {
n += 1;
}
return n;
}
public boolean isEmpty() {
return !iterator().hasNext();
}
public boolean remove(Object o) {
if (!(o instanceof Map.Entry)) {
return false;
}
Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
K key = entry.getKey();
if (!inRange(key)) {
return false;
}
V value = entry.getValue();
if (value != null) {
V v = PersistentMapImpl.this.get(key);
if (value.equals(v)) {
PersistentMapImpl.this.remove(key);
return true;
}
} else {
if (PersistentMapImpl.this.containsKey(key)
&& PersistentMapImpl.this.get(key) == null)
{
PersistentMapImpl.this.remove(key);
return true;
}
}
return false;
}
public boolean contains(Object k) {
Entry<K,V> e = (Entry<K,V>)k;
if (!inRange(e.getKey())) {
return false;
}
if (e.getValue() != null) {
return e.getValue().equals(PersistentMapImpl.this.get(e.getKey()));
} else {
return PersistentMapImpl.this.containsKey(e.getKey())
&& PersistentMapImpl.this.get(e.getKey()) == null;
}
}
};
}
return entrySet;
}
public SortedMap<K,V> subMap(K from, K to) {
if (!inRange2(from)) {
throw new IllegalArgumentException("'from' out of range");
}
if (!inRange2(to)) {
throw new IllegalArgumentException("'to' out of range");
}
return new SubMap(from, to);
}
public SortedMap<K,V> headMap(K to) {
if (!inRange2(to)) {
throw new IllegalArgumentException("'to' out of range");
}
return new SubMap(this.from, to);
}
public SortedMap<K,V> tailMap(K from) {
if (!inRange2(from)) {
throw new IllegalArgumentException("'from' out of range");
}
return new SubMap(from, this.to);
}
private boolean inRange(K key) {
return (from == null || key.compareTo(from) >= 0) &&
(to == null || key.compareTo(to) < 0);
}
// This form allows the high endpoint (as well as all legit keys)
private boolean inRange2(K key) {
return (from == null || key.compareTo(from) >= 0) &&
(to == null || key.compareTo(to) <= 0);
}
}
public K firstKey() {
if (index != null) {
return (K)index.entryIterator().next().getKey();
} else {
Comparable[] keys = (Comparable[])this.keys;
if (values.size() == 0) {
throw new NoSuchElementException();
}
return (K)((Comparable[])keys)[0];
}
}
public K lastKey() {
if (index != null) {
return (K)index.entryIterator(null, null, Index.DESCENT_ORDER).next().getKey();
} else {
int size = values.size();
if (size == 0) {
throw new NoSuchElementException();
}
return (K)((Comparable[])keys)[size-1];
}
}
public Iterator<V> select(Class cls, String predicate) {
Query<V> query = new QueryImpl<V>(getStorage());
return query.select(cls, values().iterator(), predicate);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -