Example usage for java.util AbstractSet AbstractSet

List of usage examples for java.util AbstractSet AbstractSet

Introduction

In this page you can find the example usage for java.util AbstractSet AbstractSet.

Prototype

protected AbstractSet() 

Source Link

Document

Sole constructor.

Usage

From source file:com.github.helenusdriver.driver.impl.PersistedMap.java

/**
 * {@inheritDoc}//from   ww  w.j  a v  a  2s  . c o m
 *
 * @author paouelle
 *
 * @see java.util.Map#entrySet()
 */
@Override
public Set<Map.Entry<K, T>> entrySet() {
    if (eset == null) {
        final Set<Map.Entry<K, PersistedValue<T, PT>>> eset = map.entrySet();

        this.eset = new AbstractSet<Map.Entry<K, T>>() {
            @Override
            public int size() {
                return eset.size();
            }

            @Override
            public boolean isEmpty() {
                return eset.isEmpty();
            }

            @Override
            public Iterator<Map.Entry<K, T>> iterator() {
                return new TransformIterator<Map.Entry<K, PersistedValue<T, PT>>, Map.Entry<K, T>>(
                        eset.iterator()) {
                    @Override
                    protected Map.Entry<K, T> transform(Map.Entry<K, PersistedValue<T, PT>> me) {
                        return new Entry(me);
                    }
                };
            }

            @Override
            public Stream<Map.Entry<K, T>> stream() {
                return eset.stream().map(me -> new Entry(me));
            }

            @Override
            public Stream<Map.Entry<K, T>> parallelStream() {
                return eset.parallelStream().map(me -> new Entry(me));
            }

            @Override
            public boolean remove(Object o) {
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                @SuppressWarnings("unchecked")
                final Map.Entry<K, T> me = (Map.Entry<K, T>) o;

                return (map.remove(me.getKey()) != null);
            }

            @Override
            public void clear() {
                eset.clear();
            }

            @Override
            public String toString() {
                return eset.toString();
            }
        };
    }
    return eset;
}

From source file:org.apache.openjpa.util.AbstractLRSProxyMap.java

public Set<Map.Entry<K, V>> entrySet() {
    return new AbstractSet<Map.Entry<K, V>>() {
        public int size() {
            return AbstractLRSProxyMap.this.size();
        }/* ww  w .ja v  a2s  .  c  om*/

        public Iterator<Map.Entry<K, V>> iterator() {
            return AbstractLRSProxyMap.this.iterator(MODE_ENTRY);
        }
    };
}

From source file:org.briljantframework.data.vector.AbstractVector.java

@Override
public <T> Set<Pair<Object, T>> indexSet(Class<T> cls) {
    return new AbstractSet<Pair<Object, T>>() {
        @Override/*from  ww w .  j a v a  2  s .  c  o m*/
        public Iterator<Pair<Object, T>> iterator() {
            return new Iterator<Pair<Object, T>>() {
                Iterator<Object> keys = getIndex().iterator();

                @Override
                public boolean hasNext() {
                    return keys.hasNext();
                }

                @Override
                public Pair<Object, T> next() {
                    Object key = keys.next();
                    return new ImmutablePair<>(key, get(cls, key));
                }
            };
        }

        @Override
        public int size() {
            return AbstractVector.this.size();
        }
    };
}

From source file:Cache.java

public synchronized Set<Map.Entry<K, V>> entrySet() {
    // Warning -- this method returns CacheObject instances and not Objects
    // in the same form they were put into cache.

    // First, clear all entries that have been in cache longer than the
    // maximum defined age.
    deleteExpiredEntries();/*from   w ww.  java  2 s .  co  m*/

    return new AbstractSet<Map.Entry<K, V>>() {
        private final Set<Map.Entry<K, CacheObject<V>>> set = map.entrySet();

        public Iterator<Entry<K, V>> iterator() {
            return new Iterator<Entry<K, V>>() {
                private final Iterator<Entry<K, CacheObject<V>>> it = set.iterator();

                public boolean hasNext() {
                    return it.hasNext();
                }

                public Entry<K, V> next() {
                    Map.Entry<K, CacheObject<V>> entry = it.next();
                    return new AbstractMapEntry<K, V>(entry.getKey(), entry.getValue().object) {
                        @Override
                        public V setValue(V value) {
                            throw new UnsupportedOperationException("Cannot set");
                        }
                    };
                }

                public void remove() {
                    it.remove();
                }
            };

        }

        public int size() {
            return set.size();
        }
    };
}

