From 75f34853aba959dbbc51d5b047f03750e806155b Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Fri, 31 Aug 2018 12:48:39 -0700 Subject: [PATCH] Migrate to Java 1.8 --- pom.xml | 6 +- .../ca/joeltherrien/randomforest/Main.java | 4 +- .../randomforest/utils/Utils.java | 87 +++++++++++++++++++ .../randomforest/TestSavingLoading.java | 9 +- .../joeltherrien/randomforest/TestUtils.java | 2 +- .../competingrisk/TestCompetingRisk.java | 13 +-- .../TestCompetingRiskErrorRateCalculator.java | 5 +- .../covariates/FactorCovariateTest.java | 3 +- .../randomforest/csv/TestLoadingCSV.java | 5 +- .../settings/TestPersistence.java | 5 +- .../workshop/TrainSingleTree.java | 7 +- .../workshop/TrainSingleTreeFactor.java | 9 +- 12 files changed, 125 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index cf2a8fd..5d6ba5b 100644 --- a/pom.xml +++ b/pom.xml @@ -9,9 +9,9 @@ 1.0-SNAPSHOT - 1.10 - 1.10 - 1.10 + 1.8 + 1.8 + 1.8 2.9.6 diff --git a/src/main/java/ca/joeltherrien/randomforest/Main.java b/src/main/java/ca/joeltherrien/randomforest/Main.java index ee36967..37835fd 100644 --- a/src/main/java/ca/joeltherrien/randomforest/Main.java +++ b/src/main/java/ca/joeltherrien/randomforest/Main.java @@ -161,10 +161,10 @@ public class Main { yVarSettings.set("name", new TextNode("y")); final Settings settings = Settings.builder() - .covariates(List.of( + .covariates(Utils.easyList( new NumericCovariateSettings("x1"), new BooleanCovariateSettings("x2"), - new FactorCovariateSettings("x3", List.of("cat", "mouse", "dog")) + new FactorCovariateSettings("x3", Utils.easyList("cat", "mouse", "dog")) ) ) .dataFileLocation("data.csv") diff --git a/src/main/java/ca/joeltherrien/randomforest/utils/Utils.java b/src/main/java/ca/joeltherrien/randomforest/utils/Utils.java index 5acbe8d..f9b5c86 100644 --- a/src/main/java/ca/joeltherrien/randomforest/utils/Utils.java +++ b/src/main/java/ca/joeltherrien/randomforest/utils/Utils.java @@ -64,5 +64,92 @@ public class Utils { } } + /** + * Replacement for Java 9's List.of + * + * @param array + * @param + * @return A list + */ + public static List easyList(T... array){ + final List list = new ArrayList<>(array.length); + + for(final T item : array){ + list.add(item); + } + + return list; + + } + + /** + * Replacement for Java 9's Map.of + * + * @param array + * @return A map + */ + public static Map easyMap(Object... array){ + if(array.length % 2 != 0){ + throw new IllegalArgumentException("Must provide a value for every key"); + } + + final Map map = new HashMap(); + for(int i=0; i Map easyMap(K k1, V v1){ + final Map map = new HashMap<>(); + map.put(k1, v1); + + return map; + } + + /** + * Replacement for Java 9's Map.of + * @return A map + */ + public static Map easyMap(K k1, V v1, K k2, V v2){ + final Map map = new HashMap<>(); + map.put(k1, v1); + map.put(k2, v2); + + return map; + } + + /** + * Replacement for Java 9's Map.of + * @return A map + */ + public static Map easyMap(K k1, V v1, K k2, V v2, K k3, V v3){ + final Map map = new HashMap<>(); + map.put(k1, v1); + map.put(k2, v2); + map.put(k3, v3); + + return map; + } + + /** + * Replacement for Java 9's Map.of + * @return A map + */ + public static Map easyMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4){ + final Map map = new HashMap<>(); + map.put(k1, v1); + map.put(k2, v2); + map.put(k3, v3); + map.put(k4, v4); + + return map; + } + } diff --git a/src/test/java/ca/joeltherrien/randomforest/TestSavingLoading.java b/src/test/java/ca/joeltherrien/randomforest/TestSavingLoading.java index c544b37..1e5e220 100644 --- a/src/test/java/ca/joeltherrien/randomforest/TestSavingLoading.java +++ b/src/test/java/ca/joeltherrien/randomforest/TestSavingLoading.java @@ -8,6 +8,7 @@ import ca.joeltherrien.randomforest.responses.competingrisk.CompetingRiskFunctio import ca.joeltherrien.randomforest.responses.competingrisk.CompetingRiskResponse; import ca.joeltherrien.randomforest.tree.Forest; import ca.joeltherrien.randomforest.tree.ForestTrainer; +import ca.joeltherrien.randomforest.utils.Utils; import com.fasterxml.jackson.databind.node.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -35,7 +36,7 @@ public class TestSavingLoading { final ObjectNode responseCombinerSettings = new ObjectNode(JsonNodeFactory.instance); responseCombinerSettings.set("type", new TextNode("CompetingRiskResponseCombiner")); responseCombinerSettings.set("events", - new ArrayNode(JsonNodeFactory.instance, List.of(new IntNode(1), new IntNode(2))) + new ArrayNode(JsonNodeFactory.instance, Utils.easyList(new IntNode(1), new IntNode(2))) ); // not setting times @@ -43,7 +44,7 @@ public class TestSavingLoading { final ObjectNode treeCombinerSettings = new ObjectNode(JsonNodeFactory.instance); treeCombinerSettings.set("type", new TextNode("CompetingRiskFunctionCombiner")); treeCombinerSettings.set("events", - new ArrayNode(JsonNodeFactory.instance, List.of(new IntNode(1), new IntNode(2))) + new ArrayNode(JsonNodeFactory.instance, Utils.easyList(new IntNode(1), new IntNode(2))) ); // not setting times @@ -53,7 +54,7 @@ public class TestSavingLoading { yVarSettings.set("delta", new TextNode("status")); return Settings.builder() - .covariates(List.of( + .covariates(Utils.easyList( new NumericCovariateSettings("ageatfda"), new BooleanCovariateSettings("idu"), new BooleanCovariateSettings("black"), @@ -78,7 +79,7 @@ public class TestSavingLoading { } public CovariateRow getPredictionRow(List covariates){ - return CovariateRow.createSimple(Map.of( + return CovariateRow.createSimple(Utils.easyMap( "ageatfda", "35", "idu", "false", "black", "false", diff --git a/src/test/java/ca/joeltherrien/randomforest/TestUtils.java b/src/test/java/ca/joeltherrien/randomforest/TestUtils.java index 2c6ed7d..60e7a07 100644 --- a/src/test/java/ca/joeltherrien/randomforest/TestUtils.java +++ b/src/test/java/ca/joeltherrien/randomforest/TestUtils.java @@ -77,7 +77,7 @@ public class TestUtils { @Test public void reduceListToSize(){ - final List testList = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + final List testList = Utils.easyList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); for(int i=0; i<100; i++) { // want to test many times to ensure it doesn't work just due to randomness final List testList1 = new ArrayList<>(testList); diff --git a/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRisk.java b/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRisk.java index b99e618..44296d3 100644 --- a/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRisk.java +++ b/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRisk.java @@ -9,6 +9,7 @@ import ca.joeltherrien.randomforest.tree.Node; import ca.joeltherrien.randomforest.tree.TreeTrainer; import ca.joeltherrien.randomforest.utils.MathFunction; import ca.joeltherrien.randomforest.utils.Point; +import ca.joeltherrien.randomforest.utils.Utils; import com.fasterxml.jackson.databind.node.*; import org.junit.jupiter.api.Test; @@ -37,7 +38,7 @@ public class TestCompetingRisk { final ObjectNode responseCombinerSettings = new ObjectNode(JsonNodeFactory.instance); responseCombinerSettings.set("type", new TextNode("CompetingRiskResponseCombiner")); responseCombinerSettings.set("events", - new ArrayNode(JsonNodeFactory.instance, List.of(new IntNode(1), new IntNode(2))) + new ArrayNode(JsonNodeFactory.instance, Utils.easyList(new IntNode(1), new IntNode(2))) ); // not setting times @@ -45,7 +46,7 @@ public class TestCompetingRisk { final ObjectNode treeCombinerSettings = new ObjectNode(JsonNodeFactory.instance); treeCombinerSettings.set("type", new TextNode("CompetingRiskFunctionCombiner")); treeCombinerSettings.set("events", - new ArrayNode(JsonNodeFactory.instance, List.of(new IntNode(1), new IntNode(2))) + new ArrayNode(JsonNodeFactory.instance, Utils.easyList(new IntNode(1), new IntNode(2))) ); // not setting times @@ -55,7 +56,7 @@ public class TestCompetingRisk { yVarSettings.set("delta", new TextNode("status")); return Settings.builder() - .covariates(List.of( + .covariates(Utils.easyList( new NumericCovariateSettings("ageatfda"), new BooleanCovariateSettings("idu"), new BooleanCovariateSettings("black"), @@ -84,7 +85,7 @@ public class TestCompetingRisk { } public CovariateRow getPredictionRow(List covariates){ - return CovariateRow.createSimple(Map.of( + return CovariateRow.createSimple(Utils.easyMap( "ageatfda", "35", "idu", "false", "black", "false", @@ -96,7 +97,7 @@ public class TestCompetingRisk { public void testSingleTree() throws IOException { final Settings settings = getSettings(); settings.setDataFileLocation("src/test/resources/wihs.bootstrapped.csv"); - settings.setCovariates(List.of( + settings.setCovariates(Utils.easyList( new BooleanCovariateSettings("idu"), new BooleanCovariateSettings("black") )); // by only using BooleanCovariates (only one split rule) we can guarantee identical results with randomForestSRC on one tree. @@ -199,7 +200,7 @@ public class TestCompetingRisk { @Test public void testLogRankSingleGroupDifferentiatorTwoBooleans() throws IOException { final Settings settings = getSettings(); - settings.setCovariates(List.of( + settings.setCovariates(Utils.easyList( new BooleanCovariateSettings("idu"), new BooleanCovariateSettings("black") )); diff --git a/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRiskErrorRateCalculator.java b/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRiskErrorRateCalculator.java index 7a5484e..3e1d367 100644 --- a/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRiskErrorRateCalculator.java +++ b/src/test/java/ca/joeltherrien/randomforest/competingrisk/TestCompetingRiskErrorRateCalculator.java @@ -5,6 +5,7 @@ import ca.joeltherrien.randomforest.responses.competingrisk.*; import ca.joeltherrien.randomforest.tree.Forest; import ca.joeltherrien.randomforest.utils.MathFunction; import ca.joeltherrien.randomforest.utils.Point; +import ca.joeltherrien.randomforest.utils.Utils; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -26,7 +27,7 @@ public class TestCompetingRiskErrorRateCalculator { final CompetingRiskResponse response4 = new CompetingRiskResponse(1, 3.0); final double[] mortalityArray = new double[]{1, 4, 3, 9}; - final List responseList = List.of(response1, response2, response3, response4); + final List responseList = Utils.easyList(response1, response2, response3, response4); final int event = 1; @@ -52,7 +53,7 @@ public class TestCompetingRiskErrorRateCalculator { final CompetingRiskResponse response3 = new CompetingRiskResponse(2, 8.0); final CompetingRiskResponse response4 = new CompetingRiskResponse(1, 3.0); - final List> dataset = List.of( + final List> dataset = Utils.easyList( new Row<>(Collections.emptyMap(), 1, response1), new Row<>(Collections.emptyMap(), 2, response2), new Row<>(Collections.emptyMap(), 3, response3), diff --git a/src/test/java/ca/joeltherrien/randomforest/covariates/FactorCovariateTest.java b/src/test/java/ca/joeltherrien/randomforest/covariates/FactorCovariateTest.java index 4f00c6d..5a092b1 100644 --- a/src/test/java/ca/joeltherrien/randomforest/covariates/FactorCovariateTest.java +++ b/src/test/java/ca/joeltherrien/randomforest/covariates/FactorCovariateTest.java @@ -1,6 +1,7 @@ package ca.joeltherrien.randomforest.covariates; +import ca.joeltherrien.randomforest.utils.Utils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -55,7 +56,7 @@ public class FactorCovariateTest { private FactorCovariate createTestCovariate(){ - final List levels = List.of("DOG", "CAT", "MOUSE"); + final List levels = Utils.easyList("DOG", "CAT", "MOUSE"); return new FactorCovariate("pet", levels); } diff --git a/src/test/java/ca/joeltherrien/randomforest/csv/TestLoadingCSV.java b/src/test/java/ca/joeltherrien/randomforest/csv/TestLoadingCSV.java index 3037ea6..423c857 100644 --- a/src/test/java/ca/joeltherrien/randomforest/csv/TestLoadingCSV.java +++ b/src/test/java/ca/joeltherrien/randomforest/csv/TestLoadingCSV.java @@ -7,6 +7,7 @@ import ca.joeltherrien.randomforest.covariates.BooleanCovariateSettings; import ca.joeltherrien.randomforest.covariates.Covariate; import ca.joeltherrien.randomforest.covariates.FactorCovariateSettings; import ca.joeltherrien.randomforest.covariates.NumericCovariateSettings; +import ca.joeltherrien.randomforest.utils.Utils; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; @@ -37,8 +38,8 @@ public class TestLoadingCSV { final Settings settings = Settings.builder() .dataFileLocation(filename) .covariates( - List.of(new NumericCovariateSettings("x1"), - new FactorCovariateSettings("x2", List.of("dog", "cat", "mouse")), + Utils.easyList(new NumericCovariateSettings("x1"), + new FactorCovariateSettings("x2", Utils.easyList("dog", "cat", "mouse")), new BooleanCovariateSettings("x3")) ) .yVarSettings(yVarSettings) diff --git a/src/test/java/ca/joeltherrien/randomforest/settings/TestPersistence.java b/src/test/java/ca/joeltherrien/randomforest/settings/TestPersistence.java index 77bd42b..6c149ea 100644 --- a/src/test/java/ca/joeltherrien/randomforest/settings/TestPersistence.java +++ b/src/test/java/ca/joeltherrien/randomforest/settings/TestPersistence.java @@ -4,6 +4,7 @@ import ca.joeltherrien.randomforest.Settings; import static org.junit.jupiter.api.Assertions.assertEquals; import ca.joeltherrien.randomforest.covariates.*; +import ca.joeltherrien.randomforest.utils.Utils; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; @@ -31,10 +32,10 @@ public class TestPersistence { yVarSettings.set("name", new TextNode("y")); final Settings settingsOriginal = Settings.builder() - .covariates(List.of( + .covariates(Utils.easyList( new NumericCovariateSettings("x1"), new BooleanCovariateSettings("x2"), - new FactorCovariateSettings("x3", List.of("cat", "mouse", "dog")) + new FactorCovariateSettings("x3", Utils.easyList("cat", "mouse", "dog")) ) ) .dataFileLocation("data.csv") diff --git a/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTree.java b/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTree.java index 3c3234e..b68c2af 100644 --- a/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTree.java +++ b/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTree.java @@ -9,6 +9,7 @@ import ca.joeltherrien.randomforest.responses.regression.MeanResponseCombiner; import ca.joeltherrien.randomforest.responses.regression.WeightedVarianceGroupDifferentiator; import ca.joeltherrien.randomforest.tree.Node; import ca.joeltherrien.randomforest.tree.TreeTrainer; +import ca.joeltherrien.randomforest.utils.Utils; import java.util.*; import java.util.stream.Collectors; @@ -48,7 +49,7 @@ public class TrainSingleTree { trainingSet.add(generateRow(x1, x2, i)); } - final List covariateNames = List.of(x1Covariate, x2Covariate); + final List covariateNames = Utils.easyList(x1Covariate, x2Covariate); final TreeTrainer treeTrainer = TreeTrainer.builder() .groupDifferentiator(new WeightedVarianceGroupDifferentiator()) @@ -99,7 +100,7 @@ public class TrainSingleTree { public static Row generateRow(Covariate.Value x1, Covariate.Value x2, int id){ double y = generateResponse(x1.getValue(), x2.getValue()); - final Map map = Map.of("x1", x1, "x2", x2); + final Map map = Utils.easyMap("x1", x1, "x2", x2); return new Row<>(map, id, y); @@ -107,7 +108,7 @@ public class TrainSingleTree { public static CovariateRow generateCovariateRow(Covariate.Value x1, Covariate.Value x2, int id){ - final Map map = Map.of("x1", x1, "x2", x2); + final Map map = Utils.easyMap("x1", x1, "x2", x2); return new CovariateRow(map, id); diff --git a/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTreeFactor.java b/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTreeFactor.java index dd719fc..c1a6b46 100644 --- a/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTreeFactor.java +++ b/src/test/java/ca/joeltherrien/randomforest/workshop/TrainSingleTreeFactor.java @@ -9,6 +9,7 @@ import ca.joeltherrien.randomforest.responses.regression.MeanResponseCombiner; import ca.joeltherrien.randomforest.responses.regression.WeightedVarianceGroupDifferentiator; import ca.joeltherrien.randomforest.tree.Node; import ca.joeltherrien.randomforest.tree.TreeTrainer; +import ca.joeltherrien.randomforest.utils.Utils; import java.util.ArrayList; import java.util.List; @@ -29,7 +30,7 @@ public class TrainSingleTreeFactor { final Covariate x1Covariate = new NumericCovariate("x1"); final Covariate x2Covariate = new NumericCovariate("x2"); - final FactorCovariate x3Covariate = new FactorCovariate("x3", List.of("cat", "dog", "mouse")); + final FactorCovariate x3Covariate = new FactorCovariate("x3", Utils.easyList("cat", "dog", "mouse")); final List> x1List = DoubleStream .generate(() -> random.nextDouble()*10.0) @@ -69,7 +70,7 @@ public class TrainSingleTreeFactor { trainingSet.add(generateRow(x1, x2, x3, i)); } - final List covariateNames = List.of(x1Covariate, x2Covariate); + final List covariateNames = Utils.easyList(x1Covariate, x2Covariate); final TreeTrainer treeTrainer = TreeTrainer.builder() .groupDifferentiator(new WeightedVarianceGroupDifferentiator()) @@ -127,7 +128,7 @@ public class TrainSingleTreeFactor { public static Row generateRow(Covariate.Value x1, Covariate.Value x2, Covariate.Value x3, int id){ double y = generateResponse(x1.getValue(), x2.getValue(), x3.getValue()); - final Map map = Map.of("x1", x1, "x2", x2); + final Map map = Utils.easyMap("x1", x1, "x2", x2); return new Row<>(map, id, y); @@ -135,7 +136,7 @@ public class TrainSingleTreeFactor { public static CovariateRow generateCovariateRow(Covariate.Value x1, Covariate.Value x2, Covariate.Value x3, int id){ - final Map map = Map.of("x1", x1, "x2", x2, "x3", x3); + final Map map = Utils.easyMap("x1", x1, "x2", x2, "x3", x3); return new CovariateRow(map, id);