Permalink
Browse files

extend the deep copy behavior to all the other find variants; make it…

… so that models don't need to implement find() themselves
  • Loading branch information...
1 parent 1d4b44b commit c8db90d5302c5c5e970f9ebf98f7024ba9482730 bryanduxbury committed Feb 8, 2012
@@ -1,22 +1,22 @@
package com.rapleaf.jack;
import java.io.IOException;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
+import com.rapleaf.jack.test_project.IDatabases;
import com.rapleaf.jack.util.MysqlToJavaScriptTranslator;
-public abstract class AbstractMockDatabaseModel<T extends ModelWithId>
+public abstract class AbstractMockDatabaseModel<T extends ModelWithId<T>>
implements IModelPersistence<T> {
+ private final IDatabases databases;
protected final Map<Long, T> records = new HashMap<Long, T>();
private static class JavaScriptRecordSelector<T extends ModelWithId>
@@ -72,6 +72,10 @@ public boolean selectRecord(T record) {
}
+ public AbstractMockDatabaseModel(IDatabases databases) {
+ this.databases = databases;
+ }
+
protected Set<T> realFind(Map fieldsMap) throws IOException {
return realFind(null, fieldsMap);
}
@@ -111,7 +115,11 @@ public boolean save(T model) throws IOException {
@Override
public T find(long id) throws IOException {
- return records.get(id);
+ final T tmp = records.get(id);
+ if (tmp == null) {
+ return null;
+ }
+ return (T) tmp.getCopy(databases);
}
@Override
@@ -139,11 +147,11 @@ public void clearCacheById(long id) throws IOException {
Object foreignKeyValue = record.getField(foreignKey);
if (foreignKeyValue instanceof Long) {
if (foreignKeyValue.equals(id)) {
- ret.add(record);
+ ret.add(record.getCopy(databases));
}
} else if (foreignKeyValue instanceof Integer) {
if (((Integer) foreignKeyValue).longValue() == id) {
- ret.add(record);
+ ret.add(record.getCopy(databases));
}
} else {
throw new IllegalArgumentException("Foreign key is not a long or int: "
@@ -161,11 +169,11 @@ public void clearCacheById(long id) throws IOException {
Object foreignKeyValue = record.getField(foreignKey);
if (foreignKeyValue instanceof Long) {
if (ids.contains(foreignKeyValue)) {
- foundSet.add(record);
+ foundSet.add(record.getCopy(databases));
}
} else if (foreignKeyValue instanceof Integer) {
if (ids.contains(((Integer) foreignKeyValue).longValue())) {
- foundSet.add(record);
+ foundSet.add(record.getCopy(databases));
}
} else {
throw new IllegalArgumentException("Foreign key is not a long or int: "
@@ -204,7 +212,11 @@ public boolean deleteAll() throws IOException {
@Override
public Set<T> findAll() throws IOException {
- return Collections.unmodifiableSet(new HashSet<T>(records.values()));
+ Set<T> ts = new HashSet<T>();
+ for (T t : records.values()) {
+ ts.add(t.getCopy(databases));
+ }
+ return ts;
}
@Override
@@ -217,7 +229,7 @@ public boolean deleteAll() throws IOException {
Set<T> results = new HashSet<T>();
for (T record : records.values()) {
if (selector.selectRecord(record)) {
- results.add(record);
+ results.add(record.getCopy(databases));
}
}
return results;
@@ -227,17 +239,17 @@ public boolean deleteAll() throws IOException {
throws IOException {
return new JavaScriptRecordSelector(conditions);
}
-
+
private boolean useCache = true;
-
+
public boolean isCaching() {
return useCache;
}
-
+
public void enableCaching() {
useCache = true;
}
-
+
public void disableCaching() {
useCache = false;
}
@@ -33,7 +33,7 @@
private static AtomicInteger curId = new AtomicInteger(1);
public BaseMockCommentPersistenceImpl(IDatabases databases) {
- super();
+ super(databases);
this.databases = databases;
}
@@ -67,14 +67,6 @@ public Comment create(final int commenter_id, final long commented_on_id, final
}
- public Comment find(long id) throws IOException {
- Comment temp = records.get(id);
- if (temp == null) {
- return null;
- }
- return temp.getCopy(databases);
- }
-
public Set<Comment> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -33,7 +33,7 @@
private static AtomicInteger curId = new AtomicInteger(1);
public BaseMockImagePersistenceImpl(IDatabases databases) {
- super();
+ super(databases);
this.databases = databases;
}
@@ -53,14 +53,6 @@ public Image create(final Integer user_id) throws IOException {
}
- public Image find(long id) throws IOException {
- Image temp = records.get(id);
- if (temp == null) {
- return null;
- }
- return temp.getCopy(databases);
- }
-
public Set<Image> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -33,7 +33,7 @@
private static AtomicInteger curId = new AtomicInteger(1);
public BaseMockPostPersistenceImpl(IDatabases databases) {
- super();
+ super(databases);
this.databases = databases;
}
@@ -55,14 +55,6 @@ public Post create(final String title, final Long posted_at_millis, final Intege
}
- public Post find(long id) throws IOException {
- Post temp = records.get(id);
- if (temp == null) {
- return null;
- }
- return temp.getCopy(databases);
- }
-
public Set<Post> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -33,7 +33,7 @@
private static AtomicInteger curId = new AtomicInteger(1);
public BaseMockUserPersistenceImpl(IDatabases databases) {
- super();
+ super(databases);
this.databases = databases;
}
@@ -71,14 +71,6 @@ public User create(final String handle, final int num_posts) throws IOException
}
- public User find(long id) throws IOException {
- User temp = records.get(id);
- if (temp == null) {
- return null;
- }
- return temp.getCopy(databases);
- }
-
public Set<User> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -21,7 +21,7 @@
import com.rapleaf.jack.test_project.IDatabases;
-public class Comment extends ModelWithId {
+public class Comment extends ModelWithId<Comment> {
// Fields
private String __content;
private int __commenter_id;
@@ -21,7 +21,7 @@
import com.rapleaf.jack.test_project.IDatabases;
-public class Image extends ModelWithId {
+public class Image extends ModelWithId<Image> {
// Fields
private Integer __user_id;
@@ -21,7 +21,7 @@
import com.rapleaf.jack.test_project.IDatabases;
-public class Post extends ModelWithId {
+public class Post extends ModelWithId<Post> {
// Fields
private String __title;
private Long __posted_at_millis;
@@ -21,7 +21,7 @@
import com.rapleaf.jack.test_project.IDatabases;
-public class User extends ModelWithId {
+public class User extends ModelWithId<User> {
// Fields
private String __handle;
private Long __created_at_millis;
Oops, something went wrong.
@@ -43,7 +43,7 @@ public class <%= model_defn.mock_impl_name %> extends AbstractMockDatabaseModel<
private static AtomicInteger curId = new AtomicInteger(1);
public <%= model_defn.mock_impl_name %>(IDatabases databases) {
- super();
+ super(databases);
this.databases = databases;
}
@@ -65,14 +65,6 @@ public class <%= model_defn.mock_impl_name %> extends AbstractMockDatabaseModel<
<%= render_mock_create_method(model_defn, create_signature_small, true) %>
<% end %>
- public <%= model_defn.model_name %> find(long id) throws IOException {
- <%= model_defn.model_name %> temp = records.get(id);
- if (temp == null) {
- return null;
- }
- return temp.getCopy(databases);
- }
-
public Set<<%= model_defn.model_name %>> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
@@ -37,7 +37,7 @@ import <%= project_defn.databases_namespace %>.IDatabases;
<% end %>
<% end %>
-public class <%=model_defn.model_name%> extends ModelWithId {
+public class <%=model_defn.model_name%> extends ModelWithId<<%= model_defn.model_name %>> {
// Fields
<% model_defn.fields.each do |field_defn| %>
private <%= field_defn.java_type %> __<%= field_defn.name %>;

0 comments on commit c8db90d

Please sign in to comment.