Skip to content

Commit

Permalink
Some cleanup suggested by IntelliJ
Browse files Browse the repository at this point in the history
  • Loading branch information
bladecoder committed Sep 17, 2023
1 parent 9344cdc commit 3baa36b
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 111 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/bladecoder/ink/runtime/CallStack.java
Expand Up @@ -2,6 +2,7 @@

import com.bladecoder.ink.runtime.SimpleJson.InnerWriter;
import com.bladecoder.ink.runtime.SimpleJson.Writer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -178,7 +179,7 @@ public CallStack(CallStack toCopy) {
}

public CallStack(Story storyContext) {
startOfRoot.assign(Pointer.startOf(storyContext.getRootContentContainer()));
startOfRoot.assign(Pointer.startOf(storyContext.getMainContentContainer()));

reset();
}
Expand Down Expand Up @@ -361,7 +362,7 @@ public void setJsonToken(HashMap<String, Object> jRTObject, Story storyContext)
}

threadCounter = (int) jRTObject.get("threadCounter");
startOfRoot.assign(Pointer.startOf(storyContext.getRootContentContainer()));
startOfRoot.assign(Pointer.startOf(storyContext.getMainContentContainer()));
}

public Thread forkThread() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bladecoder/ink/runtime/Choice.java
Expand Up @@ -30,7 +30,7 @@ public class Choice extends RTObject {

String sourcePath;

public Choice() throws Exception {}
public Choice() {}

public int getIndex() {
return index;
Expand Down
90 changes: 19 additions & 71 deletions src/main/java/com/bladecoder/ink/runtime/Container.java
@@ -1,6 +1,7 @@
package com.bladecoder.ink.runtime;

import com.bladecoder.ink.runtime.Path.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -9,16 +10,16 @@
public class Container extends RTObject implements INamedContent {
private String name;

private List<RTObject> content;
private final List<RTObject> content;
private HashMap<String, INamedContent> namedContent;

private boolean visitsShouldBeCounted;
private boolean turnIndexShouldBeCounted;
private boolean countingAtStartOnly;

public Container() {
content = new ArrayList<RTObject>();
setNamedContent(new HashMap<String, INamedContent>());
content = new ArrayList<>();
setNamedContent(new HashMap<>());
}

@Override
Expand All @@ -34,10 +35,6 @@ public List<RTObject> getContent() {
return content;
}

public void setContent(List<RTObject> value) throws Exception {
addContent(value);
}

public HashMap<String, INamedContent> getNamedContent() {
return namedContent;
}
Expand All @@ -61,7 +58,7 @@ public HashMap<String, RTObject> getNamedOnlyContent() {
}
}

if (namedOnlyContentDict.size() == 0) namedOnlyContentDict = null;
if (namedOnlyContentDict.isEmpty()) namedOnlyContentDict = null;

return namedOnlyContentDict;
}
Expand Down Expand Up @@ -134,46 +131,26 @@ public int getCountFlags() {
}

public void setCountFlags(int value) {
int flag = value;

if ((flag & COUNTFLAGS_VISITS) > 0) setVisitsShouldBeCounted(true);
if ((value & COUNTFLAGS_VISITS) > 0) setVisitsShouldBeCounted(true);

if ((flag & COUNTFLAGS_TURNS) > 0) setTurnIndexShouldBeCounted(true);
if ((value & COUNTFLAGS_TURNS) > 0) setTurnIndexShouldBeCounted(true);

if ((flag & COUNTFLAGS_COUNTSTARTONLY) > 0) setCountingAtStartOnly(true);
if ((value & COUNTFLAGS_COUNTSTARTONLY) > 0) setCountingAtStartOnly(true);
}

@Override
public boolean hasValidName() {
return getName() != null && getName().length() > 0;
}

public Path getPathToFirstLeafContent() {
if (_pathToFirstLeafContent == null)
_pathToFirstLeafContent = getPath().pathByAppendingPath(getInternalPathToFirstLeafContent());

return _pathToFirstLeafContent;
return getName() != null && !getName().isEmpty();
}

Path _pathToFirstLeafContent;

Path getInternalPathToFirstLeafContent() {
List<Component> components = new ArrayList<Path.Component>();

Container container = this;
while (container != null) {
if (container.getContent().size() > 0) {
components.add(new Path.Component(0));
container = container.getContent().get(0) instanceof Container
? (Container) container.getContent().get(0)
: (Container) null;
}
public void addContents(List<RTObject> contentList) throws Exception {
for (RTObject c : contentList) {
addContent(c);
}

return new Path(components);
}

public void addContent(RTObject contentObj) throws Exception {
private void addContent(RTObject contentObj) throws Exception {
getContent().add(contentObj);

if (contentObj.getParent() != null) {
Expand All @@ -185,25 +162,8 @@ public void addContent(RTObject contentObj) throws Exception {
tryAddNamedContent(contentObj);
}

public void addContent(List<RTObject> contentList) throws Exception {
for (RTObject c : contentList) {
addContent(c);
}
}

public void insertContent(RTObject contentObj, int index) throws Exception {
getContent().add(index, contentObj);
if (contentObj.getParent() != null) {
throw new Exception("content is already in " + contentObj.getParent());
}

contentObj.setParent(this);
tryAddNamedContent(contentObj);
}

public void tryAddNamedContent(RTObject contentObj) throws Exception {
INamedContent namedContentObj =
contentObj instanceof INamedContent ? (INamedContent) contentObj : (INamedContent) null;
private void tryAddNamedContent(RTObject contentObj) {
INamedContent namedContentObj = contentObj instanceof INamedContent ? (INamedContent) contentObj : null;
if (namedContentObj != null && namedContentObj.hasValidName()) {
addToNamedContentOnly(namedContentObj);
}
Expand All @@ -219,17 +179,7 @@ public void addToNamedContentOnly(INamedContent namedContentObj) {
getNamedContent().put(namedContentObj.getName(), namedContentObj);
}

public void addContentsOfContainer(Container otherContainer) throws Exception {
getContent().addAll(otherContainer.getContent());

for (RTObject obj : otherContainer.getContent()) {
obj.setParent(this);

tryAddNamedContent(obj);
}
}

protected RTObject contentWithPathComponent(Path.Component component) throws StoryException, Exception {
private RTObject contentWithPathComponent(Path.Component component) {

if (component.isIndex()) {
if (component.getIndex() >= 0 && component.getIndex() < getContent().size()) {
Expand Down Expand Up @@ -342,17 +292,15 @@ public void buildStringOfHierarchy(StringBuilder sb, int indentation, RTObject p
sb.append("\n");
}

HashMap<String, INamedContent> onlyNamed = new HashMap<String, INamedContent>();
HashMap<String, INamedContent> onlyNamed = new HashMap<>();

for (Entry<String, INamedContent> objKV : getNamedContent().entrySet()) {
if (getContent().contains(objKV.getValue())) {
continue;
} else {
if (!getContent().contains(objKV.getValue())) {
onlyNamed.put(objKV.getKey(), objKV.getValue());
}
}

if (onlyNamed.size() > 0) {
if (!onlyNamed.isEmpty()) {
appendIndentation(sb, indentation);

sb.append("-- named: --\n");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/bladecoder/ink/runtime/Json.java
@@ -1,6 +1,7 @@
package com.bladecoder.ink.runtime;

import com.bladecoder.ink.runtime.ControlCommand.CommandType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -568,7 +569,7 @@ public static void writeRuntimeContainer(SimpleJson.Writer writer, Container con
@SuppressWarnings("unchecked")
static Container jArrayToContainer(List<Object> jArray) throws Exception {
Container container = new Container();
container.setContent(jArrayToRuntimeObjList(jArray, true));
container.addContents(jArrayToRuntimeObjList(jArray, true));
// Final RTObject in the array is always a combination of
// - named content
// - a "#" key with the countFlags
Expand All @@ -584,7 +585,7 @@ static Container jArrayToContainer(List<Object> jArray) throws Exception {
} else {
RTObject namedContentItem = jTokenToRuntimeObject(keyVal.getValue());
Container namedSubContainer =
namedContentItem instanceof Container ? (Container) namedContentItem : (Container) null;
namedContentItem instanceof Container ? (Container) namedContentItem : null;
if (namedSubContainer != null) namedSubContainer.setName(keyVal.getKey());

namedOnlyContent.put(keyVal.getKey(), namedContentItem);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/bladecoder/ink/runtime/Path.java
Expand Up @@ -7,12 +7,12 @@
public class Path {
private static final String PARENT_ID = "^";

private List<Component> components;
private final List<Component> components;
private boolean isRelative = false;
private String componentsString;

public Path() {
components = new ArrayList<Component>();
components = new ArrayList<>();
}

public Path(Component head, Path tail) {
Expand Down Expand Up @@ -51,7 +51,7 @@ private void setRelative(boolean value) {
}

public Component getHead() {
if (components.size() > 0) {
if (!components.isEmpty()) {
return components.get(0);
} else {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bladecoder/ink/runtime/Pointer.java
Expand Up @@ -37,7 +37,7 @@ public RTObject resolve() {

if (container == null) return null;

if (container.getContent().size() == 0) return container;
if (container.getContent().isEmpty()) return container;

if (index >= container.getContent().size()) return null;

Expand Down
32 changes: 13 additions & 19 deletions src/main/java/com/bladecoder/ink/runtime/RTObject.java
@@ -1,6 +1,7 @@
package com.bladecoder.ink.runtime;

import com.bladecoder.ink.runtime.Path.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -12,9 +13,9 @@
public class RTObject {
/**
* Runtime.RTObjects can be included in the main Story as a hierarchy. Usually
* parents are Container RTObjects. (TODO: Always?) The parent.
* parents are Container RTObjects.
*/
private RTObject parent;
private Container parent;

private Path path;

Expand All @@ -25,11 +26,11 @@ public RTObject() {}
// for serialisation purposes at least.
private DebugMetadata debugMetadata;

public RTObject getParent() {
public Container getParent() {
return parent;
}

public void setParent(RTObject value) {
public void setParent(Container value) {
parent = value;
}

Expand Down Expand Up @@ -80,20 +81,15 @@ public Path getPath() {
} else {
List<Path.Component> comps = new ArrayList<Path.Component>();
RTObject child = this;
Container container =
child.getParent() instanceof Container ? (Container) child.getParent() : (Container) null;
Container container = child.getParent();
while (container != null) {
INamedContent namedChild =
child instanceof INamedContent ? (INamedContent) child : (INamedContent) null;
if (namedChild != null && namedChild.hasValidName()) {
comps.add(new Path.Component(namedChild.getName()));
if (child instanceof Container && ((Container) child).hasValidName()) {
comps.add(new Path.Component(((Container) child).getName()));
} else {
comps.add(new Component(container.getContent().indexOf(child)));
}
child = container;
container = container.getParent() instanceof Container
? (Container) container.getParent()
: (Container) null;
container = container.getParent();
}

// Reverse list because components are searched in reverse
Expand All @@ -109,13 +105,12 @@ public Path getPath() {

public SearchResult resolvePath(Path path) throws Exception {
if (path.isRelative()) {
Container nearestContainer = this instanceof Container ? (Container) this : (Container) null;
Container nearestContainer = this instanceof Container ? (Container) this : null;

if (nearestContainer == null) {
// Debug.Assert(this.getparent() != null, "Can't resolve
// relative path because we don't have a parent");
nearestContainer =
this.getParent() instanceof Container ? (Container) this.getParent() : (Container) null;
nearestContainer = this.getParent() != null ? this.getParent() : null;
// Debug.Assert(nearestContainer != null, "Expected parent to be
// a container");
// Debug.Assert(path.getcomponents()[0].isParent);
Expand Down Expand Up @@ -156,8 +151,7 @@ public Path convertPathToRelative(Path globalPath) {
for (int down = lastSharedPathCompIndex + 1; down < globalPath.getLength(); ++down)
newPathComps.add(globalPath.getComponent(down));

Path relativePath = new Path(newPathComps, true);
return relativePath;
return new Path(newPathComps, true);
}

// Find most compact representation for a path, whether relative or global
Expand All @@ -181,7 +175,7 @@ public Container getRootContentContainer() {
while (ancestor.getParent() != null) {
ancestor = ancestor.getParent();
}
return ancestor instanceof Container ? (Container) ancestor : (Container) null;
return ancestor instanceof Container ? (Container) ancestor : null;
}

RTObject copy() throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bladecoder/ink/runtime/SearchResult.java
Expand Up @@ -11,7 +11,7 @@
*
* @author rgarcia
*/
class SearchResult {
public class SearchResult {
public RTObject obj;
public boolean approximate;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bladecoder/ink/runtime/Story.java
Expand Up @@ -3,6 +3,7 @@
import com.bladecoder.ink.runtime.Error.ErrorType;
import com.bladecoder.ink.runtime.SimpleJson.InnerWriter;
import com.bladecoder.ink.runtime.SimpleJson.Writer;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -19,7 +20,7 @@
* A Story is the core class that represents a complete Ink narrative, and
* manages the evaluation and state of it.
*/
public class Story extends RTObject implements VariablesState.VariableChanged {
public class Story implements VariablesState.VariableChanged {
/**
* General purpose delegate definition for bound EXTERNAL function definitions
* from ink. Note that this version isn't necessary if you have a function with
Expand Down

0 comments on commit 3baa36b

Please sign in to comment.