Skip to content

Commit

Permalink
Fixed cloning bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Asis2019 committed Sep 19, 2020
1 parent 9dc2d38 commit 46acea3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
24 changes: 7 additions & 17 deletions src/com/asis/joi/model/JOI.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class JOI implements JSONString, Cloneable {
private int sceneIdCounter = 0;
private final ArrayList<JOIComponent> joiComponents = new ArrayList<>();
private ArrayList<JOIComponent> joiComponents = new ArrayList<>();

public <T extends JOIComponent> void addNewComponent(Class<T> joiComponentClass, Integer... optional) {
//Check if an id was passed
Expand All @@ -33,11 +33,6 @@ public <T extends JOIComponent> void addNewComponent(Class<T> 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));
}
Expand Down Expand Up @@ -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)
Expand All @@ -116,16 +110,9 @@ public String toJSONString() {
public JOI clone() throws CloneNotSupportedException {
JOI joi = (JOI) super.clone();

/*ArrayList<Scene> clonedArray = new ArrayList<>();
for (Scene scene: getSceneArrayList()) clonedArray.add(scene.clone());
joi.setSceneArrayList(clonedArray);
ArrayList<VariableSetter> 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<JOIComponent> clonedArray = new ArrayList<>();
for (JOIComponent component: getJoiComponents()) clonedArray.add((JOIComponent) component.clone());
joi.setJoiComponents(clonedArray);

joi.setSceneIdCounter(getSceneIdCounter());

Expand All @@ -152,4 +139,7 @@ private void setSceneIdCounter(int sceneIdCounter) {
public ArrayList<JOIComponent> getJoiComponents() {
return joiComponents;
}
private void setJoiComponents(ArrayList<JOIComponent> joiComponents) {
this.joiComponents = joiComponents;
}
}
11 changes: 8 additions & 3 deletions src/com/asis/joi/model/entities/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Scene extends JOIComponent {
private final ReadOnlyBooleanWrapper goodEnd = new ReadOnlyBooleanWrapper();
private String ambience;

private final ArrayList<SceneComponent<?>> sceneComponents = new ArrayList<>();
private ArrayList<SceneComponent<?>> sceneComponents = new ArrayList<>();

public Scene() {
this(0, 0, 10, "Scene 1");
Expand Down Expand Up @@ -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<SceneComponent<?>> clonedArray = new ArrayList<>();
for (SceneComponent<?> sceneComponent : getSceneComponents()) clonedArray.add((SceneComponent<?>) sceneComponent.clone());
scene.setSceneComponents(clonedArray);

return scene;
}
Expand Down Expand Up @@ -252,6 +253,10 @@ public ArrayList<SceneComponent<?>> getSceneComponents() {
return sceneComponents;
}

private void setSceneComponents(ArrayList<SceneComponent<?>> sceneComponents) {
this.sceneComponents = sceneComponents;
}

public String getAmbience() {
return ambience;
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/asis/joi/model/entities/SceneComponent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.asis.joi.model.entities;

public interface SceneComponent<T> {
public interface SceneComponent<T> extends Cloneable {
String jsonKeyName();
T toJSON();
double getDuration();
Expand Down

0 comments on commit 46acea3

Please sign in to comment.