private static <T extends PersistentBase> T getPersistent(T persitent, String[] fields) { List<Schema.Field> otherFields = persitent.getSchema().getFields(); String[] otherFieldStrings = new String[otherFields.size()]; for (int i = 0; i < otherFields.size(); i++) { otherFieldStrings[i] = otherFields.get(i).name(); } if (Arrays.equals(fields, otherFieldStrings)) { return persitent; } T clonedPersistent = AvroUtils.deepClonePersistent(persitent); clonedPersistent.clear(); if (fields != null && fields.length > 0) { for (String field : fields) { Schema.Field otherField = persitent.getSchema().getField(field); int index = otherField.pos(); clonedPersistent.put(index, persitent.get(index)); } } else { for (String field : otherFieldStrings) { Schema.Field otherField = persitent.getSchema().getField(field); int index = otherField.pos(); clonedPersistent.put(index, persitent.get(index)); } } return clonedPersistent; }
@Test public void testWithNullAsKeyAndValue() throws Exception { final T cache = getInstance(1000, 1000); assertEquals(0, (long) cache.size()); assertFalse(cache.contains(null)); assertNull(cache.get(null)); // Add entry with null as key and value ... cache.put(null, null); assertEquals(1, (long) cache.size()); assertTrue(cache.contains(null)); assertNull(cache.get(null)); // Add another entry ... cache.put(new Object(), new Object()); assertEquals(2, (long) cache.size()); // Remove entry with null as key ... assertNull(cache.remove(null).getValue()); assertEquals(1, (long) cache.size()); assertFalse(cache.contains(null)); assertNull(cache.get(null)); // Try to remove entry with null as key again ... assertNull(cache.remove(null)); assertEquals(1, (long) cache.size()); assertFalse(cache.contains(null)); assertNull(cache.get(null)); }
protected T doDemarshall(final T impl, final EJValue o, final MarshallingSession ctx) { if (o.isObject() == null) return impl; Object demarshalledKey; final String assumedKeyType = ctx.getAssumedMapKeyType(); final String assumedValueType = ctx.getAssumedMapValueType(); // the assumed map k/v types can only be used once since they are not set for nested maps. ctx.setAssumedMapKeyType(null); ctx.setAssumedMapValueType(null); for (final String key : o.isObject().keySet()) { final EJValue ejValue = o.isObject().get(key); if (key.startsWith(SerializationParts.EMBEDDED_JSON)) { final EJValue ejKey = ParserFactory.get().parse(key.substring(SerializationParts.EMBEDDED_JSON.length())); demarshalledKey = ctx.getMarshallerInstance(ctx.determineTypeFor(null, ejKey)).demarshall(ejKey, ctx); final String valueType = ctx.determineTypeFor(null, ejValue); impl.put(demarshalledKey, ctx.getMarshallerInstance(valueType).demarshall(ejValue, ctx)); } else { if (key.equals(SerializationParts.OBJECT_ID)) { continue; } if (assumedKeyType != null && assumedValueType != null) { if (key.equals(SerializationParts.NULL_VALUE)) { demarshalledKey = null; } else { demarshalledKey = convertKey(assumedKeyType, key, ctx); } final String valueType; if (ejValue.isObject() != null && ejValue.isObject().containsKey(SerializationParts.ENCODED_TYPE)) { valueType = ctx.determineTypeFor(null, ejValue); } else { valueType = assumedValueType; } final Object demarshalledValue = ctx.getMarshallerInstance(valueType).demarshall(ejValue, ctx); impl.put(demarshalledKey, demarshalledValue); } else { demarshalledKey = (key.equals(SerializationParts.NULL_VALUE)) ? null : key; impl.put( demarshalledKey, ctx.getMarshallerInstance(ctx.determineTypeFor(null, ejValue)) .demarshall(ejValue, ctx)); } } } return impl; }
@Test public void testWithMaxSize1() { final T cache = getInstance(1000, 1); final Object o1 = new Object(); cache.put(o1, o1); final Object o2 = new Object(); cache.put(o2, o2); assertEquals(1, (long) cache.size()); assertFalse(cache.contains(o1)); assertTrue(cache.contains(o2)); }
private <T extends ParseObject> T parseData(JSONObject jsonObject) { @SuppressWarnings("unchecked") T po = (T) new ParseObject(); Iterator<?> keys = jsonObject.keys(); while (keys.hasNext()) { String key = (String) keys.next(); Object obj = jsonObject.get(key); if (obj instanceof JSONObject) { JSONObject o = (JSONObject) obj; String type = o.getString("__type"); if ("Date".equals(type)) { Date date = Parse.parseDate(o.getString("iso")); po.put(key, date); } if ("Bytes".equals(type)) { String base64 = o.getString("base64"); po.put(key, base64); } if ("GeoPoint".equals(type)) { ParseGeoPoint gp = new ParseGeoPoint(o.getDouble("latitude"), o.getDouble("longitude")); po.put(key, gp); } if ("File".equals(type)) { ParseFile file = new ParseFile(o.getString("name"), o.getString("url")); po.put(key, file); } if ("Pointer".equals(type)) {} } else { if (Parse.isInvalidKey(key)) { setReservedKey(key, obj); } else { put(key, ParseDecoder.decode(obj)); } } } po.isDirty = false; return po; }
@Test public void testMaxLifeTimeSupport() throws Exception { final T cache = getInstance(100, 100); cache.put("varA", "valueA"); cache.put("varB", "valueB"); Thread.sleep(70); cache.put("varC", "valueC"); assertEquals("valueA", cache.get("varA")); assertEquals("valueB", cache.get("varB")); assertEquals("valueC", cache.get("varC")); Thread.sleep(70); assertNull("valueA", cache.get("varA")); assertNull("valueB", cache.get("varB")); assertEquals("valueC", cache.get("varC")); Thread.sleep(70); assertNull("valueA", cache.get("varA")); assertNull("valueB", cache.get("varB")); assertNull("valueC", cache.get("varC")); }
@SuppressWarnings("unchecked") private static <T extends Map<String, Object>> void obj2map( Object obj, T map, Map<Object, Object> memo) { if (null == obj || memo.containsKey(obj)) return; memo.put(obj, ""); Mirror<?> mirror = Mirror.me(obj.getClass()); Field[] flds = mirror.getFields(); for (Field fld : flds) { Object v = mirror.getValue(obj, fld); if (null == v) { map.put(fld.getName(), null); continue; } Mirror<?> mr = Mirror.me(fld.getType()); if (mr.isNumber() || mr.isBoolean() || mr.isChar() || mr.isStringLike() || mr.isEnum() || mr.isDateTimeLike()) { map.put(fld.getName(), v); } else if (memo.containsKey(v)) { map.put(fld.getName(), null); } else { T sub; try { sub = (T) map.getClass().newInstance(); } catch (Exception e) { throw Lang.wrapThrow(e); } obj2map(v, sub, memo); map.put(fld.getName(), sub); } } }
@Test public void testWithNullAsValue() throws Exception { final T cache = getInstance(1000, 1000); final Object o = new Object(); assertEquals(0, (long) cache.size()); assertFalse(cache.contains(o)); assertNull(cache.get(o)); // Add entry with null as value ... cache.put(o, null); assertEquals(1, (long) cache.size()); assertTrue(cache.contains(o)); assertNull(cache.get(o)); // Remove entry with null as value ... assertNull(cache.remove(o).getValue()); assertEquals(0, (long) cache.size()); assertFalse(cache.contains(o)); assertNull(cache.get(o)); // Try to remove entry with null as value again ... assertNull(cache.remove(o)); assertEquals(0, (long) cache.size()); assertFalse(cache.contains(o)); assertNull(cache.get(o)); }
@SuppressWarnings({"unchecked"}) protected void addToCollection(T collection, Object collectionRow) { // collectionRow will be the actual object if retrieved from audit relation or middle table // otherwise it will be a List Object elementData = collectionRow; Object indexData = collectionRow; if (collectionRow instanceof java.util.List) { elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); } Object element = elementComponentData .getComponentMapper() .mapToObjectFromFullMap( entityInstantiator, (Map<String, Object>) elementData, null, revision); Object index = indexComponentData .getComponentMapper() .mapToObjectFromFullMap( entityInstantiator, (Map<String, Object>) indexData, element, revision); collection.put(index, element); }
public MapBuilder<K, V, T> put(K key, V value) { map.put(key, value); return this; }
protected <T extends Map<String, String>> void fill(T map, int entryCount) { for (int i = 0; i != entryCount; i++) { map.put("key" + i, "value" + i); } }
@Override public boolean put(final Element e) throws CacheException { return e == null || store.put(copyElementForWriteIfNeeded(e)); }