@Override public <T> EntityCollectionBuilder addIndex(TypedName<T> attribute) { int pos = attributes.lookup(attribute); if (pos >= 0) { needIndex[pos] = true; } return this; }
/** * Look up an attribute. * @param name The attribute. * @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) { return lookup(name, false); }
@Override public EntityCollectionBuilder addIndex(String attrName) { int pos = attributes.lookup(attrName); if (pos >= 0) { needIndex[pos] = true; } return this; }
@Override public boolean contains(Object o) { return o instanceof TypedName && lookup((TypedName<?>) o) >= 0; }
@Override public boolean contains(Object o) { return o instanceof String && lookup((String) o) >= 0; }
@Override public boolean hasAttribute(String name) { return attributeNames.lookup(name) >= 0; }
@Nullable @Override public Object maybeGet(String attr) { int idx = layout.attributes.lookup(attr); if (idx >= 0) { BeanAttributeGetter gf = layout.getters.get(idx); return gf.get(this); } else { return null; } }
@Nullable @Override public <T> T maybeGet(TypedName<T> name) { int idx = layout.attributes.lookup(name, true); if (idx >= 0) { BeanAttributeGetter gf = layout.getters.get(idx); return (T) gf.get(this); } else { return null; } }
@Override public boolean hasAttribute(String name) { int ap = attributes.lookup(name); return ap >= 0 && !attrStores[ap].isNull(position); }
@Override public boolean hasAttribute(TypedName<?> name) { int ap = attributes.lookup(name); return ap >= 0 && !attrStores[ap].isNull(position); }
@Nullable @Override public Object maybeGet(String attr) { int ap = attributes.lookup(attr); return ap >= 0 ? attrStores[ap].get(position) : null; }
@Nullable @Override public Object maybeGet(String name) { int idx = attributeNames.lookup(name); if (idx < 0) { return null; } else if (idx == 0) { return getId(); } else { return attributeValues[idx-1]; } } }
@Nullable @Override public <T> T maybeGet(TypedName<T> name) { int ap = attributes.lookup(name); return ap >= 0 ? (T) name.getRawType().cast(attrStores[ap].get(position)) : null; }
@SuppressWarnings("unchecked") @Nullable @Override public <T> T maybeGet(TypedName<T> name) { int idx = attributeNames.lookup(name); if (idx < 0) { return null; } else if (idx == 0) { return (T) name.getRawType().cast(getId()); } else { return (T) name.getRawType().cast(attributeValues[idx-1]); } }
@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 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()); } }
@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()); } }
@Test public void testBasicSingleAttribute() { AttributeSet attrs = AttributeSet.create(CommonAttributes.ENTITY_ID); assertThat(attrs.size(), equalTo(1)); assertThat(attrs.lookup(CommonAttributes.ENTITY_ID), equalTo(0)); assertThat(attrs.lookup(CommonAttributes.ITEM_ID), equalTo(-1)); assertThat(attrs, contains(CommonAttributes.ENTITY_ID)); assertThat(attrs.contains(CommonAttributes.ENTITY_ID), equalTo(true)); assertThat(attrs.contains(CommonAttributes.NAME), equalTo(false)); }