@SuppressWarnings("unchecked")
public StorageKey reuseFor(Object entity) {
final List<FieldPartitioner> partitioners = strategy.getFieldPartitioners();
for (int i = 0; i < partitioners.size(); i++) {
final FieldPartitioner fp = partitioners.get(i);
final Object value;
// TODO: this should probably live elsewhere and be extensible
if (entity instanceof GenericRecord) {
value = ((GenericRecord) entity).get(fp.getSourceName());
} else {
final String name = fp.getSourceName();
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name,
entity.getClass(), getter(name), null /* assume read only */);
value = propertyDescriptor.getReadMethod().invoke(entity);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot read property " + name +
" from " + entity, e);
} catch (InvocationTargetException e) {
throw new IllegalStateException("Cannot read property " + name +
" from " + entity, e);
} catch (IntrospectionException e) {
throw new IllegalStateException("Cannot read property " + name +
" from " + entity, e);
}
}
replace(i, fp.apply(value));
}
return this;
}