Search in sources :

Example 1 with AbstractList

use of java.util.AbstractList in project gerrit by GerritCodeReview.

the class Natives method asList.

public static List<String> asList(final JsArrayString arr) {
    if (arr == null) {
        return null;
    }
    return new AbstractList<String>() {

        @Override
        public String set(int index, String element) {
            String old = arr.get(index);
            arr.set(index, element);
            return old;
        }

        @Override
        public String get(int index) {
            return arr.get(index);
        }

        @Override
        public int size() {
            return arr.length();
        }
    };
}
Also used : AbstractList(java.util.AbstractList) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 2 with AbstractList

use of java.util.AbstractList in project lucene-solr by apache.

the class RamUsageTester method measureObjectSize.

/*
   * Non-recursive version of object descend. This consumes more memory than recursive in-depth
   * traversal but prevents stack overflows on long chains of objects
   * or complex graphs (a max. recursion depth on my machine was ~5000 objects linked in a chain
   * so not too much).
   */
private static long measureObjectSize(Object root, Accumulator accumulator) {
    // Objects seen so far.
    final Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>());
    // Class cache with reference Field and precalculated shallow size. 
    final IdentityHashMap<Class<?>, ClassCache> classCache = new IdentityHashMap<>();
    // Stack of objects pending traversal. Recursion caused stack overflows. 
    final ArrayList<Object> stack = new ArrayList<>();
    stack.add(root);
    long totalSize = 0;
    while (!stack.isEmpty()) {
        final Object ob = stack.remove(stack.size() - 1);
        if (ob == null || seen.contains(ob)) {
            continue;
        }
        seen.add(ob);
        final Class<?> obClazz = ob.getClass();
        assert obClazz != null : "jvm bug detected (Object.getClass() == null). please report this to your vendor";
        if (obClazz.isArray()) {
            /*
         * Consider an array, possibly of primitive types. Push any of its references to
         * the processing stack and accumulate this array's shallow size. 
         */
            final long shallowSize = RamUsageEstimator.shallowSizeOf(ob);
            final int len = Array.getLength(ob);
            final List<Object> values;
            Class<?> componentClazz = obClazz.getComponentType();
            if (componentClazz.isPrimitive()) {
                values = Collections.emptyList();
            } else {
                values = new AbstractList<Object>() {

                    @Override
                    public Object get(int index) {
                        return Array.get(ob, index);
                    }

                    @Override
                    public int size() {
                        return len;
                    }
                };
            }
            totalSize += accumulator.accumulateArray(ob, shallowSize, values, stack);
        } else {
            /*
         * Consider an object. Push any references it has to the processing stack
         * and accumulate this object's shallow size. 
         */
            try {
                ClassCache cachedInfo = classCache.get(obClazz);
                if (cachedInfo == null) {
                    classCache.put(obClazz, cachedInfo = createCacheEntry(obClazz));
                }
                boolean needsReflection = true;
                if (Constants.JRE_IS_MINIMUM_JAVA9 && obClazz.getName().startsWith("java.")) {
                    // Java 9: Best guess for some known types, as we cannot precisely look into runtime classes:
                    final ToLongFunction<Object> func = SIMPLE_TYPES.get(obClazz);
                    if (func != null) {
                        // some simple type like String where the size is easy to get from public properties
                        totalSize += accumulator.accumulateObject(ob, cachedInfo.alignedShallowInstanceSize + func.applyAsLong(ob), Collections.emptyMap(), stack);
                        needsReflection = false;
                    } else if (ob instanceof Iterable) {
                        final List<Object> values = StreamSupport.stream(((Iterable<?>) ob).spliterator(), false).collect(Collectors.toList());
                        totalSize += accumulator.accumulateArray(ob, cachedInfo.alignedShallowInstanceSize + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER, values, stack);
                        needsReflection = false;
                    } else if (ob instanceof Map) {
                        final List<Object> values = ((Map<?, ?>) ob).entrySet().stream().flatMap(e -> Stream.of(e.getKey(), e.getValue())).collect(Collectors.toList());
                        totalSize += accumulator.accumulateArray(ob, cachedInfo.alignedShallowInstanceSize + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER, values, stack);
                        totalSize += RamUsageEstimator.NUM_BYTES_ARRAY_HEADER;
                        needsReflection = false;
                    }
                }
                if (needsReflection) {
                    final Map<Field, Object> fieldValues = new HashMap<>();
                    for (Field f : cachedInfo.referenceFields) {
                        fieldValues.put(f, f.get(ob));
                    }
                    totalSize += accumulator.accumulateObject(ob, cachedInfo.alignedShallowInstanceSize, fieldValues, stack);
                }
            } catch (IllegalAccessException e) {
                // this should never happen as we enabled setAccessible().
                throw new RuntimeException("Reflective field access failed?", e);
            }
        }
    }
    // Help the GC (?).
    seen.clear();
    stack.clear();
    classCache.clear();
    return totalSize;
}
Also used : Array(java.lang.reflect.Array) IdentityHashMap(java.util.IdentityHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collection(java.util.Collection) AbstractList(java.util.AbstractList) Set(java.util.Set) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) AccessController(java.security.AccessController) ToLongFunction(java.util.function.ToLongFunction) Path(java.nio.file.Path) Collections(java.util.Collections) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with AbstractList

