Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit c8db90d5302c5c5e970f9ebf98f7024ba9482730 1 parent 1d4b44b
bryanduxbury authored
40 src/java/com/rapleaf/jack/AbstractMockDatabaseModel.java
View
@@ -1,10 +1,8 @@
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;
@@ -12,11 +10,13 @@
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;
}
10 src/java/com/rapleaf/jack/test_project/database_1/mock_impl/BaseMockCommentPersistenceImpl.java
View
@@ -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);
}
10 src/java/com/rapleaf/jack/test_project/database_1/mock_impl/BaseMockImagePersistenceImpl.java
View
@@ -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);
}
10 src/java/com/rapleaf/jack/test_project/database_1/mock_impl/BaseMockPostPersistenceImpl.java
View
@@ -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);
}
10 src/java/com/rapleaf/jack/test_project/database_1/mock_impl/BaseMockUserPersistenceImpl.java
View
@@ -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);
}
2  src/java/com/rapleaf/jack/test_project/database_1/models/Comment.java
View
@@ -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;
2  src/java/com/rapleaf/jack/test_project/database_1/models/Image.java
View
@@ -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;
2  src/java/com/rapleaf/jack/test_project/database_1/models/Post.java
View
@@ -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;
2  src/java/com/rapleaf/jack/test_project/database_1/models/User.java
View
@@ -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;
4 src/java/com/rapleaf/jack/util/MysqlFlex.java
View
@@ -1,4 +1,4 @@
-/* The following code was generated by JFlex 1.4.3 on 2/7/12 5:16 PM */
+/* The following code was generated by JFlex 1.4.3 on 2/8/12 3:18 PM */
package com.rapleaf.jack.util;
import java.io.IOException;
@@ -7,7 +7,7 @@
/**
* This class is a scanner generated by
* <a href="http://www.jflex.de/">JFlex</a> 1.4.3
- * on 2/7/12 5:16 PM from the specification file
+ * on 2/8/12 3:18 PM from the specification file
* <tt>src/jflex/mysql.flex</tt>
*/
class MysqlFlex {
10 src/rb/templates/mock_persistence_impl.erb
View
@@ -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);
}
2  src/rb/templates/model.erb
View
@@ -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 %>;
Please sign in to comment.
Something went wrong with that request. Please try again.