created Sort as separate construct

This commit is contained in:
Thomas Risberg
2011-02-03 11:57:45 -05:00
parent 3ecad1cfd2
commit 4ed5ff7dc8
8 changed files with 103 additions and 57 deletions

View File

@@ -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;
}
}

View File

@@ -22,8 +22,6 @@ public class BasicUpdate implements Update {
private DBObject updateObject = null; private DBObject updateObject = null;
private DBObject sortObject;
public BasicUpdate(String updateString) { public BasicUpdate(String updateString) {
super(); super();
this.updateObject = (DBObject) JSON.parse(updateString); this.updateObject = (DBObject) JSON.parse(updateString);
@@ -34,24 +32,8 @@ public class BasicUpdate implements Update {
this.updateObject = updateObject; 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() { public DBObject getUpdateObject() {
return updateObject; return updateObject;
} }
public DBObject getSortObject() {
return this.sortObject;
}
} }

View File

@@ -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();
}

View File

@@ -22,7 +22,7 @@ import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
public class SortSpec { public class SortSpec implements Sort {
public enum SortOrder { public enum SortOrder {
ASCENDING, DESCENDING ASCENDING, DESCENDING
@@ -35,6 +35,10 @@ public class SortSpec {
return this; return this;
} }
public Sort build() {
return this;
}
public DBObject getSortObject() { public DBObject getSortObject() {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) { for (String k : criteria.keySet()) {

View File

@@ -21,6 +21,4 @@ import com.mongodb.DBObject;
public interface Update { public interface Update {
DBObject getUpdateObject(); DBObject getUpdateObject();
DBObject getSortObject();
} }

View File

@@ -30,8 +30,6 @@ public class UpdateSpec implements Update {
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>(); private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
private SortSpec sortSpec;
public UpdateSpec set(String key, Object value) { public UpdateSpec set(String key, Object value) {
criteria.put("$set", Collections.singletonMap(key, value)); criteria.put("$set", Collections.singletonMap(key, value));
return this; return this;
@@ -86,15 +84,6 @@ public class UpdateSpec implements Update {
return this; return this;
} }
public SortSpec sort() {
synchronized (this) {
if (this.sortSpec == null) {
this.sortSpec = new SortSpec();
}
}
return this.sortSpec;
}
public Update build() { public Update build() {
return this; return this;
} }
@@ -107,11 +96,4 @@ public class UpdateSpec implements Update {
return dbo; return dbo;
} }
public DBObject getSortObject() {
if (this.sortSpec == null) {
return null;
}
return this.sortSpec.getSortObject();
}
} }

View File

@@ -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());
}
}

View File

@@ -121,21 +121,4 @@ public class UpdateTests {
Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", u.build().getUpdateObject().toString()); 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());
}
} }