Skip to content

Commit

Permalink
Replaced custom id getter/setter in InMem with Cls property utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jan 16, 2015
1 parent c56d282 commit 11bfc48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 95 deletions.
Expand Up @@ -41,6 +41,7 @@
import org.rapidoid.lambda.Callback;
import org.rapidoid.lambda.Operation;
import org.rapidoid.lambda.Predicate;
import org.rapidoid.util.Cls;
import org.rapidoid.util.U;

public class DbImpl extends NamedActivity<Database> implements Database {
Expand Down Expand Up @@ -220,7 +221,7 @@ public String toString() {

@Override
public long getIdOf(Object record) {
return InMem.getIdOf(record, false);
return U.or(Cls.getIdIfExists(record), -1L);
}

@Override
Expand Down
117 changes: 23 additions & 94 deletions rapidoid-inmem/src/main/java/org/rapidoid/inmem/InMem.java
Expand Up @@ -229,7 +229,7 @@ public long insert(Object record) {
sharedLock();
try {
long id = ids.incrementAndGet();
setId(record, id);
Cls.setId(record, id);

if (insideTx.get()) {
if (txInsertions.putIfAbsent(id, INSERTION) != null) {
Expand Down Expand Up @@ -428,13 +428,19 @@ public <E> E getIfExists(long id) {
}
}

@SuppressWarnings("unchecked")
private <E> E get_(long id, boolean validateId) {
if (validateId) {
validateId(id);
}
Rec rec = data.get(id);
return (E) (rec != null ? setId(obj(rec), id) : null);

if (rec != null) {
E record = obj(rec);
Cls.setId(record, id);
return record;
} else {
return null;
}
}

public <E> E get(long id, Class<E> clazz) {
Expand All @@ -451,7 +457,7 @@ public void refresh(Object record) {
sharedLock();
try {

long id = getIdOf(record, true);
long id = Cls.getId(record);
validateId(id);
Rec rec = getRec(id);
obj(rec, record);
Expand Down Expand Up @@ -480,7 +486,7 @@ public void update(long id, Object record) {
private void update_(long id, Object record, boolean reflectRelChanges) {
validateId(id);

setId(record, id);
Cls.setId(record, id);

Rec removed = data.replace(id, rec(record));

Expand All @@ -500,11 +506,11 @@ private void update_(long id, Object record, boolean reflectRelChanges) {
}

public void update(Object record) {
update(getIdOf(record, true), record);
update(Cls.getId(record), record);
}

public long persist(Object record) {
long id = getIdOf(record, true);
long id = Cls.getId(record);
if (id <= 0) {
return insert(record);
} else {
Expand All @@ -514,7 +520,7 @@ public long persist(Object record) {
}

public long persistedIdOf(Object record) {
long id = getIdOf(record, true);
long id = Cls.getId(record);
if (id <= 0) {
return insert(record);
} else {
Expand Down Expand Up @@ -659,7 +665,7 @@ public boolean matches(Object record, String query, Object... args) {
}

if (P_WORD.matcher(query).matches() && args.length == 1) {
Object val = getProperty(record, query, false);
Object val = Cls.getPropValue(record, query, null);
Object arg = args[0];
return val == arg || (val != null && val.equals(arg));
}
Expand All @@ -673,7 +679,7 @@ public <E> void each(final Operation<E> lambda) {

for (Entry<Long, Rec> entry : data.entrySet()) {
E record = obj(entry.getValue());
setId(record, entry.getKey());
Cls.setId(record, entry.getKey());

try {
lambda.execute(record);
Expand Down Expand Up @@ -785,7 +791,13 @@ private void txRollback() {
private <T> T get_(long id, Class<T> clazz) {
validateId(id);
Rec rec = data.get(id);
return rec != null ? setId(obj(rec, clazz), id) : null;
if (rec != null) {
T record = obj(rec, clazz);
Cls.setId(record, id);
return record;
} else {
return null;
}
}

private void sharedLock() {
Expand Down Expand Up @@ -1099,89 +1111,6 @@ private <T> T obj(Rec rec, T destination) {
return destination;
}

@SuppressWarnings("unchecked")
private static <T> T setId(T record, long id) {
if (record != null) {

if (record instanceof Map) {
((Map<Object, Object>) record).put("id", id);
}

try {
setObjId(record, id);
} catch (Exception e) {
// ignore
}
}

return record;
}

public static void setObjId(Object obj, long id) {
Class<?> c = obj.getClass();

try {
try {
c.getMethod("setId", long.class).invoke(obj, id);
return;
} catch (NoSuchMethodException e1) {
try {
c.getMethod("id", long.class).invoke(obj, id);
return;
} catch (NoSuchMethodException e2) {
try {
c.getField("id").set(obj, id);
return;
} catch (NoSuchFieldException e3) {
}
}
}
} catch (Exception e) {
throw new RuntimeException("Cannot get object id!", e);
}

throw new RuntimeException(
"Cannot find public 'id' field nor 'setId' setter method nor 'id' setter method in class: " + c);
}

public static long getIdOf(Object obj, boolean failIfNotFound) {
Number id = (Number) getProperty(obj, "id", failIfNotFound);
return id != null ? id.longValue() : -1;
}

@SuppressWarnings("unchecked")
public static <T> T getProperty(Object obj, String property, boolean failIfNotFound) {
Class<?> c = obj.getClass();

String capitalized = property.substring(0, 1).toUpperCase() + property.substring(1);
String getter = "get" + capitalized;

try {
try {
return (T) c.getMethod(getter).invoke(obj);
} catch (NoSuchMethodException e1) {
try {
return (T) c.getMethod(property).invoke(obj);
} catch (NoSuchMethodException e2) {
try {
return (T) c.getField(property).get(obj);
} catch (NoSuchFieldException e3) {
}
}
}
} catch (Exception e) {
throw new RuntimeException("Cannot get property: " + property, e);
}

if (failIfNotFound) {
throw new RuntimeException(String.format(
"Cannot find public '%s' field nor '%s' getter method nor '%s' getter method in class: %s",
property, getter, property, c));
} else {
return null;
}
}

public int size() {
globalLock();
try {
Expand Down

0 comments on commit 11bfc48

Please sign in to comment.