Minor code cleanups in mythos editor

This commit is contained in:
Brendan Robert 2016-07-05 02:53:49 -05:00
parent 2f75c3e14a
commit 4df9461029

View File

@ -7,13 +7,13 @@
* ANY KIND, either express or implied. See the License for the specific language * ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License. * governing permissions and limitations under the License.
*/ */
package org.badvision.outlaweditor; package org.badvision.outlaweditor;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -38,9 +38,9 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.badvision.outlaweditor.api.ApplicationState; import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.data.DataUtilities;
import static org.badvision.outlaweditor.data.DataUtilities.extract; import static org.badvision.outlaweditor.data.DataUtilities.extract;
import static org.badvision.outlaweditor.data.DataUtilities.extractFirst; import static org.badvision.outlaweditor.data.DataUtilities.extractFirst;
import org.badvision.outlaweditor.data.xml.Arg;
import org.badvision.outlaweditor.data.xml.Block; import org.badvision.outlaweditor.data.xml.Block;
import org.badvision.outlaweditor.data.xml.Global; import org.badvision.outlaweditor.data.xml.Global;
import org.badvision.outlaweditor.data.xml.Mutation; import org.badvision.outlaweditor.data.xml.Mutation;
@ -75,9 +75,7 @@ public class MythosEditor {
script = theScript; script = theScript;
scope = theScope; scope = theScope;
spellChecker = new SpellChecker(); spellChecker = new SpellChecker();
if (script.getBlock() != null) { fixMutators(script.getBlock());
fixMutators(script.getBlock());
}
} }
public void show() { public void show() {
@ -190,9 +188,9 @@ public class MythosEditor {
return new ArrayList<>(); return new ArrayList<>();
} else { } else {
List<Script> scripts = scriptScope.getScripts().getScript(); List<Script> scripts = scriptScope.getScripts().getScript();
List<Script> filteredList = scripts.stream().filter((Script s) -> { List<Script> filteredList = scripts.stream()
return s.getName() != null; .filter(s -> s.getName() != null)
}).collect(Collectors.toList()); .collect(Collectors.toList());
return filteredList; return filteredList;
} }
} }
@ -227,20 +225,16 @@ public class MythosEditor {
allGlobals = getGlobalVariables().stream(); allGlobals = getGlobalVariables().stream();
} }
Stream<Variable> allLocals = getLocalVariables().stream(); Stream<Variable> allLocals = getLocalVariables().stream();
return Stream.concat(allGlobals, allLocals).filter( return Stream.concat(allGlobals, allLocals)
(Variable v) -> { .filter((v) -> v.getType().equals(type))
return v.getType().equals(type); .collect(Collectors.toList());
}).collect(Collectors.toList());
} }
public List<String> getParametersForScript(Script script) { public List<String> getParametersForScript(Script script) {
List<String> allArgs = new ArrayList(); List<String> allArgs = new ArrayList();
if (script.getBlock() != null) { if (script.getBlock() != null) {
extractFirst(script.getBlock(), Mutation.class).ifPresent((m) -> { extractFirst(script.getBlock(), Mutation.class)
m.getArg().stream().forEach((a) -> { .ifPresent((m) -> m.getArg().stream().map(Arg::getName).forEach(allArgs::add));
allArgs.add(a.getName());
});
});
} }
return allArgs; return allArgs;
} }
@ -271,13 +265,14 @@ public class MythosEditor {
controls_if(MythosEditor::fixIfStatement); controls_if(MythosEditor::fixIfStatement);
Consumer<Block> rebuildMutation; Consumer<Block> rebuildMutation;
MutationType(Consumer<Block> rebuilder) { MutationType(Consumer<Block> rebuilder) {
rebuildMutation = rebuilder; rebuildMutation = rebuilder;
} }
} }
private void fixMutators(Block block) { private void fixMutators(Block block) {
extractFirst(block, Mutation.class).ifPresent((mutation)-> { extractFirst(block, Mutation.class).ifPresent((mutation) -> {
if (mutation.getOtherAttributes().isEmpty()) { if (mutation.getOtherAttributes().isEmpty()) {
try { try {
MutationType type = MutationType.valueOf(block.getType()); MutationType type = MutationType.valueOf(block.getType());
@ -288,16 +283,16 @@ public class MythosEditor {
} }
}); });
extract(block, Statement.class).map((s)->s.getBlock()).flatMap((l)->l.stream()).forEach(this::fixMutators); extract(block, Statement.class).map(Statement::getBlock).flatMap(List::stream).forEach(this::fixMutators);
if (block.getNext() != null && block.getNext().getBlock() != null) { if (block != null && block.getNext() != null && block.getNext().getBlock() != null) {
fixMutators(block.getNext().getBlock()); fixMutators(block.getNext().getBlock());
} }
} }
private static void fixIfStatement(Block block) { private static void fixIfStatement(Block block) {
Mutation mutation = extractFirst(block, Mutation.class).get(); Mutation mutation = extractFirst(block, Mutation.class).get();
long doCount = extract(block, Statement.class).filter((s)->s.getName().startsWith("DO")).collect(Collectors.counting()); long doCount = extract(block, Statement.class).filter((s) -> s.getName().startsWith("DO")).collect(Collectors.counting());
long elseCount = extract(block, Statement.class).filter((s)->s.getName().startsWith("ELSE")).collect(Collectors.counting()); long elseCount = extract(block, Statement.class).filter((s) -> s.getName().startsWith("ELSE")).collect(Collectors.counting());
if (doCount > 1) { if (doCount > 1) {
mutation.getOtherAttributes().put(new QName("elseif"), String.valueOf(doCount - 1)); mutation.getOtherAttributes().put(new QName("elseif"), String.valueOf(doCount - 1));
} }