/** * Releases all the resources associated with this map. A * map can be used again once it's released, although * if it's used again it should be released again. */ @Override @Deprecated public void release() { close(); }
/** * Creates a List from a V8Array using a deep copy and a TypeAdapter to handle * type conversions. All elements in the V8Array are released after they are accessed. * However, the root array itself is not released. * * @param array The root of the V8Array graph. * @param adapter The {@link TypeAdapter} to use for the object conversions. * * @return A list representing a deep copy of the V8Array rooted at 'array'. */ public static List<? super Object> toList(final V8Array array, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { return toList(array, cache, adapter); } finally { cache.close(); } }
/** * Creates a Map from a V8Object using a deep copy and a TypeAdapter to handle * type conversions. All elements in the V8Object are released after they are accessed. * However, the root object itself is not released. * * @param object The root of the V8Object graph. * @param adapter The {@link TypeAdapter} to use for the object conversions. * * @return A map representing a deep copy of the V8Object rooted at 'object'. */ public static Map<String, ? super Object> toMap(final V8Object object, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { return toMap(object, cache, adapter); } finally { cache.close(); } }
/** * Create a Java Object from a result from V8 using a {@link TypeAdapter} to convert * objects. V8 can return basic Java types or V8Values (V8Object, V8Array, etc...). This * method will attempt to convert the result into a pure Java object using * a deep copy. * * If the input is basic Java type (Integer, Double, Boolean, String) * it will be returned. If the input is a V8Value, it will be converted. * * All elements in the V8Object are released after they are accessed. * However, the root object itself is not released. * * @param v8Object The input to convert. * @param adapter The {@link TypeAdapter} to use for the object conversions. * @return A Java object representing the input. */ public static Object getValue(final Object v8Object, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { if (v8Object instanceof V8Value) { int type = ((V8Value) v8Object).getV8Type(); return getValue(v8Object, type, cache, adapter); } else { return v8Object; } } finally { cache.close(); } }
((Releasable) object).release(); cache.close();
((Releasable) object).release(); cache.close();
((Releasable) object).release(); cache.close();
/** * Releases all the resources associated with this map. A * map can be used again once it's released, although * if it's used again it should be released again. */ @Override @Deprecated public void release() { close(); }
@Test public void testReleaseEmptyMap() { V8Map<String> map = new V8Map<String>(); map.close(); }
@Test public void testEntrySet() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); Set<Entry<V8Value, String>> entrySet = map.entrySet(); assertEquals(1, entrySet.size()); assertEquals(v1, entrySet.iterator().next().getKey()); assertEquals("foo", entrySet.iterator().next().getValue()); v1.close(); map.close(); }
@Test public void testValues() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); Collection<String> values = map.values(); assertEquals(1, values.size()); assertEquals("foo", values.iterator().next()); v1.close(); map.close(); }
@Test public void testKeysetNotReleased() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); v1.close(); Set<V8Value> keySet = map.keySet(); assertEquals(1, keySet.size()); assertFalse(keySet.iterator().next().isReleased()); map.close(); }
@Test public void testDoesNotContainValue() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); assertFalse(map.containsValue("foo")); map.close(); }
@Test public void testReleaseMapReleasesKeys() { V8Object v8Object = new V8Object(v8); V8Map<String> map = new V8Map<String>(); map.put(v8Object, "foo"); v8Object.close(); map.close(); }
@Test public void testIsNotEmpty() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); v1.close(); assertFalse(map.isEmpty()); map.close(); }
@Test public void testDoesNotContainKey() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); assertFalse(map.containsKey(v8Object)); v8Object.close(); map.close(); }
@Test public void testRemoveKeyReleasesKey() { V8Map<String> map = new V8Map<String>(); v8.executeScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); map.remove(v8Object); v8Object.close(); map.close(); }
@Test public void testGetMissing() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); assertNull(map.get(v8Object)); v8Object.close(); map.close(); }
@Test public void testContainsKey() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); assertTrue(map.containsKey(v8Object)); v8Object.close(); map.close(); }
@Test public void testAddDuplicateKey() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); map.put(v1, "bar"); assertEquals("bar", map.get(v1)); v1.close(); map.close(); }