From source file:IntHashMap.java

/**
 * Returns a set view of the keys contained in this map.  The set is
 * backed by the map, so changes to the map are reflected in the set, and
 * vice-versa.  The set supports element removal, which removes the
 * corresponding mapping from this map, via the <tt>Iterator.remove</tt>,
 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
 * <tt>clear</tt> operations.  It does not support the <tt>add</tt> or
 * <tt>addAll</tt> operations.
 *
 * @return a set view of the keys contained in this map.
 *///from w  w  w. j  a  v a 2s .com
public Set<Integer> keySet() {
    if (keySet == null) {
        keySet = new AbstractSet<Integer>() {
            public Iterator iterator() {
                return new IntHashIterator(KEYS);
            }

            public int size() {
                return count;
            }

            public boolean contains(Object o) {
                return containsKey(o);
            }

            public boolean remove(Object o) {
                return IntHashMap.this.remove(o) != null;
            }

            public void clear() {
                IntHashMap.this.clear();
            }
        };
    }
    return keySet;
}

From source file:ArraySet.java

@Override
public final Set<Map.Entry<K, V>> entrySet() {
    if (entrySet == null) {
        entrySet = new AbstractSet<Map.Entry<K, V>>() {
            @Override//  w  w  w.j  av  a  2  s . co m
            public Iterator<Map.Entry<K, V>> iterator() {
                return new ArrayMapIterator();
            }

            @Override
            @SuppressWarnings("unchecked")
            public boolean contains(Object o) {
                if (!(o instanceof Entry)) {
                    return false;
                }
                Entry<K, V> entry = (Entry<K, V>) o;
                int index = (entry.hashCode & 0x7FFFFFFF) % mapTable.length;
                for (Entry<K, V> e = mapTable[index]; e != null; e = e.next) {
                    if (e.equals(entry)) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            @SuppressWarnings("unchecked")
            public boolean remove(Object o) {
                if (!(o instanceof Entry)) {
                    return false;
                }
                Entry<K, V> entry = (Entry) o;
                return ArrayMap.this.remove(entry.key) != null;
            }

            @Override
            public int size() {
                return size;
            }

            @Override
            public void clear() {
                ArrayMap.this.clear();
            }
        };
    }
    return entrySet;
}

From source file:WeakIdentityMap.java

public Set<K> keySet() {
    if (this.keySet == null) {
        this.keySet = new AbstractSet<K>() {
            public Iterator iterator() {
                return createHashIterator(KEYS);
            }/*from  www  .  ja v  a  2  s .c om*/

            public int size() {
                return WeakIdentityMap.this.count;
            }

            public boolean contains(Object o) {
                return containsKey(o);
            }

            public boolean remove(Object o) {
                return o == null ? false : WeakIdentityMap.this.remove(o) == o;
            }

            public void clear() {
                WeakIdentityMap.this.clear();
            }

            public String toString() {
                return WeakIdentityMap.this.toString(this);
            }
        };
    }
    return this.keySet;
}

From source file:IntHashMap.java

/**
 * Returns a set view of the keys contained in this map. The set is backed by
 * the map, so changes to the map are reflected in the set, and vice-versa.
 * The set supports element removal, which removes the corresponding mapping
 * from this map, via the <code>Iterator.remove</code>,
 * <code>Set.remove</code>, <code>removeAll</code>, <code>retainAll</code>,
 * and <code>clear</code> operations. It does not support the
 * <code>add</code> or <code>addAll</code> operations.
 *
 * @return a set view of the keys contained in this map.
 *///from  ww  w.j  a  va  2 s  .  com
@Override
public Set keySet() {
    if (keySet == null) {
        keySet = new AbstractSet() {
            @Override
            public Iterator iterator() {
                return new IntHashIterator(KEYS);
            }

            @Override
            public int size() {
                return count;
            }

            @Override
            public boolean contains(Object o) {
                return containsKey(o);
            }

            @Override
            public boolean remove(Object o) {
                return IntHashMap.this.remove(o) != null;
            }

            @Override
            public void clear() {
                IntHashMap.this.clear();
            }
        };
    }
    return keySet;
}

From source file:IdentityHashMap.java

/**
 * Returns a set view of the keys contained in this map.  The set is
 * backed by the map, so changes to the map are reflected in the set, and
 * vice-versa.  The set supports element removal, which removes the
 * corresponding mapping from this map, via the <tt>Iterator.remove</tt>,
 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
 * <tt>clear</tt> operations.  It does not support the <tt>add</tt> or
 * <tt>addAll</tt> operations.
 *
 * @return a set view of the keys contained in this map.
 *//*w w w. j a  va  2s. c  om*/
public Set keySet() {
    if (keySet == null) {
        keySet = new AbstractSet() {
            public Iterator iterator() {
                return getHashIterator(KEYS);
            }

            public int size() {
                return count;
            }

            public boolean contains(Object o) {
                return containsKey(o);
            }

            public boolean remove(Object o) {
                int oldSize = count;
                IdentityHashMap.this.remove(o);
                return count != oldSize;
            }

            public void clear() {
                IdentityHashMap.this.clear();
            }
        };
    }
    return keySet;
}

From source file:WeakIdentityMap.java

public Set<Map.Entry<K, V>> entrySet() {
    if (this.entrySet == null) {
        this.entrySet = new AbstractSet<Map.Entry<K, V>>() {
            public Iterator<Map.Entry<K, V>> iterator() {
                return createHashIterator(ENTRIES);
            }//from w  ww  .  j a  v a  2  s. co  m

            public boolean contains(Object o) {
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                Map.Entry entry = (Map.Entry) o;
                Object key = entry.getKey();

                Entry[] tab = WeakIdentityMap.this.table;
                int hash = System.identityHashCode(key);
                int index = (hash & 0x7fffffff) % tab.length;

                for (Entry e = tab[index], prev = null; e != null; e = e.next) {
                    Object entryKey = e.get();

                    if (entryKey == null) {
                        // Clean up after a cleared Reference.
                        WeakIdentityMap.this.modCount++;
                        if (prev != null) {
                            prev.next = e.next;
                        } else {
                            tab[index] = e.next;
                        }
                        WeakIdentityMap.this.count--;
                    } else if (e.hash == hash && e.equals(entry)) {
                        return true;
                    } else {
                        prev = e;
                    }
                }

                return false;
            }

            public boolean remove(Object o) {
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                Map.Entry entry = (Map.Entry) o;
                Object key = entry.getKey();
                Entry[] tab = WeakIdentityMap.this.table;
                int hash = System.identityHashCode(key);
                int index = (hash & 0x7fffffff) % tab.length;

                for (Entry e = tab[index], prev = null; e != null; e = e.next) {
                    if (e.get() == null) {
                        // Clean up after a cleared Reference.
                        WeakIdentityMap.this.modCount++;
                        if (prev != null) {
                            prev.next = e.next;
                        } else {
                            tab[index] = e.next;
                        }
                        WeakIdentityMap.this.count--;
                    } else if (e.hash == hash && e.equals(entry)) {
                        WeakIdentityMap.this.modCount++;
                        if (prev != null) {
                            prev.next = e.next;
                        } else {
                            tab[index] = e.next;
                        }
                        WeakIdentityMap.this.count--;

                        e.value = null;
                        return true;
                    } else {
                        prev = e;
                    }
                }
                return false;
            }

            public int size() {
                return WeakIdentityMap.this.count;
            }

            public void clear() {
                WeakIdentityMap.this.clear();
            }

            public String toString() {
                return WeakIdentityMap.toString(this);
            }
        };
    }

    return this.entrySet;
}