⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistentmapimpl.java

📁 这个是perst-269.zip下面的SOURCECODE,和大家分享了。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                Object value = e.getValue();
                if (value == null) {
                    if (!(t.get(key)==null && t.containsKey(key))) {
                        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 i = entrySet().iterator();
	while (i.hasNext()) {
	    h += i.next().hashCode();
        }
	return h;
    }

    public String toString() {
	StringBuffer buf = new StringBuffer();
	buf.append("{");

	Iterator i = entrySet().iterator();
        boolean hasNext = i.hasNext();
        while (hasNext) {
	    Entry e = (Entry)i.next();
	    Object key = e.getKey();
            Object 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 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 comparator() {
        return null;
    }

    public SortedMap subMap(Object from, Object to) {
        if (((Comparable)from).compareTo(to) > 0) {
            throw new IllegalArgumentException("from > to");
        }
        return new SubMap(from, to);
    }

    public SortedMap headMap(Object to) {
        return new SubMap(null, to);
    }

    public SortedMap tailMap(Object from) {
        return new SubMap(from, null);
    }

    private class SubMap extends AbstractMap implements SortedMap {
        private Key fromKey;
        private Key toKey;
        private Object from;
        private Object to;
        volatile Set   entrySet;

        SubMap(Object from, Object 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(key) && PersistentMapImpl.this.containsKey(key);
        }

        public Object get(Object key) {
            if (!inRange(key)) {
                return null;
            }
            return PersistentMapImpl.this.get(key);
        }

        public Object put(Object key, Object value) {
            if (!inRange(key)) {
                throw new IllegalArgumentException("key out of range");
            }
            return PersistentMapImpl.this.put(key, value);
        }

        public Comparator comparator() {
            return null;
        }

        public Object firstKey() {
            return ((Entry)entryIterator(Index.ASCENT_ORDER).next()).getKey();
        }

        public Object lastKey() {
            return ((Entry)entryIterator(Index.DESCENT_ORDER).next()).getKey();
        }

        protected Iterator entryIterator(final int order) {
            if (index != null) { 
                return index.entryIterator(fromKey, toKey, order);
            } else {
                if (order == Index.ASCENT_ORDER) { 
                    final int beg = (from != null ? binarySearch(from) : 0) - 1;
                    final int end = values.size();
                    
                    return new Iterator() {                     
                        private int i = beg;
                            
                        public boolean hasNext() {
                            return i+1 < end && (to == null || ((Comparable[])keys)[i+1].compareTo(to) < 0);
                        }
                            
                        public Object next() {
                            if (!hasNext()) { 
                                throw new NoSuchElementException(); 
                            }
                            i += 1;
                            return new Entry() {
                                public Object getKey() { 
                                    return ((Comparable[])keys)[i];
                                }
                                public Object getValue() { 
                                    return values.get(i);
                                }
                                public Object setValue(Object 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.remove(i);
                            i -= 1;
                        }
                    };
                } else {
                    final int beg = to != null ? binarySearch(to) : values.size();
                    
                    return new Iterator() {                     
                        private int i = beg;
                            
                        public boolean hasNext() {
                            return i > 0 && (from == null || ((Comparable[])keys)[i-1].compareTo(from) >= 0);
                        }
                            
                        public Object next() {
                            if (!hasNext()) { 
                                throw new NoSuchElementException(); 
                            }
                            i -= 1;
                            return new Entry() {
                                public Object getKey() { 
                                    return ((Comparable[])keys)[i];
                                }
                                public Object getValue() { 
                                    return values.get(i);
                                }
                                public Object setValue(Object 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.remove(i);
                        }
                   };
                }
            }
        }

        public Set entrySet() {
            if (entrySet == null) {
                entrySet = new AbstractSet() {
                    public Iterator iterator() {
                        return entryIterator(Index.ASCENT_ORDER);
                    }
                    
                    public int size() {
                        Iterator 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 entry = (Map.Entry)o;
                        Object key = entry.getKey();
                        if (!inRange(key)) {
                            return false;
                        }
                        Object value = entry.getValue();
                        if (value != null) { 
                            Object 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 e = (Entry)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 subMap(Object from, Object 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 headMap(Object to) {
            if (!inRange2(to)) {
                throw new IllegalArgumentException("'to' out of range");
            }
            return new SubMap(this.from, to);
        }

        public SortedMap tailMap(Object from) {
            if (!inRange2(from)) {
                throw new IllegalArgumentException("'from' out of range");
            }
            return new SubMap(from, this.to);
        }

        private boolean inRange(Object key) {
            Comparable k = (Comparable)key;
            return (from == null || k.compareTo(from) >= 0) &&
                (to == null || k.compareTo(to) < 0);
        }

        // This form allows the high endpoint (as well as all legit keys)
        private boolean inRange2(Object key) {
            Comparable k = (Comparable)key;
            return (from == null || k.compareTo(from) >= 0) &&
                (to == null || k.compareTo(to) <= 0);
        }
    }

    public Object firstKey() {
        if (index != null) { 
            return ((Entry)index.entryIterator().next()).getKey();
        } else { 
            Comparable[] keys = (Comparable[])this.keys;
            if (values.size() == 0) {
                throw new NoSuchElementException(); 
            }
            return ((Comparable[])keys)[0];
        }
    }

    public Object lastKey() {
        if (index != null) { 
            return ((Entry)index.entryIterator(null, null, Index.DESCENT_ORDER).next()).getKey();
        } else { 
            int size = values.size();
            if (size == 0) {
                throw new NoSuchElementException(); 
            }
            return ((Comparable[])keys)[size-1];
        }
    }

    public Iterator select(Class cls, String predicate) { 
        Query query = new QueryImpl(getStorage());
        return query.select(cls, values().iterator(), predicate);
    }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -