* @return a {@link RiakIndexes} made of the values of the RiakIndex
* annotated fields and methods. For methods it is expected to be
* a Set<Long> or Set<String>
*/
@SuppressWarnings("unchecked") public <T> RiakIndexes getIndexes(T obj) {
final RiakIndexes riakIndexes = new RiakIndexes();
for (RiakIndexField f : indexFields) {
if (Set.class.isAssignableFrom(f.getType())) {
final Type t = f.getField().getGenericType();
if (t instanceof ParameterizedType) {
Class genericType = (Class)((ParameterizedType)t).getActualTypeArguments()[0];
if (String.class.equals(genericType)) {
riakIndexes.addBinSet(f.getIndexName(), (Set<String>)getFieldValue(f.getField(), obj));
} else if (Long.class.equals(genericType) ||Integer.class.equals(genericType)) {
riakIndexes.addIntSet(f.getIndexName(), (Set<Long>)getFieldValue(f.getField(), obj));
} else if (Integer.class.equals(genericType)) {
// Supporting Integer as legacy. All new code should use Long
Set<Integer> iSet = (Set<Integer>) getFieldValue(f.getField(), obj);
Set<Long> lSet = new HashSet<Long>();
for (Integer i : iSet) {
lSet.add(i.longValue());
}
riakIndexes.addIntSet(f.getIndexName(), lSet);
}
}
} else {
final Object val = getFieldValue(f.getField(), obj);
// null is not an index value
if (val != null) {
if (val instanceof String) {
riakIndexes.add(f.getIndexName(), (String) val);
} else if (val instanceof Long) {
riakIndexes.add(f.getIndexName(), (Long) val);
} else if (val instanceof Integer) {
// Supporting int / Integer for legacy. New code should use long / Long
riakIndexes.add(f.getIndexName(), ((Integer) val).longValue());
}
}
}
}
for (RiakIndexMethod m : indexMethods) {
if (Set.class.isAssignableFrom(m.getType())) {
final Type t = m.getMethod().getGenericReturnType();
if (t instanceof ParameterizedType) {
final Object val = getMethodValue(m.getMethod(), obj);
if (val != null) {
final Class<?> genericType = (Class<?>) ((ParameterizedType) t).getActualTypeArguments()[0];
if (String.class.equals(genericType)) {
riakIndexes.addBinSet(m.getIndexName(), (Set<String>) val);
} else if (Long.class.equals(genericType)) {
riakIndexes.addIntSet(m.getIndexName(), (Set<Long>) val);
} else if (Integer.class.equals(genericType)) {
// Supporting Integer as legacy. All new code should use Long
Set<Integer> iSet = (Set<Integer>) val;
Set<Long> lSet = new HashSet<Long>();
for (Integer i : iSet) {
lSet.add(i.longValue());
}
riakIndexes.addIntSet(m.getIndexName(), lSet);
}
}
}
} else {
final Object val = getMethodValue(m.getMethod(), obj);
// null is not an index value
if (val != null) {
if (val instanceof String) {
riakIndexes.add(m.getIndexName(), (String) val);
} else if (val instanceof Long) {
riakIndexes.add(m.getIndexName(), (Long) val);
} else if (val instanceof Integer) {
riakIndexes.add(m.getIndexName(), ((Integer) val).longValue());
}
}
}
}