use of java.util.AbstractList in project robovm by robovm.

the class ConcurrentModTest method testSet.

/*
     * Test method for 'java.util.AbstractList.subList(int, int)'
     */
public void testSet() {
    AbstractList al = new ArrayList();
    Double one = new Double(1.0);
    Double two = new Double(2.0);
    Double three = new Double(3.0);
    Double four = new Double(4.0);
    al.add(one);
    al.add(two);
    al.add(three);
    al.add(four);
    List sub = al.subList(1, 3);
    assertEquals(2, sub.size());
    // the sub.get(1) is 3.0
    assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0);
    assertTrue(((Double) sub.get(1)).doubleValue() > 2.0);
    // remove the 2.0
    al.remove(1);
    try {
        // illegal call the subList's method set(int,Object).
        sub.set(1, two);
        fail("It should throws ConcurrentModificationException.");
    } catch (ConcurrentModificationException e) {
        return;
    }
}
Also used : AbstractList(java.util.AbstractList) ConcurrentModificationException(java.util.ConcurrentModificationException) ArrayList(java.util.ArrayList) List(java.util.List) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList)

Example 4 with AbstractList

use of java.util.AbstractList in project robovm by robovm.

the class ConcurrentModTest method testGet.

/*
     * Test method for 'java.util.AbstractList.subList(int, int)'
     */
public void testGet() {
    AbstractList al = new ArrayList();
    Double one = new Double(1.0);
    Double two = new Double(2.0);
    Double three = new Double(3.0);
    Double four = new Double(4.0);
    al.add(one);
    al.add(two);
    al.add(three);
    al.add(four);
    List sub = al.subList(1, 3);
    assertEquals(2, sub.size());
    // the sub.get(1) is 3.0
    assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0);
    assertTrue(((Double) sub.get(1)).doubleValue() > 2.0);
    // remove the 2.0
    al.remove(1);
    try {
        // illegal call the subList's method get(int).
        sub.get(1);
        fail("It should throws ConcurrentModificationException.");
    } catch (ConcurrentModificationException e) {
        return;
    }
    try {
        al.get(-1);
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException ee) {
    //expected
    }
    try {
        al.get(al.size() + 1);
        fail("IndexOutOfBoundsException expected");
    } catch (IndexOutOfBoundsException ee) {
    //expected
    }
}
Also used : AbstractList(java.util.AbstractList) ConcurrentModificationException(java.util.ConcurrentModificationException) ArrayList(java.util.ArrayList) List(java.util.List) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList)

Example 5 with AbstractList

use of java.util.AbstractList in project robovm by robovm.

the class ConcurrentModTest method test_addLjava_lang_Object.

public void test_addLjava_lang_Object() {
    AbstractList abstr = new AbstractList() {

        @Override
        public Object get(int arg0) {
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    try {
        abstr.add(null);
        fail("UnsupportedOperationException expected");
    } catch (UnsupportedOperationException e) {
    //ecpected
    }
    abstr = new AbstractList<Double>() {

        @Override
        public boolean add(Double value) {
            return true;
        }

        @Override
        public Double get(int index) {
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    try {
        abstr.add(1);
        fail("ClassCastException expected");
    } catch (ClassCastException ee) {
    //expected
    }
    abstr = new AbstractList<Integer>() {

        final int forbiddenValue = 33;

        @Override
        public boolean add(Integer value) {
            if (value == forbiddenValue) {
                throw new IllegalArgumentException();
            }
            return true;
        }

        @Override
        public Integer get(int index) {
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    abstr.add(1);
    try {
        abstr.add(33);
        fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException ee) {
    //expected
    }
}
Also used : AbstractList(java.util.AbstractList)

Aggregations

AbstractList (java.util.AbstractList)76 ArrayList (java.util.ArrayList)30 List (java.util.List)17 HashMap (java.util.HashMap)10 NodeRef (org.alfresco.service.cmr.repository.NodeRef)10 QName (org.alfresco.service.namespace.QName)9 UserInfo (org.alfresco.rest.api.model.UserInfo)8 FileInfo (org.alfresco.service.cmr.model.FileInfo)8 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)7 RexNode (org.apache.calcite.rex.RexNode)7 Collection (java.util.Collection)6 ConcurrentModificationException (java.util.ConcurrentModificationException)6 Iterator (java.util.Iterator)6 Paging (org.alfresco.rest.framework.resource.parameters.Paging)6 IOException (java.io.IOException)5 Set (java.util.Set)5 FilterProp (org.alfresco.repo.node.getchildren.FilterProp)5 Test (org.junit.Test)5 ListIterator (java.util.ListIterator)4 RandomAccess (java.util.RandomAccess)4