private Object readResolve() throws ObjectStreamException { // Look up the name to ensure singletons // This is not entirely safe; see Effective Java, 2nd Ed., #77 for details. // However, we do not depend on singletons for security, only for correctness. // It is acceptable if malicious serialization streams result in broken objects. return create(name, type); }
/** * Construct a new index builder. * @param name The attribute name to index. */ GenericEntityIndexBuilder(String name) { this(TypedName.create(name, Object.class)); }
/** * Construct a new index builder. * @param name The attribute name to index. */ LongEntityIndexBuilder(String name) { this(TypedName.create(name, Long.class)); }
@Nonnull @Override public List<Entity> find(String name, Object value) { if (value instanceof Long) { return find(TypedName.create(name, Long.class), (Long) value); } else { return Collections.emptyList(); } }
@Override public GroupedQuery<E> groupBy(String name) { return groupBy(TypedName.create(name, Long.class)); }
/** * Create an typed name from a name and type name. * @param name The name. * @param typeName The type name. * @return The attribute. * @throws IllegalArgumentException if `typeName` is not a valid type name. */ public static TypedName<?> create(String name, String typeName) { return create(name, TypeUtils.resolveTypeName(typeName)); }
@Override public EntityCollectionBuilder addIndex(String attrName) { return addIndex(TypedName.create(attrName, Object.class)); }
static TypedName<?> parseAttribute(EntityDefaults entityDefaults, JsonNode col) { if (col.isNull() || col.isMissingNode()) { return null; } else if (col.isObject()) { String name = col.path("name").asText(null); String type = col.path("type").asText(null); Preconditions.checkArgument(name != null, "no attribute name specified"); Preconditions.checkArgument(type != null, "no attribute type specified"); return TypedName.create(name, type); } else if (col.isTextual()) { String name = col.asText(); TypedName<?> attr = entityDefaults != null ? entityDefaults.getAttributeDefaults(name) : null; if (attr == null) { attr = TypedName.create(col.asText(), col.asText().equals("id") ? (Class) Long.class : String.class); } return attr; } else { throw new IllegalArgumentException("invalid attribute specification: " + col.toString()); } } }
/** * Parse an entity derivation from a JSON node. * @param node The JSON node. * @return The entity derivation. */ public static EntityDerivation fromJSON(JsonNode node) { JsonNode src = node.get("source_type"); Preconditions.checkArgument(src != null, "missing source_type"); JsonNode tgt = node.get("entity_type"); Preconditions.checkArgument(tgt != null, "missing entity_type"); JsonNode attr = node.get("source_attribute"); Preconditions.checkArgument(attr != null, "missing source_attribute"); return create(EntityType.forName(tgt.asText()), EntityType.forName(src.asText()), TypedName.create(attr.asText(), Long.class)); } }
protected static BeanEntityLayout makeLayout(Class<? extends AbstractBeanEntity> type) { BeanEntityLayout res = cache.get(type); if (res != null) { return res; } DynamicClassLoader dlc = new DynamicClassLoader(type.getClassLoader()); Map<String, BeanAttributeGetter> attrs = new HashMap<>(); List<TypedName<?>> names = new ArrayList<>(); for (Method m: type.getMethods()) { EntityAttribute annot = m.getAnnotation(EntityAttribute.class); if (annot != null) { BeanAttributeGetter gfunc = generateGetter(dlc, type, m); attrs.put(annot.value(), gfunc); names.add(TypedName.create(annot.value(), TypeToken.of(m.getGenericReturnType()))); } } AttributeSet aset = AttributeSet.create(names); ImmutableList.Builder<BeanAttributeGetter> mhlb = ImmutableList.builder(); for (String name: aset.nameSet()) { mhlb.add(attrs.get(name)); } res = new BeanEntityLayout(aset, mhlb.build()); cache.put(type, res); return res; }
@Test public void testTypeNames() { assertThat(TypedName.create("foo", "string"), equalTo(TypedName.create("foo", (Class) String.class))); assertThat(TypedName.create("bar", "long"), equalTo(TypedName.create("bar", (Class) Long.class))); assertThat(TypedName.create("bar", "int"), equalTo(TypedName.create("bar", (Class) Integer.class))); assertThat(TypedName.create("bar", "double"), equalTo(TypedName.create("bar", (Class) Double.class))); assertThat(TypedName.create("bar", "real"), equalTo(TypedName.create("bar", (Class) Double.class))); assertThat(TypedName.create("bar", "Double"), equalTo(TypedName.create("bar", (Class) Double.class))); assertThat(TypedName.create("bar", "Integer"), equalTo(TypedName.create("bar", (Class) Integer.class))); assertThat(TypedName.create("bar", "String"), equalTo(TypedName.create("bar", (Class) String.class))); assertThat(TypedName.create("file", "java.io.File"), equalTo(TypedName.create("file", (Class) File.class))); }
@Test public void testParseStringList() { TypeToken<List<String>> ltt = new TypeToken<List<String>>() {}; TypedName<List<String>> name = TypedName.create("tags", ltt); assertThat(name.parseString("foo,bar"), contains("foo", "bar")); } }
@Test public void testParseString() { assertThat(TypedName.create("foo", String.class).parseString("wombat"), equalTo("wombat")); }
@Test public void testParseLong() { assertThat(TypedName.create("foo", Long.class).parseString("3209"), equalTo(3209L)); }
@Test public void testMultiple() { AttributeSet attrs = AttributeSet.create(TypedName.create("abbr", String.class), CommonAttributes.ENTITY_ID, CommonAttributes.USER_ID); assertThat(attrs.nameSet(), contains("id", "abbr", "user")); } }
@Test public void testBasicField() { TypedName<String> attribute = TypedName.create("foo", String.class); assertThat(attribute.getName(), equalTo("foo")); assertThat(attribute.getType(), equalTo(TypeToken.of(String.class))); // check equality to random other object assertThat(attribute, not(equalTo((Object) "foo"))); assertThat(TypedName.create("foo", String.class), sameInstance(attribute)); assertThat(attribute.toString(), notNullValue()); assertThat(attribute.toString(), matchesPattern("^TypedName\\[foo: .*\\]$")); }
@Test public void testSerialize() { TypedName<String> attribute = TypedName.create("foo", String.class); assertThat(SerializationUtils.clone(attribute), sameInstance(attribute)); }
@Test public void testRatingEntityBadAttr() { EntityFactory fac = new EntityFactory(); Rating r = fac.rating(42, 37, 3.5, 10); try { r.get(TypedName.create("rating", long.class)); fail("get with bad attribute should throw"); } catch (Throwable th) { assertThat(th, instanceOf(IllegalArgumentException.class)); } try { r.getLong(TypedName.create("rating", long.class)); fail("get with bad attribute type should throw"); } catch (Throwable th) { assertThat(th, instanceOf(IllegalArgumentException.class)); } try { r.get("foobat"); fail("get with missing attribute should throw"); } catch (Throwable th) { assertThat(th, instanceOf(NoSuchAttributeException.class)); } } }
@Test public void testConfigureReaderCompoundField() throws IOException { TypeToken<List<String>> sl = TypeUtils.makeListType(TypeToken.of(String.class)); TypedName<List<String>> tlName = TypedName.create("tags", sl); ObjectReader reader = new ObjectMapper().reader(); JsonNode json = reader.readTree("{\"entity_type\": \"item\", \"attributes\": {\"id\": \"long\", \"title\": {\"name\": \"name\", \"type\": \"string\"}, \"tags\": \"string[]\"}}"); JSONEntityFormat fmt = JSONEntityFormat.fromJSON(null, ClassLoaders.inferDefault(), json); assertThat(fmt.getEntityType(), equalTo(CommonTypes.ITEM)); assertThat(fmt.getDefinedAttributes(), hasEntry("id", (TypedName) CommonAttributes.ENTITY_ID)); assertThat(fmt.getDefinedAttributes(), hasEntry("title", (TypedName) CommonAttributes.NAME)); assertThat(fmt.getDefinedAttributes(), hasEntry("tags", (TypedName) tlName)); assertThat(fmt.getDefinedAttributes().size(), equalTo(3)); assertThat(fmt.getAttributes(), containsInAnyOrder(CommonAttributes.ENTITY_ID, CommonAttributes.NAME, tlName)); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"title\": \"hamster\", \"tags\": [\"foo\", \"bar\"]}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.get(CommonAttributes.NAME), equalTo("hamster")); assertThat(res.get(tlName), contains("foo", "bar")); } }