diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java new file mode 100644 index 000000000..f96d7691d --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb; + +import java.util.Collections; +import java.util.LinkedHashMap; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + +public class UpdateBuilder { + + private LinkedHashMap criteria = new LinkedHashMap(); + + public UpdateBuilder set(String key, Object value) { + criteria.put("$set", Collections.singletonMap(key, value)); + return this; + } + + public UpdateBuilder inc(String key, long inc) { + criteria.put("$inc", Collections.singletonMap(key, inc)); + return this; + } + + public DBObject build() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + dbo.put(k, criteria.get(k)); + } + return dbo; + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java new file mode 100644 index 000000000..630227380 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb; + +import org.junit.Assert; +import org.junit.Test; + +public class UpdateBuilderTests { + + @Test + public void testSetUpdate() { + UpdateBuilder ub = new UpdateBuilder() + .set("directory", "/Users/Test/Desktop"); + Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", ub.build().toString()); + } + + @Test + public void testIncUpdate() { + UpdateBuilder ub = new UpdateBuilder() + .inc("size", 1); + Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", ub.build().toString()); + } + + @Test + public void testIncAndSetUpdate() { + UpdateBuilder ub = new UpdateBuilder() + .inc("size", 1) + .set("directory", "/Users/Test/Desktop"); + Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", + ub.build().toString()); + } + + +}