created Sort as separate construct
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2011 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.builder;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
|
||||
public class BasicSort implements Sort {
|
||||
|
||||
private DBObject sortObject;
|
||||
|
||||
public BasicSort(String sortString) {
|
||||
this.sortObject = (DBObject) JSON.parse(sortString);
|
||||
}
|
||||
|
||||
public BasicSort(DBObject sortObject) {
|
||||
this.sortObject = sortObject;
|
||||
}
|
||||
|
||||
public DBObject getSortObject() {
|
||||
return this.sortObject;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,8 +22,6 @@ public class BasicUpdate implements Update {
|
||||
|
||||
private DBObject updateObject = null;
|
||||
|
||||
private DBObject sortObject;
|
||||
|
||||
public BasicUpdate(String updateString) {
|
||||
super();
|
||||
this.updateObject = (DBObject) JSON.parse(updateString);
|
||||
@@ -34,24 +32,8 @@ public class BasicUpdate implements Update {
|
||||
this.updateObject = updateObject;
|
||||
}
|
||||
|
||||
public BasicUpdate(String updateString, String sortString) {
|
||||
super();
|
||||
this.updateObject = (DBObject) JSON.parse(updateString);
|
||||
this.sortObject = (DBObject) JSON.parse(sortString);
|
||||
}
|
||||
|
||||
public BasicUpdate(DBObject updateObject, DBObject sortObject) {
|
||||
super();
|
||||
this.updateObject = updateObject;
|
||||
this.sortObject = sortObject;
|
||||
}
|
||||
|
||||
public DBObject getUpdateObject() {
|
||||
return updateObject;
|
||||
}
|
||||
|
||||
public DBObject getSortObject() {
|
||||
return this.sortObject;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2011 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.builder;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
|
||||
public interface Sort {
|
||||
|
||||
DBObject getSortObject();
|
||||
}
|
||||
@@ -22,7 +22,7 @@ import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
|
||||
public class SortSpec {
|
||||
public class SortSpec implements Sort {
|
||||
|
||||
public enum SortOrder {
|
||||
ASCENDING, DESCENDING
|
||||
@@ -35,6 +35,10 @@ public class SortSpec {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Sort build() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBObject getSortObject() {
|
||||
DBObject dbo = new BasicDBObject();
|
||||
for (String k : criteria.keySet()) {
|
||||
|
||||
@@ -21,6 +21,4 @@ import com.mongodb.DBObject;
|
||||
public interface Update {
|
||||
|
||||
DBObject getUpdateObject();
|
||||
|
||||
DBObject getSortObject();
|
||||
}
|
||||
|
||||
@@ -30,8 +30,6 @@ public class UpdateSpec implements Update {
|
||||
|
||||
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
|
||||
|
||||
private SortSpec sortSpec;
|
||||
|
||||
public UpdateSpec set(String key, Object value) {
|
||||
criteria.put("$set", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
@@ -86,15 +84,6 @@ public class UpdateSpec implements Update {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SortSpec sort() {
|
||||
synchronized (this) {
|
||||
if (this.sortSpec == null) {
|
||||
this.sortSpec = new SortSpec();
|
||||
}
|
||||
}
|
||||
return this.sortSpec;
|
||||
}
|
||||
|
||||
public Update build() {
|
||||
return this;
|
||||
}
|
||||
@@ -107,11 +96,4 @@ public class UpdateSpec implements Update {
|
||||
return dbo;
|
||||
}
|
||||
|
||||
public DBObject getSortObject() {
|
||||
if (this.sortSpec == null) {
|
||||
return null;
|
||||
}
|
||||
return this.sortSpec.getSortObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2010-2011 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.builder;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.document.mongodb.builder.SortSpec.SortOrder;
|
||||
|
||||
public class SortTests {
|
||||
|
||||
@Test
|
||||
public void testWithSortAscending() {
|
||||
SortSpec s = new SortSpec().on("name", SortOrder.ASCENDING);
|
||||
Assert.assertEquals("{ \"name\" : 1}", s.build().getSortObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSortDescending() {
|
||||
SortSpec s = new SortSpec().on("name", SortOrder.DESCENDING);
|
||||
Assert.assertEquals("{ \"name\" : -1}", s.build().getSortObject().toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -121,21 +121,4 @@ public class UpdateTests {
|
||||
Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", u.build().getUpdateObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSortAscending() {
|
||||
UpdateSpec u = new UpdateSpec();
|
||||
u.set("name",52);
|
||||
u.sort().on("name", SortOrder.ASCENDING);
|
||||
Assert.assertEquals("{ \"$set\" : { \"name\" : 52}}", u.build().getUpdateObject().toString());
|
||||
Assert.assertEquals("{ \"name\" : 1}", u.build().getSortObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSortDescending() {
|
||||
UpdateSpec u = new UpdateSpec();
|
||||
u.set("name",52);
|
||||
u.sort().on("name", SortOrder.DESCENDING);
|
||||
Assert.assertEquals("{ \"$set\" : { \"name\" : 52}}", u.build().getUpdateObject().toString());
|
||||
Assert.assertEquals("{ \"name\" : -1}", u.build().getSortObject().toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user