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
public Update set(String key, Object value) {
updateObject.put("$set", Collections.singletonMap(key, value));
updateObject.put("$set", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this;
}
@@ -48,28 +48,32 @@ public class BasicUpdate extends Update {
}
@Override
public Update inc(String key, long inc) {
public Update inc(String key, Number inc) {
updateObject.put("$inc", Collections.singletonMap(key, inc));
return this;
}
@Override
public Update push(String key, Object value) {
updateObject.put("$push", Collections.singletonMap(key, value));
updateObject.put("$push", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this;
}
@Override
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();
keyValue.put(key, values);
keyValue.put(key, convertedValues);
updateObject.put("$pushAll", keyValue);
return this;
}
@Override
public Update addToSet(String key, Object value) {
updateObject.put("$addToSet", Collections.singletonMap(key, value));
updateObject.put("$addToSet", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this;
}
@@ -81,14 +85,18 @@ public class BasicUpdate extends Update {
@Override
public Update pull(String key, Object value) {
updateObject.put("$pull", Collections.singletonMap(key, value));
updateObject.put("$pull", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this;
}
@Override
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();
keyValue.put(key, values);
keyValue.put(key, convertedValues);
updateObject.put("$pullAll", keyValue);
return this;
}

View File

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

View File

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