/** * Get the attribute's type. * @return The attribute's type. */ @Nonnull public TypeToken<T> getType() { return name.getType(); }
@Override protected boolean doEquivalent(TypedName<?> a, TypedName<?> b) { return a == b || (a.name.equals(b.getName()) && a.type.equals(b.getType())); }
@Override public <T> EntityBuilder setAttribute(TypedName<T> name, T val) { Preconditions.checkNotNull(name, "attribute"); Preconditions.checkNotNull(val, "value"); Preconditions.checkArgument(name.getRawType().isInstance(val), "value %s not of type %s", val, name.getType()); if (name == CommonAttributes.ENTITY_ID) { return setId(((Long) val).longValue()); } else { attributes.put(name.getName(), Attribute.create(name, val)); return this; } }
@Override public ObjectNode toJSON() { JsonNodeFactory nf = JsonNodeFactory.instance; ObjectNode json = nf.objectNode(); json.put("format", "json"); json.put("entity_type", entityType.getName()); if (!attributes.isEmpty()) { ObjectNode attrNode = json.putObject("attributes"); for (Map.Entry<String,TypedName<?>> attr: attributes.entrySet()) { ObjectNode an = attrNode.putObject(attr.getKey()); an.put("name", attr.getValue().getName()); an.put("type", TypeUtils.makeTypeName(attr.getValue().getType())); } } return json; }
/** * Look up an attribute. * @param name The attribute. * @param matchSubclasses If true, then attributes whose types are subclasses of `name`'s type will also match. * @return The attribute's index, or a negative value if it does not exist. Nonexistence is further * differentiated with -1 for no attribute, -2 for type mismatch. */ public int lookup(TypedName<?> name, boolean matchSubclasses) { // Linear search with interned objects is faster for short lists for (int i = 0; i < names.length; i++) { TypedName<?> n = names[i]; if (n == name) { return i; } else if (n.getName() == name.getName()) { if (matchSubclasses && n.getType().isSubtypeOf(name.getType())) { return i; } else { // FIXME Handle typecasting return -2; } } } return -1; }
ObjectNode colObj = cols.addObject(); colObj.put("name", col.getName()); colObj.put("type", TypeUtils.makeTypeName(col.getType())); ObjectNode colNode = cols.putObject(colE.getKey()); colNode.put("name", colE.getValue().getName()); colNode.put("type", TypeUtils.makeTypeName(colE.getValue().getType()));
TypedName<?> attr = attrs.getAttribute(i); AttrStoreBuilder asb; if (attr.getType().equals(TypeToken.of(Long.class))) { logger.debug("{}: storing long column {}", et, attr.getName()); asb = new LongAttrStoreBuilder(); } else if (attr.getType().equals(TypeToken.of(Integer.class))) { logger.debug("{}: storing int column {}", et, attr.getName()); asb = new AttrStoreBuilder(IntShard::create); } else if (attr.getType().equals(TypeToken.of(Double.class))) { logger.debug("{}: storing double column {}", et, attr.getName()); asb = new DoubleAttrStoreBuilder();
@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: .*\\]$")); }
/** * Get the attribute's type. * @return The attribute's type. */ @Nonnull public TypeToken<T> getType() { return name.getType(); }
@Override public <T> EntityBuilder setAttribute(TypedName<T> name, T val) { Preconditions.checkNotNull(name, "attribute"); Preconditions.checkNotNull(val, "value"); Preconditions.checkArgument(name.getRawType().isInstance(val), "value %s not of type %s", val, name.getType()); if (name == CommonAttributes.ENTITY_ID) { return setId(((Long) val).longValue()); } else { attributes.put(name.getName(), Attribute.create(name, val)); return this; } }
@Override protected boolean doEquivalent(TypedName<?> a, TypedName<?> b) { return a == b || (a.name.equals(b.getName()) && a.type.equals(b.getType())); }
@Override public ObjectNode toJSON() { JsonNodeFactory nf = JsonNodeFactory.instance; ObjectNode json = nf.objectNode(); json.put("format", "json"); json.put("entity_type", entityType.getName()); if (!attributes.isEmpty()) { ObjectNode attrNode = json.putObject("attributes"); for (Map.Entry<String,TypedName<?>> attr: attributes.entrySet()) { ObjectNode an = attrNode.putObject(attr.getKey()); an.put("name", attr.getValue().getName()); an.put("type", TypeUtils.makeTypeName(attr.getValue().getType())); } } return json; }
/** * Look up an attribute. * @param name The attribute. * @param matchSubclasses If true, then attributes whose types are subclasses of `name`'s type will also match. * @return The attribute's index, or a negative value if it does not exist. Nonexistence is further * differentiated with -1 for no attribute, -2 for type mismatch. */ public int lookup(TypedName<?> name, boolean matchSubclasses) { // Linear search with interned objects is faster for short lists for (int i = 0; i < names.length; i++) { TypedName<?> n = names[i]; if (n == name) { return i; } else if (n.getName() == name.getName()) { if (matchSubclasses && n.getType().isSubtypeOf(name.getType())) { return i; } else { // FIXME Handle typecasting return -2; } } } return -1; }
ObjectNode colObj = cols.addObject(); colObj.put("name", col.getName()); colObj.put("type", TypeUtils.makeTypeName(col.getType())); ObjectNode colNode = cols.putObject(colE.getKey()); colNode.put("name", colE.getValue().getName()); colNode.put("type", TypeUtils.makeTypeName(colE.getValue().getType()));
TypedName<?> attr = attrs.getAttribute(i); AttrStoreBuilder asb; if (attr.getType().equals(TypeToken.of(Long.class))) { logger.debug("{}: storing long column {}", et, attr.getName()); asb = new LongAttrStoreBuilder(); } else if (attr.getType().equals(TypeToken.of(Integer.class))) { logger.debug("{}: storing int column {}", et, attr.getName()); asb = new AttrStoreBuilder(IntShard::create); } else if (attr.getType().equals(TypeToken.of(Double.class))) { logger.debug("{}: storing double column {}", et, attr.getName()); asb = new DoubleAttrStoreBuilder();