diff --git a/src/biotree/BioTree.java b/src/biotree/BioTree.java index 89a88b96f65935ba15853286f52559391e12a9e9..c5d6e930344c5642444138baa4d1fe2afe01c482 100644 --- a/src/biotree/BioTree.java +++ b/src/biotree/BioTree.java @@ -45,9 +45,10 @@ public class BioTree { * @param taxonId The taxonId of the possible new entry * @return taxonId of new species entry */ - public static int processRecord(int taxonId) { + public static Integer processRecord(int taxonId) { //pass taxonId directly to function to add / increment it - processTaxonId(taxonId); + if (processTaxonId(taxonId)) return null; + System.out.println(taxonId); return taxonId; } @@ -59,10 +60,10 @@ public class BioTree { * @return taxonId of new / existing entry * @throws IOException */ - public static int processRecord(String scientificName) throws IOException { + public static Integer processRecord(String scientificName) throws IOException { //reverse lookup based on name, try adding the found taxonId. int taxonId = WormsAPI.nameToID(scientificName); - processTaxonId(taxonId); + if (processTaxonId(taxonId)) return null; return taxonId; } @@ -70,8 +71,9 @@ public class BioTree { * Process a new entry if it doesn't exist. If it does exist, increment the number * of Records for this classification by one. * @param taxonId New / existing TaxonID to add / increment count thereof. + * @return true if the process failed, false if nothing went wrong */ - private static void processTaxonId(int taxonId) { + private static boolean processTaxonId(int taxonId) { TaxonNode[] newNodes = null; //possible eventual new nodes TaxonNode tx = nodes.get(taxonId); //search tree to see if the node exists already if (tx != null) //if it does exist, increment its count @@ -83,6 +85,7 @@ public class BioTree { // TODO Auto-generated catch block e.printStackTrace(); } + if (newNodes == null) return true; newNodes[newNodes.length - 1].incCount(); //one of the new nodes exists for (int i = newNodes.length - 1; i >= 0; i--) { //iterate over all node starting from lowest child @@ -102,6 +105,7 @@ public class BioTree { break; } } + return false; } /** diff --git a/src/biotree/TestBioTree.java b/src/biotree/TestBioTree.java index 6391b5fb4c7596e98a744b5bfed1852363a5bad9..67005ca5968e23db0f0a7d051e68d02a0f93647d 100644 --- a/src/biotree/TestBioTree.java +++ b/src/biotree/TestBioTree.java @@ -20,6 +20,7 @@ public class TestBioTree { BioTree.processRecord(125122); BioTree.processRecord(125392); BioTree.processRecord(125391); + BioTree.processRecord(600000000); //System.out.println(nodes.size()); //System.out.println(nodes.get(123207)); //Iterable<Integer> keys = nodes.keys(); diff --git a/src/biotree/WormsAPI.java b/src/biotree/WormsAPI.java index 58bdc477ce91aeddb814c60486d33031f2f8056c..79c0ccab86a9d3657b5a5c3a99c368b60861f834 100644 --- a/src/biotree/WormsAPI.java +++ b/src/biotree/WormsAPI.java @@ -79,6 +79,7 @@ public class WormsAPI { public static TaxonNode[] idToClassification(int taxonId) throws IOException, ParseException { String resp = makeRequest( String.format("http://marinespecies.org/rest/AphiaClassificationByAphiaID/%d", taxonId)); + if (resp.length() == 0) return null; JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(resp);