Search in sources :

Example 11 with AbstractSet

use of java.util.AbstractSet in project bnd by bndtools.

the class PersistentMap method entrySet.

public Set<java.util.Map.Entry<String, V>> entrySet() {
    return new AbstractSet<Map.Entry<String, V>>() {

        public int size() {
            init();
            return cache.size();
        }

        public Iterator<java.util.Map.Entry<String, V>> iterator() {
            init();
            return new Iterator<Map.Entry<String, V>>() {

                Iterator<java.util.Map.Entry<String, SoftReference<V>>> it = cache.entrySet().iterator();

                java.util.Map.Entry<String, SoftReference<V>> entry;

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

                @SuppressWarnings("unchecked")
                public java.util.Map.Entry<String, V> next() {
                    try {
                        entry = it.next();
                        SoftReference<V> ref = entry.getValue();
                        V value = null;
                        if (ref != null)
                            value = ref.get();
                        if (value == null) {
                            File file = new File(data, entry.getKey());
                            value = (V) codec.dec().from(file).get(type);
                            entry.setValue(new SoftReference<V>(value));
                        }
                        final V v = value;
                        return new Map.Entry<String, V>() {

                            public String getKey() {
                                return entry.getKey();
                            }

                            public V getValue() {
                                return v;
                            }

                            public V setValue(V value) {
                                return put(entry.getKey(), value);
                            }
                        };
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }

                public void remove() {
                    PersistentMap.this.remove(entry.getKey());
                }
            };
        }
    };
}
Also used : AbstractSet(java.util.AbstractSet) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IOException(java.io.IOException) SoftReference(java.lang.ref.SoftReference) Iterator(java.util.Iterator) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 12 with AbstractSet

use of java.util.AbstractSet in project freemarker by apache.

the class IdentityHashMap method entrySet.

/**
 * Returns a collection view of the mappings contained in this map.  Each
 * element in the returned collection is a <tt>Map.Entry</tt>.  The
 * collection is backed by the map, so changes to the map are reflected in
 * the collection, and vice versa.  The collection supports element
 * removal, which removes the corresponding mapping from the map, via the
 * <tt>Iterator.remove</tt>, <tt>Collection.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 collection view of the mappings contained in this map.
 * @see java.util.Map.Entry
 */
@Override
public Set entrySet() {
    if (entrySet == null) {
        entrySet = new AbstractSet() {

            @Override
            public Iterator iterator() {
                return getHashIterator(ENTRIES);
            }

            @Override
            public boolean contains(Object o) {
                if (!(o instanceof Map.Entry))
                    return false;
                Map.Entry entry = (Map.Entry) o;
                Object key = entry.getKey();
                Entry[] tab = table;
                int hash = (key == null ? 0 : System.identityHashCode(key));
                int index = (hash & 0x7FFFFFFF) % tab.length;
                for (Entry e = tab[index]; e != null; e = e.next) if (e.hash == hash && e.equals(entry))
                    return true;
                return false;
            }

            @Override
            public boolean remove(Object o) {
                if (!(o instanceof Map.Entry))
                    return false;
                Map.Entry entry = (Map.Entry) o;
                Object key = entry.getKey();
                Entry[] tab = table;
                int hash = (key == null ? 0 : System.identityHashCode(key));
                int index = (hash & 0x7FFFFFFF) % tab.length;
                for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.next) {
                    if (e.hash == hash && e.equals(entry)) {
                        modCount++;
                        if (prev != null)
                            prev.next = e.next;
                        else
                            tab[index] = e.next;
                        count--;
                        e.value = null;
                        return true;
                    }
                }
                return false;
            }

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

            @Override
            public void clear() {
                IdentityHashMap.this.clear();
            }
        };
    }
    return entrySet;
}
Also used : Iterator(java.util.Iterator) AbstractSet(java.util.AbstractSet) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 13 with AbstractSet

use of java.util.AbstractSet in project Purus-Pasta by puruscor.

the class CacheMap method entrySet.

