/** * Create a new entity builder that is initialized with a copy of an entity. * @param e The entity. * @return An entity builder initialized to build a copy of {@code e}. */ public static EntityBuilder copyBuilder(Entity e) { EntityBuilder eb = newBuilder(e.getType(), e.getId()); for (TypedName a: e.getTypedAttributeNames()) { eb.setAttribute(a, e.get(a)); } return eb; }
public Entity likeBatch(long uid, long iid, int count) { return Entities.newBuilder(EntityType.forName("like-batch")) .setId(++entityId) .setLongAttribute(CommonAttributes.USER_ID, uid) .setLongAttribute(CommonAttributes.ITEM_ID, iid) .setAttribute(CommonAttributes.COUNT, count) .build(); } }
@Override public EntityBuilder setLongAttribute(TypedName<Long> name, long val) { AttrMethod e = findEntry(name); if (e instanceof LongAttrMethod) { ((LongAttrMethod) e).set(this, val); } else if (e != null) { e.set(this, val); } else { setExtraAttribute(name, val); } return this; }
@Override public String toString() { ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); tsb.append("type", getType()); for (Attribute<?> av: getAttributes()) { tsb.append(av.getTypedName().toString(), av.getValue()); } return tsb.toString(); }
@Override public double getDouble(TypedName<Double> name) { int idx = layout.attributes.lookup(name); if (idx >= 0) { BeanAttributeGetter gf = layout.getters.get(idx); return gf.getDouble(this); } else { throw new NoSuchAttributeException(name.toString()); } }
@Override public int getInteger(TypedName<Integer> name) { int idx = layout.attributes.lookup(name); if (idx >= 0) { BeanAttributeGetter gf = layout.getters.get(idx); return gf.getInt(this); } else { throw new NoSuchAttributeException(name.toString()); } }
@Override public boolean getBoolean(TypedName<Boolean> name) { int idx = layout.attributes.lookup(name); if (idx >= 0) { BeanAttributeGetter gf = layout.getters.get(idx); return gf.getBoolean(this); } else { throw new NoSuchAttributeException(name.toString()); } }
@Override public long getLong(TypedName<Long> name) { int idx = layout.attributes.lookup(name); if (idx >= 0) { BeanAttributeGetter gf = layout.getters.get(idx); return gf.getLong(this); } else { throw new NoSuchAttributeException(name.toString()); } }
/** * Set an attribute in the entity. * @param attr The attribute to set. * @param <T> The attribute type. * @return The entity builder (for chaining). * @throws NoSuchAttributeException if the specified attribute is not supported by this entity. */ public <T> EntityBuilder setAttribute(Attribute<T> attr) { setAttribute(attr.getTypedName(), attr.getValue()); return this; }
@Override public <T> EntityBuilder setAttribute(TypedName<T> name, T val) { AttrMethod e = findEntry(name); if (e == null) { setExtraAttribute(name, val); } else { e.set(this, val); } return this; }
@Override public EntityBuilder clearAttribute(TypedName<?> name) { AttrMethod e = findEntry(name); if (e == null) { clearExtraAttribute(name); } else { e.clear(this); } return this; }
/** * Create a new basic entity builder. * @param id The entity ID. * @return The entity builder. */ public static EntityBuilder newBuilder(EntityType type, long id) { return newBuilder(type).setId(id); }
/** * 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()); }
private Object readResolve() { // 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 forName(name); }
/** * Set the rating ID. * @param id The rating ID. * @return The builder (for chaining). */ @EntityAttributeSetter("id") public RatingBuilder setId(long id) { return (RatingBuilder) super.setId(id); }
/** * {@inheritDoc} * * This implementation delegates to {@link #get(TypedName)}. */ @Override public long getLong(TypedName<Long> name) { return get(name); }
@Override public Object get(Object key) { if (key instanceof String) { return maybeGet((String) key); } else { return null; } }
@Override @EntityAttribute("timestamp") public long getTimestamp() { return timestamp; } }
/** * Create a new basic entity builder. * @param type The entity type. * @return The entity builder. */ public static EntityBuilder newBuilder(EntityType type) { return new BasicEntityBuilder(type); }
@Override public void describeTo(DescriptionWriter writer) { writer.putField("type", getType()); for (Attribute<?> av: getAttributes()) { writer.putField(av.getTypedName().toString(), av.getValue()); } }