Author: jcouteau Date: 2010-04-19 12:50:13 +0200 (Mon, 19 Apr 2010) New Revision: 203 Log: Split the set method in a method per authorized type Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2010-04-19 10:40:21 UTC (rev 202) +++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2010-04-19 10:50:13 UTC (rev 203) @@ -46,8 +46,6 @@ //Content of the list (eg elements of the list in R) private List<Object> data; private Log log = LogFactory.getLog(RDataFrame.class); - - //TODO JC implement the logger use ! /** * Create a default RList linked to a R engine (the List is not initialized * in R.) @@ -166,6 +164,11 @@ returnString = returnString.substring(0, returnString.length() - 1); } returnString += ")"; + + + + System.out.println(returnString); + return returnString; } @@ -179,8 +182,106 @@ * @throws RException * if no variable name has been given to the list */ - public void set(int x, Object data) throws RException { + public void set(int x, Double data) throws RException { checkVariable(); + + setInData(x,data); + + //in R, all data is numeric, no need to transformation, simple + //assignation + engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, + this.variable, x + 1, data)); + } + + /** + * Method to set the xth object of the list to a value. + * + * @param x + * x coordinate (from 0 to size-1) + * @param data + * value to set + * @throws RException + * if no variable name has been given to the list + */ + public void set(int x, Integer data) throws RException { + checkVariable(); + + setInData(x,data); + + //transform the data in R integer + String asInteger = String.format(RInstructions.AS_INTEGER, data); + //assign the transformed data + engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, + this.variable, x + 1, asInteger)); + } + + /** + * Method to set the xth object of the list to a value. + * + * @param x + * x coordinate (from 0 to size-1) + * @param data + * value to set + * @throws RException + * if no variable name has been given to the list + */ + public void set(int x, Boolean data) throws RException { + checkVariable(); + + setInData(x,data); + + if (data) { + engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, + this.variable, x + 1, RInstructions.TRUE)); + } else { + engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, + this.variable, x + 1, RInstructions.FALSE)); + } + } + + /** + * Method to set the xth object of the list to a value. + * + * @param x + * x coordinate (from 0 to size-1) + * @param data + * value to set + * @throws RException + * if no variable name has been given to the list + */ + public void set(int x, String data) throws RException { + checkVariable(); + + setInData(x,data); + + engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, + this.variable, x + 1, "\"" + data + "\"")); + } + + /** + * Method to set the xth object of the list to a value. + * + * @param x + * x coordinate (from 0 to size-1) + * @param data + * value to set + * @throws RException + * if no variable name has been given to the list + */ + public void set(int x, REXP data) throws RException { + checkVariable(); + + setInData(x,data); + + //Send the REXP to R + engine.voidEval(data.toRString()); + //Set the REXP as list item + engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, + this.variable, x + 1, data.getVariable())); + } + + + private void setInData(int x, Object data){ for (int i = 0; i <= x; i++) { try { this.data.get(i); @@ -194,37 +295,7 @@ this.data.add(null); } } - } - if (data instanceof Double) { - //in R, all data is numeric, no need to transformation, simple - //assignation - engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, - this.variable, x + 1, data)); - } else if (data instanceof Integer) { - //transform the data in R integer - String asInteger = String.format(RInstructions.AS_INTEGER, data); - //assign the transformed data - engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, - this.variable, x + 1, asInteger)); - } else if (data instanceof Boolean) { - if ((Boolean) data) { - engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, - this.variable, x + 1, RInstructions.TRUE)); - } else { - engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, - this.variable, x + 1, RInstructions.FALSE)); - } - } else if (data instanceof String) { - engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, - this.variable, x + 1, "\"" + data + "\"")); - } else if (data instanceof REXP) { - //Send the REXP to R - engine.voidEval(((REXP) data).toRString()); - //Set the REXP as list item - engine.voidEval(String.format(RInstructions.SET_LIST_ITEM, - this.variable, x + 1, ((REXP) data).getVariable())); - } } /**
participants (1)
-
jcouteau@users.nuiton.org