public Set<Entry<K, V>> entrySet() {
    if (entries == null)
        entries = new AbstractSet<Entry<K, V>>() {

            public int size() {
                clean();
                return (back.size());
            }

            public Iterator<Entry<K, V>> iterator() {
                clean();
                final Iterator<Entry<K, Reference<V>>> iter = back.entrySet().iterator();
                return (new Iterator<Entry<K, V>>() {

                    private K nk;

                    private V nv;

                    public boolean hasNext() {
                        while (true) {
                            if (nv != null)
                                return (true);
                            if (!iter.hasNext())
                                return (false);
                            Entry<K, Reference<V>> e = iter.next();
                            K k = e.getKey();
                            V v = e.getValue().get();
                            if (v != null) {
                                nk = k;
                                nv = v;
                                return (true);
                            }
                        }
                    }

                    public Entry<K, V> next() {
                        if (!hasNext())
                            throw (new NoSuchElementException());
                        Entry<K, V> ret = new IteredEntry(nk, nv);
                        nk = null;
                        nv = null;
                        return (ret);
                    }

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

            public void clear() {
                back.clear();
            }
        };
    return (entries);
}
Also used : AbstractSet(java.util.AbstractSet) NoSuchElementException(java.util.NoSuchElementException)

Example 14 with AbstractSet

use of java.util.AbstractSet in project atlas by alibaba.

the class PatchMethodTool method modifyMethod.

public static void modifyMethod(String srcDexFile, String outDexFile, boolean isAndFix) throws IOException {
    DexFile dexFile = DexFileFactory.loadDexFile(srcDexFile, Opcodes.getDefault());
    final Set<ClassDef> classes = Sets.newConcurrentHashSet();
    for (ClassDef classDef : dexFile.getClasses()) {
        Set<Method> methods = Sets.newConcurrentHashSet();
        boolean modifiedMethod = false;
        for (Method method : classDef.getMethods()) {
            MethodImplementation implementation = method.getImplementation();
            if (implementation != null && (methodNeedsModification(classDef, method, isAndFix))) {
                modifiedMethod = true;
                methods.add(new ImmutableMethod(method.getDefiningClass(), method.getName(), method.getParameters(), method.getReturnType(), method.getAccessFlags(), method.getAnnotations(), isAndFix ? modifyMethodAndFix(implementation, method) : modifyMethodTpatch(implementation, method)));
            } else {
                methods.add(method);
            }
        }
        if (!modifiedMethod) {
            classes.add(classDef);
        } else {
            classes.add(new ImmutableClassDef(classDef.getType(), classDef.getAccessFlags(), classDef.getSuperclass(), classDef.getInterfaces(), classDef.getSourceFile(), classDef.getAnnotations(), classDef.getFields(), methods));
        }
    }
    DexFileFactory.writeDexFile(outDexFile, new DexFile() {

        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<ClassDef>() {

                @Nonnull
                @Override
                public Iterator<ClassDef> iterator() {
                    return classes.iterator();
                }

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

        @Nonnull
        @Override
        public Opcodes getOpcodes() {
            return Opcodes.getDefault();
        }
    });
}
Also used : MutableMethodImplementation(org.jf.dexlib2.builder.MutableMethodImplementation) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) AbstractSet(java.util.AbstractSet) Set(java.util.Set) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Nonnull(javax.annotation.Nonnull) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) Method(org.jf.dexlib2.iface.Method) DexFile(org.jf.dexlib2.iface.DexFile) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) Opcodes(org.jf.dexlib2.Opcodes) Iterator(java.util.Iterator)

Aggregations

AbstractSet (java.util.AbstractSet)14 Iterator (java.util.Iterator)8 NoSuchElementException (java.util.NoSuchElementException)4 Set (java.util.Set)3 AbstractMap (java.util.AbstractMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Nonnull (javax.annotation.Nonnull)2 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)2 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)2 ClassDef (org.jf.dexlib2.iface.ClassDef)2 DexFile (org.jf.dexlib2.iface.DexFile)2 Method (org.jf.dexlib2.iface.Method)2 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)2 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)2 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)2 Beta (com.google.common.annotations.Beta)1 Entry (hudson.scm.ChangeLogSet.Entry)1 AdaptedIterator (hudson.util.AdaptedIterator)1 File (java.io.File)1