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();
}
};
}
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;
}
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;
}
}
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
}
}
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
}
}
Aggregations