diff --git a/src/studio/kdb/K.java b/src/studio/kdb/K.java index 7ad9f791..8de54179 100755 --- a/src/studio/kdb/K.java +++ b/src/studio/kdb/K.java @@ -10,15 +10,12 @@ import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; +import java.util.function.ToLongFunction; public class K { - private static SimpleDateFormat formatter = new SimpleDateFormat(); - private static DecimalFormat nsFormatter = new DecimalFormat("000000000"); + private static final SimpleDateFormat formatter = new SimpleDateFormat(); + private static final DecimalFormat nsFormatter = new DecimalFormat("000000000"); static { formatter.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); @@ -61,11 +58,6 @@ public void serialise(OutputStream o) throws IOException { } - /* public String toString(boolean showType) { - return ""; - } - ; - */ public String toString() { return toString(true); } @@ -84,7 +76,7 @@ public void setAttr(byte attr) { this.attr = attr; } - private static String[] sAttr = new String[]{"", "`s#", "`u#", "`p#", "`g#"}; + private static final String[] sAttr = new String[]{"", "`s#", "`u#", "`p#", "`g#"}; public String toString(boolean showType) { if (attr <= sAttr.length) @@ -100,6 +92,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class Adverb extends KBase { + @Override public String getDataType() { return "Adverb"; } @@ -118,6 +111,7 @@ public Object getObject() { public static class BinaryPrimitive extends Primitive { private static final String[] ops = {":", "+", "-", "*", "%", "&", "|", "^", "=", "<", ">", "$", ",", "#", "_", "~", "!", "?", "@", ".", "0:", "1:", "2:", "in", "within", "like", "bin", "ss", "insert", "wsum", "wavg", "div", "xexp", "setenv", "binr", "cov", "cor"}; + @Override public String getDataType() { return "Binary Primitive"; } @@ -127,6 +121,7 @@ public BinaryPrimitive(int i) { type = 102; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(getPrimitive()); } @@ -135,6 +130,7 @@ public void toString(Writer w, boolean showType) throws IOException { public static class FComposition extends KBase { Object[] objs; + @Override public String getDataType() { return "Function Composition"; } @@ -155,6 +151,7 @@ public FEachLeft(K.KBase o) { type = 111; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("\\:"); @@ -167,6 +164,7 @@ public FEachRight(K.KBase o) { type = 110; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("/:"); @@ -179,6 +177,7 @@ public FPrior(K.KBase o) { type = 109; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("':"); @@ -191,6 +190,7 @@ public Feach(K.KBase o) { type = 106; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("'"); @@ -203,6 +203,7 @@ public Fover(K.KBase o) { type = 107; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("/"); @@ -216,6 +217,7 @@ public Fscan(KBase o) { this.o = o; } + @Override public void toString(Writer w, boolean showType) throws IOException { o.toString(w, showType); w.write("\\"); @@ -223,11 +225,12 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class Function extends KBase { + @Override public String getDataType() { return "Function"; } - private String body; + private final String body; public Function(KCharacterVector body) { type = 100; @@ -238,27 +241,35 @@ public String getBody() { return body; } + @Override public String toString(boolean showType) { return body; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(body); } } public static class Primitive extends KBase { + @Override public String getDataType() { return "Primitive"; } - private int primitive; - private String s = " "; + private final int primitive; + private final String s; public Primitive(String[] ops, int i) { primitive = i; - if (i >= 0 && i < ops.length) - s = ops[i]; + s = (i >= 0 && i < ops.length) ? ops[i] : " "; + } + + + @Override + public String toString(boolean showType) { + return getPrimitive(); } public String getPrimitive() { @@ -271,17 +282,19 @@ public int getPrimitiveAsInt() { } public static class Projection extends KBase { + @Override public String getDataType() { return "Projection"; } - private K.KList objs; + private final K.KList objs; public Projection(K.KList objs) { type = 104; this.objs = objs; } + @Override public void toString(Writer w, boolean showType) throws IOException { boolean listProjection = false; if ((objs.getLength() > 0) && (objs.at(0) instanceof UnaryPrimitive)) { @@ -330,6 +343,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class TernaryOperator extends KBase { + @Override public String getDataType() { return "Ternary Operator"; } @@ -341,7 +355,7 @@ public static void init(char[] ops, int[] values) { map.put(values[i], ops[i]); } - private int primitive; + private final int primitive; private char charVal = ' '; @@ -365,19 +379,21 @@ public int getPrimitiveAsInt() { return primitive; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(charVal); } } public static class UnaryPrimitive extends Primitive { - private static String[] ops = {"::", "+:", "-:", "*:", "%:", "&:", "|:", "^:", "=:", "<:", ">:", "$:", ",:", "#:", "_:", "~:", "!:", "?:", "@:", ".:", "0::", "1::", "2::", "avg", "last", "sum", "prd", "min", "max", "exit", "getenv", "abs", "sqrt", "log", "exp", "sin", "asin", "cos", "acos", "tan", "atan", "enlist", "var", "dev", "hopen"}; + private static final String[] ops = {"::", "+:", "-:", "*:", "%:", "&:", "|:", "^:", "=:", "<:", ">:", "$:", ",:", "#:", "_:", "~:", "!:", "?:", "@:", ".:", "0::", "1::", "2::", "avg", "last", "sum", "prd", "min", "max", "exit", "getenv", "abs", "sqrt", "log", "exp", "sin", "asin", "cos", "acos", "tan", "atan", "enlist", "var", "dev", "hopen"}; public UnaryPrimitive(int i) { super(ops, i); type = 101; } + @Override public void toString(Writer w, boolean showType) throws IOException { if (getPrimitiveAsInt() == -1) return; @@ -386,6 +402,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class Variable extends KBase { + @Override public String getDataType() { return "Variable"; } @@ -419,6 +436,7 @@ public void setType(short type) { } public static class KBoolean extends KBase implements ToDouble { + @Override public String getDataType() { return "Boolean"; } @@ -430,6 +448,7 @@ public KBoolean(boolean b) { type = -1; } + @Override public String toString(boolean showType) { String s = b ? "1" : "0"; if (showType) @@ -437,10 +456,12 @@ public String toString(boolean showType) { return s; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public double toDouble() { return b ? 1.0 : 0.0; } @@ -451,12 +472,14 @@ public boolean toBoolean() { } public static class KByte extends KBase implements ToDouble { + @Override public String getDataType() { return "Byte"; } public byte b; + @Override public double toDouble() { return b; } @@ -466,22 +489,26 @@ public KByte(byte b) { type = -4; } + @Override public String toString(boolean showType) { return "0x" + Integer.toHexString((b >> 4) & 0xf) + Integer.toHexString(b & 0xf); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KShort extends KBase implements ToDouble { + @Override public String getDataType() { return "Short"; } public short s; + @Override public double toDouble() { return s; } @@ -491,10 +518,12 @@ public KShort(short s) { type = -5; } + @Override public boolean isNull() { return s == Short.MIN_VALUE; } + @Override public String toString(boolean showType) { String t; if (s == Short.MIN_VALUE) @@ -510,18 +539,21 @@ else if (s == -Short.MAX_VALUE) return t; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KInteger extends KBase implements ToDouble { + @Override public String getDataType() { return "Integer"; } public int i; + @Override public double toDouble() { return i; } @@ -531,10 +563,12 @@ public KInteger(int i) { type = -6; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { String s; if (isNull()) @@ -550,12 +584,14 @@ else if (i == -Integer.MAX_VALUE) return s; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KSymbol extends KBase { + @Override public String getDataType() { return "Symbol"; } @@ -567,45 +603,58 @@ public KSymbol(String s) { type = -11; } + @Override public String toString(boolean showType) { return s; } + @Override public boolean isNull() { return s.length() == 0; } + @Override public void toString(Writer w, boolean showType) throws IOException { if (showType) w.write("`"); w.write(s); } + @Override public void serialise(OutputStream o) throws IOException { o.write(s.getBytes(Config.getInstance().getEncoding())); } } - public static class KLong extends KBase implements ToDouble { + public static class KLong extends KBase implements ToDouble, ToLongFunction { + @Override public String getDataType() { return "Long"; } public long j; + @Override public double toDouble() { return j; } + @Override + public long applyAsLong(KLong o) { + return j; + } + public KLong(long j) { this.j = j; type = -7; } + @Override public boolean isNull() { return j == Long.MIN_VALUE; } + @Override public String toString(boolean showType) { String s; if (isNull()) @@ -623,10 +672,12 @@ else if (j == -Long.MAX_VALUE) return s; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, j); @@ -634,6 +685,7 @@ public void serialise(OutputStream o) throws IOException { } public static class KCharacter extends KBase { + @Override public String getDataType() { return "Character"; } @@ -645,10 +697,12 @@ public KCharacter(char c) { type = -10; } + @Override public boolean isNull() { return c == ' '; } + @Override public String toString(boolean showType) { if (showType) return "\"" + c + "\""; @@ -656,10 +710,12 @@ public String toString(boolean showType) { return "" + c; } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) c); @@ -667,12 +723,14 @@ public void serialise(OutputStream o) throws IOException { } public static class KFloat extends KBase implements ToDouble { + @Override public String getDataType() { return "Float"; } public float f; + @Override public double toDouble() { return f; } @@ -682,10 +740,12 @@ public KFloat(float f) { this.f = f; } + @Override public boolean isNull() { return Float.isNaN(f); } + @Override public String toString(boolean showType) { if (isNull()) return "0ne"; @@ -705,10 +765,12 @@ else if (f == Float.NEGATIVE_INFINITY) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); int i = Float.floatToIntBits(f); @@ -717,6 +779,7 @@ public void serialise(OutputStream o) throws IOException { } public static class KDouble extends KBase implements ToDouble { + @Override public String getDataType() { return "Double"; } @@ -728,14 +791,17 @@ public KDouble(double d) { this.d = d; } + @Override public double toDouble() { return d; } + @Override public boolean isNull() { return Double.isNaN(d); } + @Override public String toString(boolean showType) { if (isNull()) return "0n"; @@ -755,10 +821,12 @@ else if (d == Double.NEGATIVE_INFINITY) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); long j = Double.doubleToLongBits(d); @@ -767,6 +835,7 @@ public void serialise(OutputStream o) throws IOException { } public static class KDate extends KBase { + @Override public String getDataType() { return "Date"; } @@ -778,10 +847,12 @@ public KDate(int date) { this.date = date; } + @Override public boolean isNull() { return date == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nd"; @@ -793,6 +864,7 @@ else if (date == -Integer.MAX_VALUE) return sd("yyyy.MM.dd", new Date(86400000L * (date + 10957))); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -805,6 +877,7 @@ public Date toDate() { public static class KGuid extends KBase { static UUID nuuid = new UUID(0, 0); + @Override public String getDataType() { return "Guid"; } @@ -816,20 +889,24 @@ public KGuid(UUID uuid) { this.uuid = uuid; } + @Override public boolean isNull() { return uuid == nuuid; } + @Override public String toString(boolean showType) { return uuid.toString(); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class KTime extends KBase { + @Override public String getDataType() { return "Time"; } @@ -841,10 +918,12 @@ public KTime(int time) { this.time = time; } + @Override public boolean isNull() { return time == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nt"; @@ -856,6 +935,7 @@ else if (time == -Integer.MAX_VALUE) return sd("HH:mm:ss.SSS", new Time(time)); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -866,6 +946,7 @@ public Time toTime() { } public static class KDatetime extends KBase { + @Override public String getDataType() { return "Datetime"; } @@ -877,10 +958,12 @@ public KDatetime(double time) { this.time = time; } + @Override public boolean isNull() { return Double.isNaN(time); } + @Override public String toString(boolean showType) { if (isNull()) return "0nz"; @@ -892,6 +975,7 @@ else if (time == Double.NEGATIVE_INFINITY) return sd("yyyy.MM.dd HH:mm:ss.SSS", toTimestamp()); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -903,6 +987,7 @@ public Timestamp toTimestamp() { public static class KTimestamp extends KBase { + @Override public String getDataType() { return "Timestamp"; } @@ -914,10 +999,12 @@ public KTimestamp(long time) { this.time = time; } + @Override public boolean isNull() { return time == Long.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Np"; @@ -931,6 +1018,7 @@ else if (time == -Long.MAX_VALUE) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -948,6 +1036,7 @@ public Timestamp toTimestamp() { } public static class Dict extends KBase { + @Override public String getDataType() { return "Dictionary"; } @@ -976,9 +1065,10 @@ public void upsert(K.Dict upd) { cy.append(updy); } + @Override public void toString(Writer w, boolean showType) throws IOException { boolean useBrackets = getAttr() != 0 || x instanceof Flip; - super.toString(w, showType); + w.write(super.toString(showType)); if (useBrackets) w.write("("); x.toString(w, showType); @@ -987,9 +1077,15 @@ public void toString(Writer w, boolean showType) throws IOException { w.write("!"); y.toString(w, showType); } + + @Override + public String toString(boolean showType) { + return "(" + x.toString(showType) + ")!" + y.toString(showType); + } } public static class Flip extends KBase { + @Override public String getDataType() { return "Flip"; } @@ -1003,6 +1099,7 @@ public Flip(Dict X) { y = (K.KBaseVector) X.y; } + @Override public void toString(Writer w, boolean showType) throws IOException { boolean usebracket = x.getLength() == 1; w.write(flip); @@ -1022,6 +1119,7 @@ public void append(Flip nf) { } public static class Month extends KBase { + @Override public String getDataType() { return "Month"; } @@ -1033,10 +1131,12 @@ public Month(int x) { i = x; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nm"; @@ -1056,16 +1156,18 @@ else if (i == -Integer.MAX_VALUE) public Date toDate() { int m = i + 24000, y = m / 12; Calendar cal = Calendar.getInstance(); - cal.set(y, m, 01); + cal.set(y, m, 1); return cal.getTime(); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } } public static class Minute extends KBase { + @Override public String getDataType() { return "Minute"; } @@ -1077,10 +1179,12 @@ public Minute(int x) { i = x; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nu"; @@ -1092,6 +1196,7 @@ else if (i == -Integer.MAX_VALUE) return i2(i / 60) + ":" + i2(i % 60); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -1105,6 +1210,7 @@ public Date toDate() { } public static class Second extends KBase { + @Override public String getDataType() { return "Second"; } @@ -1116,10 +1222,12 @@ public Second(int x) { i = x; } + @Override public boolean isNull() { return i == Integer.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nv"; @@ -1131,6 +1239,7 @@ else if (i == -Integer.MAX_VALUE) return new Minute(i / 60).toString() + ':' + i2(i % 60); } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -1152,14 +1261,17 @@ public KTimespan(long x) { type = -16; } + @Override public String getDataType() { return "Timespan"; } + @Override public boolean isNull() { return j == Long.MIN_VALUE; } + @Override public String toString(boolean showType) { if (isNull()) return "0Nn"; @@ -1184,6 +1296,7 @@ else if (j == -Long.MAX_VALUE) } } + @Override public void toString(Writer w, boolean showType) throws IOException { w.write(toString(showType)); } @@ -1231,6 +1344,27 @@ protected int calcCapacity(int length) { return (int) (1.1 * length); } + @Override + public final String toString(boolean showType) { + try { + CharArrayWriter writer = new CharArrayWriter(); + writer.write(super.toString(showType)); + toString(writer, showType); + return writer.toString(); + } catch (IOException e) { + e.printStackTrace(); + return super.toString(showType); + } + } + + @Override + public final void toString(Writer w, boolean showType) throws IOException { + w.write(super.toString(showType)); + toStringVector(w, showType); + } + + abstract void toStringVector(Writer w, boolean showType) throws IOException; + public void append(KBaseVector x) { if ((x.getLength() + getLength()) > Array.getLength(getArray())) { int newLength = Array.getLength(getArray()) + x.getLength(); @@ -1244,6 +1378,7 @@ public void append(KBaseVector x) { } public static class KShortVector extends KBaseVector { + @Override public String getDataType() { return "Short Vector"; } @@ -1253,13 +1388,13 @@ public KShortVector(int length) { type = 5; } + @Override public KBase at(int i) { return new KShort(Array.getShort(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`short$()"); else { @@ -1286,6 +1421,7 @@ else if (v == -Short.MAX_VALUE) } public static class KIntVector extends KBaseVector { + @Override public String getDataType() { return "Int Vector"; } @@ -1295,13 +1431,13 @@ public KIntVector(int length) { type = 6; } + @Override public KBase at(int i) { return new KInteger(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`int$()"); else { @@ -1327,6 +1463,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KList extends KBaseVector { + @Override public String getDataType() { return "List"; } @@ -1336,13 +1473,13 @@ public KList(int length) { type = 0; } + @Override public KBase at(int i) { return (KBase) Array.get(array, i); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 1) w.write(enlist); else @@ -1358,6 +1495,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KDoubleVector extends KBaseVector { + @Override public String getDataType() { return "Double Vector"; } @@ -1367,13 +1505,13 @@ public KDoubleVector(int length) { type = 9; } + @Override public KBase at(int i) { return new KDouble(Array.getDouble(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`float$()"); else { @@ -1410,6 +1548,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KFloatVector extends KBaseVector { + @Override public String getDataType() { return "Float Vector"; } @@ -1419,13 +1558,13 @@ public KFloatVector(int length) { type = 8; } + @Override public KBase at(int i) { return new KFloat(Array.getFloat(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`real$()"); else { @@ -1460,6 +1599,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KLongVector extends KBaseVector { + @Override public String getDataType() { return "Long Vector"; } @@ -1469,13 +1609,13 @@ public KLongVector(int length) { type = 7; } + @Override public KBase at(int i) { return new KLong(Array.getLong(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`long$()"); else { @@ -1502,6 +1642,7 @@ else if (v == -Long.MAX_VALUE) } public static class KMonthVector extends KBaseVector { + @Override public String getDataType() { return "Month Vector"; } @@ -1511,13 +1652,13 @@ public KMonthVector(int length) { type = 13; } + @Override public KBase at(int i) { return new Month(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`month$()"); else { @@ -1546,6 +1687,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KDateVector extends KBaseVector { + @Override public String getDataType() { return "Date Vector"; } @@ -1555,13 +1697,13 @@ public KDateVector(int length) { type = 14; } + @Override public KBase at(int i) { return new KDate(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`date$()"); else { @@ -1590,6 +1732,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KGuidVector extends KBaseVector { + @Override public String getDataType() { return "Guid Vector"; } @@ -1599,13 +1742,13 @@ public KGuidVector(int length) { type = 2; } + @Override public KBase at(int i) { return new KGuid((UUID) Array.get(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`guid$()"); else { @@ -1621,6 +1764,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KMinuteVector extends KBaseVector { + @Override public String getDataType() { return "Minute Vector"; } @@ -1630,13 +1774,13 @@ public KMinuteVector(int length) { type = 17; } + @Override public KBase at(int i) { return new Minute(Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`minute$()"); else { @@ -1660,6 +1804,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KDatetimeVector extends KBaseVector { + @Override public String getDataType() { return "Datetime Vector"; } @@ -1669,13 +1814,13 @@ public KDatetimeVector(int length) { type = 15; } + @Override public KBase at(int i) { return new KDatetime(Array.getDouble(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`datetime$()"); else { @@ -1706,6 +1851,7 @@ else if (d == Double.NEGATIVE_INFINITY) } public static class KTimestampVector extends KBaseVector { + @Override public String getDataType() { return "Timestamp Vector"; } @@ -1715,13 +1861,13 @@ public KTimestampVector(int length) { type = 12; } + @Override public KBase at(int i) { return new KTimestamp(Array.getLong(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`timestamp$()"); else { @@ -1737,6 +1883,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KTimespanVector extends KBaseVector { + @Override public String getDataType() { return "Timespan Vector"; } @@ -1746,13 +1893,13 @@ public KTimespanVector(int length) { type = 16; } + @Override public KBase at(int i) { return new KTimespan(Array.getLong(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`timespan$()"); else { @@ -1768,6 +1915,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KSecondVector extends KBaseVector { + @Override public String getDataType() { return "Second Vector"; } @@ -1777,10 +1925,12 @@ public KSecondVector(int length) { type = 18; } + @Override public KBase at(int i) { return new Second(Array.getInt(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1789,8 +1939,8 @@ public void serialise(OutputStream o) throws IOException { write(o, Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`second$()"); @@ -1815,6 +1965,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KTimeVector extends KBaseVector { + @Override public String getDataType() { return "Time Vector"; } @@ -1824,10 +1975,12 @@ public KTimeVector(int length) { type = 19; } + @Override public KBase at(int i) { return new KTime(Array.getInt(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1836,9 +1989,8 @@ public void serialise(OutputStream o) throws IOException { write(o, Array.getInt(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`time$()"); else { @@ -1862,6 +2014,7 @@ else if (v == -Integer.MAX_VALUE) } public static class KBooleanVector extends KBaseVector { + @Override public String getDataType() { return "Boolean Vector"; } @@ -1871,10 +2024,12 @@ public KBooleanVector(int length) { type = 1; } + @Override public KBase at(int i) { return new KBoolean(Array.getBoolean(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1883,8 +2038,8 @@ public void serialise(OutputStream o) throws IOException { write(o, (byte) (Array.getBoolean(array, i) ? 1 : 0)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`boolean$()"); else { @@ -1898,6 +2053,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KByteVector extends KBaseVector { + @Override public String getDataType() { return "Byte Vector"; } @@ -1907,10 +2063,12 @@ public KByteVector(int length) { type = 4; } + @Override public KBase at(int i) { return new KByte(Array.getByte(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); write(o, (byte) 0); @@ -1919,8 +2077,8 @@ public void serialise(OutputStream o) throws IOException { write(o, Array.getByte(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 0) w.write("`byte$()"); else { @@ -1937,6 +2095,7 @@ public void toString(Writer w, boolean showType) throws IOException { } public static class KSymbolVector extends KBaseVector { + @Override public String getDataType() { return "Symbol Vector"; } @@ -1946,23 +2105,29 @@ public KSymbolVector(int length) { type = 11; } + @Override public KBase at(int i) { return new KSymbol((String) Array.get(array, i)); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); - if (getLength() == 0) - w.write("0#`"); - else if (getLength() == 1) - w.write(enlist); - for (int i = 0; i < getLength(); i++) - w.write("`" + Array.get(array, i)); + void toStringVector(Writer w, boolean showType) throws IOException { + if (getLength() == 0) { + w.write("`symbol$()"); + } else { + if (getLength() == 1) { + w.write(enlist); + } + + for (int i = 0; i < getLength(); i++) { + w.append("`").append(Array.get(array, i).toString()); + } + } } } public static class KCharacterVector extends KBaseVector { + @Override public String getDataType() { return "Character Vector"; } @@ -1985,10 +2150,12 @@ public KCharacterVector(String s) { type = 10; } + @Override public KBase at(int i) { return new KCharacter(Array.getChar(array, i)); } + @Override public void serialise(OutputStream o) throws IOException { super.serialise(o); byte[] b = new String((char[]) array).getBytes(Config.getInstance().getEncoding()); @@ -1997,8 +2164,8 @@ public void serialise(OutputStream o) throws IOException { o.write(b); } - public void toString(Writer w, boolean showType) throws IOException { - w.write(super.toString(showType)); + @Override + void toStringVector(Writer w, boolean showType) throws IOException { if (getLength() == 1) w.write(enlist); @@ -2009,16 +2176,6 @@ public void toString(Writer w, boolean showType) throws IOException { if (showType) w.write("\""); } - - public String toString(boolean showType) { - CharArrayWriter w = new CharArrayWriter(); - try { - toString(w, showType); - } catch (IOException e) { - e.printStackTrace(System.err); - } - return w.toString(); - } } public static String decode(KBase obj, boolean showType) { diff --git a/src/studio/kdb/LimitedWriter.java b/src/studio/kdb/LimitedWriter.java index 051d7f42..0f996c3e 100755 --- a/src/studio/kdb/LimitedWriter.java +++ b/src/studio/kdb/LimitedWriter.java @@ -4,7 +4,9 @@ import java.io.IOException; public class LimitedWriter extends CharArrayWriter { - private int limit; + private static final String TOO_LONG = " ... "; + + private final int limit; public static class LimitException extends RuntimeException { } @@ -15,20 +17,21 @@ public LimitedWriter(int limit) { public void write(char c) throws IOException { if ((1 + size()) > limit) { - super.write(" ... "); + super.write(TOO_LONG); throw new LimitException(); } super.write(c); } - public void write(String s) throws IOException { + public void write(String s, int off, int len) { if ((size() + s.length()) > limit) { - if (limit>size()) { - super.write(s.substring(0,limit - size())); + if (limit > size()) { + String substring = s.substring(0, limit - size()); + super.write(substring, 0, substring.length()); } - super.write(" ... "); + super.write(TOO_LONG, 0, TOO_LONG.length()); throw new LimitException(); } - super.write(s); + super.write(s, off, len); } }