@SuppressWarnings("unchecked") private VectorOfDouble encode(ScilabType var, VectorOfDouble vec) { switch (var.getType()) { case sci_matrix: encode((ScilabDouble) var, vec); break; case sci_ints: encode((ScilabInteger) var, vec); break; case sci_boolean: encode((ScilabBoolean) var, vec); break; case sci_strings: encode((ScilabString) var, vec); break; case sci_list: encode((ArrayList<ScilabType>) var, vec, var.getType()); break; case sci_mlist: encode((ArrayList<ScilabType>) var, vec, var.getType()); break; case sci_tlist: encode((ArrayList<ScilabType>) var, vec, var.getType()); break; default: break; } return vec; }
/** * Encode any scilab type to a buffer * * @param var the Scilab value to encode * @return the encoded buffer */ public VectorOfDouble var2vec(ScilabType var) { final ScilabType value; if (var == null) { value = new ScilabDouble(); } else { value = var; } if (LOG.isLoggable(Level.FINER)) { LOG.entering(ScilabTypeCoder.class.getCanonicalName(), "var2vec"); } VectorOfDouble vec = new VectorOfDouble(); encode(value, vec); // System.err.println("var2vec:" + var.toString() + ":" + toString(vec)); if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "var2vec:{0}:{1}", new Object[] {var.toString(), toString(vec)}); } if (LOG.isLoggable(Level.FINER)) { LOG.exiting(ScilabTypeCoder.class.getCanonicalName(), "var2vec"); } return vec; }
@SuppressWarnings("unchecked") private ScilabType decode(VectorOfDouble vec, ScilabType var) { switch (var.getType()) { case sci_matrix: decode(vec, (ScilabDouble) var); break; case sci_ints: decode(vec, (ScilabInteger) var); break; case sci_boolean: decode(vec, (ScilabBoolean) var); break; case sci_strings: decode(vec, (ScilabString) var); break; case sci_list: decode(vec, (ArrayList<ScilabType>) var); break; case sci_mlist: decode(vec, (ArrayList<ScilabType>) var); break; case sci_tlist: decode(vec, (ArrayList<ScilabType>) var); break; default: break; } return var; }
/** * Decode a scilab type from a buffer * * @param vec the buffer containing encoded scilab types * @return the decoded scilab type */ public ScilabType vec2var(VectorOfDouble vec) { position = 0; if (LOG.isLoggable(Level.FINER)) { LOG.entering(ScilabTypeCoder.class.getName(), "vec2var", vec); } ScilabType var = decodeHeader(vec); decode(vec, var); // System.err.println("vec2var:" + toString(vec) + ":" + var.toString()); if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "vec2var:{0}:{1}", new Object[] {toString(vec), var.toString()}); } if (LOG.isLoggable(Level.FINER)) { LOG.exiting(ScilabTypeCoder.class.getName(), "vec2var"); } return var; }
/** * Helper method to add an header of the detected type * * @param var the scilab matrix type to encode * @param vec the raw encoded data container * @param as the type to encode * @param detected the detected type */ @SuppressWarnings({"unchecked", "fallthrough"}) private void encodeHeader(Object var, VectorOfDouble vec, final ScilabTypeEnum as) { ScilabType matrix = null; ArrayList<ScilabType> list = null; // defensive programming switch (as) { case sci_boolean: case sci_ints: case sci_matrix: case sci_strings: matrix = (ScilabType) var; break; case sci_list: case sci_mlist: case sci_tlist: list = (ArrayList<ScilabType>) var; break; default: throw new IllegalArgumentException(); } vec.add(as.swigValue()); if (matrix instanceof ScilabInteger) { vec.add(((ScilabInteger) matrix).getPrec().swigValue()); } if (matrix != null) { vec.add(2); vec.add(matrix.getHeight()); vec.add(matrix.getWidth()); } else if (list != null) { vec.add(list.size()); } else { throw new IllegalArgumentException(); } }