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); }
/** * Set an extra attribute. An extra attribute is an attribute that is not provided by a bean-style setter. * * The default implementation throws {@link NoSuchAttributeException}. * * @param name The attribute name to clear. * @param val The attribute value to set. */ public <T> void setExtraAttribute(TypedName<T> name, T val) { throw new NoSuchAttributeException(name.toString()); }
/** * {@inheritDoc} * * This implementation delegates to {@link #hasAttribute(String)} */ @Override public boolean hasAttribute(TypedName<?> name) { Object value = maybeGet(name.getName()); return value != null && name.getRawType().isInstance(value); }
@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; } }
@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: .*\\]$")); }
public String getGainAttribute() { if (attribute == null) { return CommonAttributes.RATING.getName(); } else { return attribute; } }
TypedName<?> name = TypedName.create(annot.value(), atype); setters.put(name, m); Method smethod = ce.getValue(); Class smVtype = smethod.getParameterTypes()[0]; Method cmethod = clearers.get(ce.getKey().getName()); ClassNode cn = new ClassNode(); cn.name = String.format("org/lenskit/generated/entities/AttrSet$$%s$$%s", cn.version = V1_8; Class<? extends AttrMethod> sc; if (attr.getRawType().equals(Long.class) && smVtype.equals(long.class)) { sc = LongAttrMethod.class; } else if (attr.getRawType().equals(Double.class) && smVtype.equals(double.class)) { sc = DoubleAttrMethod.class; } else { if (attr.getRawType().equals(Long.class) && smVtype.equals(long.class)) { cn.methods.add(generateLongSetter(type, smethod)); if (attr.getRawType().equals(Double.class) && smVtype.equals(double.class)) { cn.methods.add(generateDoubleSetter(type, smethod));
@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")); } }
/** * Create a new attribute pair. * @param name The attribute name. * @param val The value. */ private Attribute(@Nonnull TypedName<T> name, @Nonnull T val) { Preconditions.checkNotNull(name, "name"); Preconditions.checkNotNull(val, "value"); Preconditions.checkArgument(name.getRawType().isInstance(val), "creating attribute %s: value '%s' is not of correct type", name, val); this.name = name; value = val; }
Map<String,TypedName<?>> attrs = new HashMap<>(); for (Map.Entry<String,String> ab: bean.getAttributes().entrySet()) { attrs.put(ab.getKey(), TypedName.create(ab.getKey(), ab.getValue())); EntityType et = EntityType.forName(d.getKey()); TypedName<?> attr = attrs.get(d.getValue()); Verify.verify(attr.getType().equals(LONG_TYPE), "derived entity source column has non-Long type %s", attr.getType()); derivs.add(EntityDerivation.create(et, type, (TypedName<Long>) attr));
eb.setAttribute(attr, mapper.convertValue(fn, attr.getJacksonType())); } else { if (fn.isIntegralNumber()) { eb.setAttribute(TypedName.create(name, Long.class), fn.asLong()); } else if (fn.isFloatingPointNumber()) { eb.setAttribute(TypedName.create(name, Double.class), fn.asDouble()); } else if (fn.isTextual()) { eb.setAttribute(TypedName.create(name, String.class), fn.asText()); } else { eb.setAttribute(TypedName.create(name, JsonNode.class), fn);
/** * Get the attribute's type. * @return The attribute's type. */ @Nonnull public TypeToken<T> getType() { return name.getType(); }
@Override public Entity parse(String line) { tokenizer.reset(line); lineNo += 1; EntityBuilder builder = newEntityBuilder() .setId(lineNo + baseId); // since ID is already set, a subsequent ID column will properly override for (TypedName column: fileColumns) { String value = tokenizer.nextToken(); if (value != null && column != null) { Object parsed; try { parsed = column.parseString(value); } catch (IllegalArgumentException e) { throw new DataAccessException("line " + lineNo + ": error parsing column " + column, e); } builder.setAttribute(column, parsed); } } return builder.build(); } }
/** * Create a typed name object. * * @param name The name. * @param type The type. * @return An object encapsulating the specified name and type. */ @SuppressWarnings("unchecked") @Nonnull public static <T> TypedName<T> create(String name, Class<T> type) { Preconditions.checkNotNull(name, "name"); Preconditions.checkNotNull(type, "type"); if (type.isPrimitive()) { type = (Class<T>) ClassUtils.primitiveToWrapper(type); } TypedName<T> attribute = new TypedName<>(name.intern(), TypeToken.of(type)); return (TypedName<T>) FIELD_CACHE.intern(NAME_EQUIVALENCE.wrap(attribute)).get(); }
/** * Construct a new nDCG Top-N metric. * @param disc The discount to apply. * @param name The column name to use. */ public TopNNDCGMetric(Discount disc, String name) { this(disc, name, CommonAttributes.RATING.getName()); }
@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; } }
/** * {@inheritDoc} * * This implementation delegates to {@link #maybeGet(String)} and checks the type. */ @Nullable @Override public <T> T maybeGet(TypedName<T> name) { try { // FIXME This is not fully type-safe! return (T) name.getRawType().cast(maybeGet(name.getName())); } catch (ClassCastException e) { throw new IllegalArgumentException(e); } }
@Test public void testParseString() { assertThat(TypedName.create("foo", String.class).parseString("wombat"), equalTo("wombat")); }
@SuppressWarnings("unchecked") @Nullable @Override public <T> T maybeGet(TypedName<T> name) { return name == CommonAttributes.ENTITY_ID ? (T) name.getRawType().cast(getId()) : null; }