From 46acea3383fee271375016923385b5a9590c18c0 Mon Sep 17 00:00:00 2001 From: Asis <47801367+Asis2019@users.noreply.github.com> Date: Sat, 19 Sep 2020 09:53:44 -0400 Subject: [PATCH] Fixed cloning bug --- src/com/asis/joi/model/JOI.java | 24 ++++++------------- src/com/asis/joi/model/entities/Scene.java | 11 ++++++--- .../joi/model/entities/SceneComponent.java | 2 +- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/com/asis/joi/model/JOI.java b/src/com/asis/joi/model/JOI.java index fe4cece..752e168 100644 --- a/src/com/asis/joi/model/JOI.java +++ b/src/com/asis/joi/model/JOI.java @@ -13,7 +13,7 @@ public class JOI implements JSONString, Cloneable { private int sceneIdCounter = 0; - private final ArrayList joiComponents = new ArrayList<>(); + private ArrayList joiComponents = new ArrayList<>(); public void addNewComponent(Class joiComponentClass, Integer... optional) { //Check if an id was passed @@ -33,11 +33,6 @@ public void addNewComponent(Class joiComponentClass, setSceneIdCounter(getSceneIdCounter()+1); } - /*@Deprecated - public boolean removeScene(int sceneId) { - return getSceneArrayList().remove(getScene(sceneId)); - }*/ - public boolean removeComponent(int sceneId) { return getJoiComponents().remove(getComponent(sceneId)); } @@ -96,7 +91,6 @@ public double getDuration() { public JSONObject toJSON() { JSONArray joiArray = new JSONArray(); - //for(Scene scene: getSceneArrayList()) joiArray.put(scene.toJSON()); for(JOIComponent component: getJoiComponents()) { if(component.toJSON() != null) @@ -116,16 +110,9 @@ public String toJSONString() { public JOI clone() throws CloneNotSupportedException { JOI joi = (JOI) super.clone(); - /*ArrayList clonedArray = new ArrayList<>(); - for (Scene scene: getSceneArrayList()) clonedArray.add(scene.clone()); - joi.setSceneArrayList(clonedArray); - - ArrayList clonedArray2 = new ArrayList<>(); - for (VariableSetter setter: getVariableSetterArrayList()) clonedArray2.add((VariableSetter) setter.clone()); - joi.setVariableSetterArrayList(clonedArray2);*/ - - for (JOIComponent component: getJoiComponents()) - joi.getJoiComponents().add((JOIComponent) component.clone()); + ArrayList clonedArray = new ArrayList<>(); + for (JOIComponent component: getJoiComponents()) clonedArray.add((JOIComponent) component.clone()); + joi.setJoiComponents(clonedArray); joi.setSceneIdCounter(getSceneIdCounter()); @@ -152,4 +139,7 @@ private void setSceneIdCounter(int sceneIdCounter) { public ArrayList getJoiComponents() { return joiComponents; } + private void setJoiComponents(ArrayList joiComponents) { + this.joiComponents = joiComponents; + } } diff --git a/src/com/asis/joi/model/entities/Scene.java b/src/com/asis/joi/model/entities/Scene.java index 53f67ec..32326c7 100644 --- a/src/com/asis/joi/model/entities/Scene.java +++ b/src/com/asis/joi/model/entities/Scene.java @@ -14,7 +14,7 @@ public class Scene extends JOIComponent { private final ReadOnlyBooleanWrapper goodEnd = new ReadOnlyBooleanWrapper(); private String ambience; - private final ArrayList> sceneComponents = new ArrayList<>(); + private ArrayList> sceneComponents = new ArrayList<>(); public Scene() { this(0, 0, 10, "Scene 1"); @@ -195,8 +195,9 @@ public Scene clone() throws CloneNotSupportedException { scene.setGoodEnd(isGoodEnd()); scene.setAmbience(getAmbience()); - for (SceneComponent sceneComponent : getSceneComponents()) - scene.addComponent((SceneComponent) sceneComponent.clone()); + ArrayList> clonedArray = new ArrayList<>(); + for (SceneComponent sceneComponent : getSceneComponents()) clonedArray.add((SceneComponent) sceneComponent.clone()); + scene.setSceneComponents(clonedArray); return scene; } @@ -252,6 +253,10 @@ public ArrayList> getSceneComponents() { return sceneComponents; } + private void setSceneComponents(ArrayList> sceneComponents) { + this.sceneComponents = sceneComponents; + } + public String getAmbience() { return ambience; } diff --git a/src/com/asis/joi/model/entities/SceneComponent.java b/src/com/asis/joi/model/entities/SceneComponent.java index 66136ba..c794177 100644 --- a/src/com/asis/joi/model/entities/SceneComponent.java +++ b/src/com/asis/joi/model/entities/SceneComponent.java @@ -1,6 +1,6 @@ package com.asis.joi.model.entities; -public interface SceneComponent { +public interface SceneComponent extends Cloneable { String jsonKeyName(); T toJSON(); double getDuration();