added Enum support for BasicUpdate; tweaking the Criteria support

This commit is contained in:
Thomas Risberg
2011-02-14 18:53:10 -05:00
parent 202837e349
commit 750fb907e3
3 changed files with 23 additions and 13 deletions

View File

@@ -37,7 +37,7 @@ public class BasicUpdate extends Update {
@Override @Override
public Update set(String key, Object value) { public Update set(String key, Object value) {
updateObject.put("$set", Collections.singletonMap(key, value)); updateObject.put("$set", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
@@ -48,28 +48,32 @@ public class BasicUpdate extends Update {
} }
@Override @Override
public Update inc(String key, long inc) { public Update inc(String key, Number inc) {
updateObject.put("$inc", Collections.singletonMap(key, inc)); updateObject.put("$inc", Collections.singletonMap(key, inc));
return this; return this;
} }
@Override @Override
public Update push(String key, Object value) { public Update push(String key, Object value) {
updateObject.put("$push", Collections.singletonMap(key, value)); updateObject.put("$push", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
@Override @Override
public Update pushAll(String key, Object[] values) { public Update pushAll(String key, Object[] values) {
Object[] convertedValues = new Object[values.length];
for (int i = 0; i < values.length; i++) {
convertedValues[i] = convertValueIfNecessary(values[i]);
}
DBObject keyValue = new BasicDBObject(); DBObject keyValue = new BasicDBObject();
keyValue.put(key, values); keyValue.put(key, convertedValues);
updateObject.put("$pushAll", keyValue); updateObject.put("$pushAll", keyValue);
return this; return this;
} }
@Override @Override
public Update addToSet(String key, Object value) { public Update addToSet(String key, Object value) {
updateObject.put("$addToSet", Collections.singletonMap(key, value)); updateObject.put("$addToSet", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
@@ -81,14 +85,18 @@ public class BasicUpdate extends Update {
@Override @Override
public Update pull(String key, Object value) { public Update pull(String key, Object value) {
updateObject.put("$pull", Collections.singletonMap(key, value)); updateObject.put("$pull", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
@Override @Override
public Update pullAll(String key, Object[] values) { public Update pullAll(String key, Object[] values) {
Object[] convertedValues = new Object[values.length];
for (int i = 0; i < values.length; i++) {
convertedValues[i] = convertValueIfNecessary(values[i]);
}
DBObject keyValue = new BasicDBObject(); DBObject keyValue = new BasicDBObject();
keyValue.put(key, values); keyValue.put(key, convertedValues);
updateObject.put("$pullAll", keyValue); updateObject.put("$pullAll", keyValue);
return this; return this;
} }

View File

@@ -93,16 +93,18 @@ public class Criteria implements CriteriaDefinition {
return this; return this;
} }
public Criteria size(Object o) { public Criteria size(int s) {
criteria.put("$is", o); criteria.put("$size", s);
return this; return this;
} }
public Criteria exists(boolean b) { public Criteria exists(boolean b) {
criteria.put("$exists", b);
return this; return this;
} }
public Criteria type(int t) { public Criteria type(int t) {
criteria.put("$type", t);
return this; return this;
} }
@@ -111,7 +113,8 @@ public class Criteria implements CriteriaDefinition {
return this; return this;
} }
public Criteria regExp(String re) { public Criteria regex(String re) {
criteria.put("$regex", re);
return this; return this;
} }

View File

@@ -40,7 +40,7 @@ public class Update {
return this; return this;
} }
public Update inc(String key, long inc) { public Update inc(String key, Number inc) {
criteria.put("$inc", Collections.singletonMap(key, inc)); criteria.put("$inc", Collections.singletonMap(key, inc));
return this; return this;
} }
@@ -100,8 +100,7 @@ public class Update {
return dbo; return dbo;
} }
private Object convertValueIfNecessary(Object value) { protected Object convertValueIfNecessary(Object value) {
System.out.println("-> " + value);
if (value instanceof Enum) { if (value instanceof Enum) {
return ((Enum<?>)value).name(); return ((Enum<?>)value).name();
} }