diff --git a/src/studio/kdb/K.java b/src/studio/kdb/K.java index f215f3a..109410e 100755 --- a/src/studio/kdb/K.java +++ b/src/studio/kdb/K.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Array; -import java.lang.reflect.ParameterizedType; import java.sql.Time; import java.sql.Timestamp; import java.text.DecimalFormat; @@ -53,20 +52,19 @@ public static void write(OutputStream o, long j) throws IOException { } public abstract static class KBase implements Comparable { - public abstract String getDataType(); - private final int type; + private final KType type; - protected KBase(int type) { + protected KBase(KType type) { this.type = type; } - public int getType() { + public KType getType() { return type; } public void serialise(OutputStream o) throws IOException { - write(o, (byte) type); + write(o, (byte) type.getType()); serialiseData(o); } @@ -109,7 +107,7 @@ public int compareTo(KBase o) { private abstract static class KByteBase extends KBase implements ToDouble { protected byte value; - KByteBase(int type, byte value) { + KByteBase(KType type, byte value) { super(type); this.value = value; } @@ -143,11 +141,6 @@ public boolean equals(Object obj) { return value == ((KByteBase)obj).value; } - @Override - public String getDataType() { - return null; - } - @Override public int compareTo(KBase o) { if (o instanceof KByteBase) { @@ -160,7 +153,7 @@ public int compareTo(KBase o) { public abstract static class KIntBase extends KBase implements ToDouble { protected final int value; - KIntBase(int type, int value) { + KIntBase(KType type, int value) { super(type); this.value = value; } @@ -217,7 +210,7 @@ public abstract static class KLongBase extends KBase implements ToDouble { protected final long value; - KLongBase(int type, long value) { + KLongBase(KType type, long value) { super(type); this.value = value; } @@ -270,7 +263,7 @@ public int compareTo(KBase o) { private abstract static class KDoubleBase extends KBase implements ToDouble { protected double value; - KDoubleBase(int type, double value) { + KDoubleBase(KType type, double value) { super(type); this.value = value; } @@ -322,7 +315,7 @@ public int compareTo(KBase o) { private abstract static class KArrayBase extends KBase { protected KBase[] array; - KArrayBase(int type, KBase[] array) { + KArrayBase(KType type, KBase[] array) { super(type); this.array = array; } @@ -349,13 +342,10 @@ public boolean equals(Object obj) { } public abstract static class Adverb extends KBase { - public String getDataType() { - return "Adverb"; - } protected K.KBase obj; - public Adverb(int type, K.KBase o) { + public Adverb(KType type, K.KBase o) { super(type); this.obj = o; } @@ -389,23 +379,16 @@ public boolean equals(Object obj) { public static class BinaryPrimitive extends Primitive { private final static String[] ops = {":", "+", "-", "*", "%", "&", "|", "^", "=", "<", ">", "$", ",", "#", "_", "~", "!", "?", "@", ".", "0:", "1:", "2:", "in", "within", "like", "bin", "ss", "insert", "wsum", "wavg", "div", "xexp", "setenv", "binr", "cov", "cor"}; - public String getDataType() { - return "Binary Primitive"; - } - public BinaryPrimitive(int i) { - super(102, ops, i); + super(KType.BinaryPrimitive, ops, i); } } public static class FComposition extends KArrayBase { - public String getDataType() { - return "Function Composition"; - } public FComposition(KBase... array) { - super(105, array); + super(KType.Composition, array); } @Override @@ -420,7 +403,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class FEachLeft extends Adverb { public FEachLeft(K.KBase o) { - super(111, o); + super(KType.EachLeft, o); } @Override @@ -431,7 +414,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class FEachRight extends Adverb { public FEachRight(K.KBase o) { - super(110, o); + super(KType.EachRight, o); } @Override @@ -442,7 +425,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class FPrior extends Adverb { public FPrior(K.KBase o) { - super(109, o); + super(KType.Prior, o); } @Override @@ -453,7 +436,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class Feach extends Adverb { public Feach(K.KBase o) { - super(106, o); + super(KType.Each, o); } @Override @@ -464,7 +447,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class Fover extends Adverb { public Fover(K.KBase o) { - super(107, o); + super(KType.Over, o); } @Override @@ -475,7 +458,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class Fscan extends Adverb { public Fscan(KBase o) { - super(108, o); + super(KType.Scan, o); this.obj = o; } @@ -486,14 +469,11 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { } public static class Function extends KBase { - public String getDataType() { - return "Function"; - } private final String body; public Function(String body) { - super(100); + super(KType.Function); this.body = body; } @@ -525,13 +505,10 @@ public boolean equals(Object obj) { } public abstract static class Primitive extends KByteBase { - public String getDataType() { - return "Primitive"; - } private String s = " "; - public Primitive(int type, String[] ops, int value) { + public Primitive(KType type, String[] ops, int value) { super(type, (byte) value); if (value >= 0 && value < ops.length) s = ops[value]; @@ -551,12 +528,9 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { } public static class Projection extends KArrayBase { - public String getDataType() { - return "Projection"; - } public Projection(KBase... array) { - super(104, array); + super(KType.Projection, array); } @Override @@ -577,12 +551,8 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { public static class TernaryOperator extends Primitive { private final static String[] ops = {"'", "/", "\\", "':", "/:", "\\:"}; - public String getDataType() { - return "Ternary Operator"; - } - public TernaryOperator(int i) { - super(103, ops, i); + super(KType.TernaryOperator, ops, i); } } @@ -590,7 +560,7 @@ public static class UnaryPrimitive extends Primitive { 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(101, ops, i); + super(KType.UnaryPrimitive, ops, i); } public boolean isIdentity() { @@ -599,14 +569,11 @@ public boolean isIdentity() { } public static class KBoolean extends KBase implements ToDouble { - public String getDataType() { - return "Boolean"; - } public boolean b; public KBoolean(boolean b) { - super(-1); + super(KType.Boolean); this.b = b; } @@ -661,12 +628,9 @@ public int compareTo(KBase o) { } public static class KByte extends KByteBase{ - public String getDataType() { - return "Byte"; - } public KByte(byte b) { - super(-4, b); + super(KType.Byte, b); } @Override @@ -680,9 +644,6 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { } public static class KShort extends KBase implements ToDouble { - public String getDataType() { - return "Short"; - } public short s; @@ -701,7 +662,7 @@ public boolean isNegativeInfinity() { } public KShort(short s) { - super(-5); + super(KType.Short); this.s = s; } @@ -748,12 +709,8 @@ public int compareTo(KBase o) { public static class KInteger extends KIntBase { public final static KInteger ZERO = new KInteger(0); - public String getDataType() { - return "Integer"; - } - public KInteger(int i) { - super(-6, i); + super(KType.Int, i); } public KInteger add(int increment) { @@ -773,19 +730,16 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { } public static class KSymbol extends KBase { - public String getDataType() { - return "Symbol"; - } public String s; public KSymbol(String s) { - super(-11); + super(KType.Symbol); this.s = s; } public boolean isNull() { - return s.length() == 0; + return s.isEmpty(); } @Override @@ -817,12 +771,8 @@ public static class KLong extends KLongBase { public final static KLong NULL = new KLong(NULL_VALUE); public final static KLong ZERO = new KLong(0); - public String getDataType() { - return "Long"; - } - public KLong(long j) { - super(-7, j); + super(KType.Long, j); } public KLong add(long increment) { @@ -841,14 +791,11 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { } public static class KCharacter extends KBase { - public String getDataType() { - return "Character"; - } public char c; public KCharacter(char c) { - super(-10); + super(KType.Char); this.c = c; } @@ -882,9 +829,6 @@ public boolean equals(Object obj) { } public static class KFloat extends KBase implements ToDouble { - public String getDataType() { - return "Float"; - } public float f; @@ -903,7 +847,7 @@ public boolean isNegativeInfinity() { } public KFloat(float f) { - super(-8); + super(KType.Float); this.f = f; } @@ -949,12 +893,9 @@ public int compareTo(KBase o) { } public static class KDouble extends KDoubleBase { - public String getDataType() { - return "Double"; - } public KDouble(double d) { - super(-9, d); + super(KType.Double, d); } @Override @@ -968,20 +909,12 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) { return builder; } - @Override - public void serialiseData(OutputStream o) throws IOException { - long j = Double.doubleToLongBits(value); - write(o, j); - } } public static class KDate extends KIntBase { - public String getDataType() { - return "Date"; - } public KDate(int date) { - super(-14, date); + super(KType.Date, date); } @Override @@ -1006,14 +939,10 @@ public KTimestamp toTimestamp() { public static class KGuid extends KBase { static UUID nuuid = new UUID(0, 0); - public String getDataType() { - return "Guid"; - } - UUID uuid; public KGuid(UUID uuid) { - super(-2); + super(KType.Guid); this.uuid = uuid; } @@ -1046,12 +975,9 @@ public boolean equals(Object obj) { } public static class KTime extends KIntBase { - public String getDataType() { - return "Time"; - } public KTime(int time) { - super(-19, time); + super(KType.Time, time); } @Override @@ -1082,12 +1008,9 @@ public Time toTime() { } public static class KDatetime extends KDoubleBase { - public String getDataType() { - return "Datetime"; - } public KDatetime(double time) { - super(-15, time); + super(KType.Datetime, time); } @Override @@ -1128,12 +1051,8 @@ public KTimespan span(KTimestamp t2) { return new KTimespan(t2.value - value); } - public String getDataType() { - return "Timestamp"; - } - public KTimestamp(long time) { - super(-12, time); + super(KType.Timestamp, time); } @Override @@ -1163,9 +1082,6 @@ public Timestamp toJavaTimestamp() { } public static class Dict extends KBase { - public String getDataType() { - return "Dictionary"; - } private byte attr = 0; @@ -1173,7 +1089,7 @@ public String getDataType() { public K.KBase y; public Dict(K.KBase X, K.KBase Y) { - super(99); + super(KType.Dict); x = X; y = Y; } @@ -1221,12 +1137,9 @@ public boolean equals(Object obj) { } abstract private static class FlipBase extends KBase { - public String getDataType() { - return "Flip"; - } public FlipBase() { - super(98); + super(KType.Table); } abstract public K.KBase getX(); @@ -1285,7 +1198,7 @@ public int count() { } public static class MappedTable extends FlipBase { - private K.Dict dict; + private final K.Dict dict; public MappedTable(K.Dict dict) { super(); @@ -1305,12 +1218,9 @@ public KBase getY() { //@TODO: rename to KMonth public static class Month extends KIntBase { - public String getDataType() { - return "Month"; - } public Month(int x) { - super(-13, x); + super(KType.Month, x); } @Override @@ -1340,12 +1250,9 @@ public Date toDate() { //@TODO: rename to Minute public static class Minute extends KIntBase { - public String getDataType() { - return "Minute"; - } public Minute(int x) { - super(-17, x); + super(KType.Minute, x); } @Override @@ -1371,12 +1278,9 @@ public Date toDate() { //@TODO: rename to KSecond public static class Second extends KIntBase { - public String getDataType() { - return "Second"; - } public Second(int x) { - super(-18, x); + super(KType.Second, x); } @Override @@ -1410,48 +1314,48 @@ public static class KTimespan extends KLongBase { public final static KTimespan NULL = new KTimespan(NULL_VALUE); public final static KTimespan ZERO = new KTimespan(0); - private final static Map, Long> NS_IN_TYPES = Map.of( - KTimestamp.class, 1L, - KTimespan.class, 1L, - KDate.class, NS_IN_DAY, - Month.class, NS_IN_MONTH, - Minute.class, 60 * NS_IN_SEC, - Second.class, NS_IN_SEC, - KTime.class, 1_000_000L, - KDatetime.class, NS_IN_DAY + private final static Map NS_IN_TYPES = Map.of( + KType.Timestamp, 1L, + KType.Timespan, 1L, + KType.Date, NS_IN_DAY, + KType.Month, NS_IN_MONTH, + KType.Minute, 60 * NS_IN_SEC, + KType.Second, NS_IN_SEC, + KType.Time, 1_000_000L, + KType.Datetime, NS_IN_DAY ); private final static Map NS_IN_UNITS = Map.of( - ChronoUnit.NANOS, NS_IN_TYPES.get(KTimestamp.class), - ChronoUnit.MILLIS, NS_IN_TYPES.get(KTime.class), - ChronoUnit.SECONDS, NS_IN_TYPES.get(Second.class), - ChronoUnit.MINUTES, NS_IN_TYPES.get(Minute.class), - ChronoUnit.HOURS, 60 * NS_IN_TYPES.get(Minute.class), - ChronoUnit.DAYS, NS_IN_TYPES.get(KDate.class), - ChronoUnit.MONTHS, NS_IN_TYPES.get(Month.class), - ChronoUnit.YEARS, 12 * NS_IN_TYPES.get(Month.class) + ChronoUnit.NANOS, NS_IN_TYPES.get(KType.Timestamp), + ChronoUnit.MILLIS, NS_IN_TYPES.get(KType.Time), + ChronoUnit.SECONDS, NS_IN_TYPES.get(KType.Second), + ChronoUnit.MINUTES, NS_IN_TYPES.get(KType.Minute), + ChronoUnit.HOURS, 60 * NS_IN_TYPES.get(KType.Minute), + ChronoUnit.DAYS, NS_IN_TYPES.get(KType.Date), + ChronoUnit.MONTHS, NS_IN_TYPES.get(KType.Month), + ChronoUnit.YEARS, 12 * NS_IN_TYPES.get(KType.Month) ); private final static List SUPPORTED_UNiTS = List.of(NS_IN_UNITS.keySet().toArray(new ChronoUnit[0])); - public final static ChronoUnit[] getSupportedUnits() { + public static ChronoUnit[] getSupportedUnits() { return new ChronoUnit[] { ChronoUnit.NANOS, ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES, ChronoUnit.HOURS, ChronoUnit.DAYS, ChronoUnit.MONTHS, ChronoUnit.YEARS }; } - public static KTimespan duration(double duration, Class unitClass) { - if (! NS_IN_TYPES.containsKey(unitClass)) - throw new IllegalArgumentException(unitClass.toString() + " is not supported"); + public static KTimespan duration(double duration, KType unitType) { + if (! NS_IN_TYPES.containsKey(unitType)) + throw new IllegalArgumentException(unitType.toString() + " is not supported"); - return new KTimespan((long) (duration * NS_IN_TYPES.get(unitClass))); + return new KTimespan((long) (duration * NS_IN_TYPES.get(unitType))); } - public double toUnitValue(Class unitClass) { - if (! NS_IN_TYPES.containsKey(unitClass)) - throw new IllegalArgumentException(unitClass.toString() + " is not supported"); + public double toUnitValue(KType unitType) { + if (! NS_IN_TYPES.containsKey(unitType)) + throw new IllegalArgumentException(unitType.toString() + " is not supported"); - return value / (double) NS_IN_TYPES.get(unitClass); + return value / (double) NS_IN_TYPES.get(unitType); } public static KTimespan duration(double duration, ChronoUnit unit) { @@ -1470,17 +1374,13 @@ public double toUnitValue(ChronoUnit unit) { } public KTimespan(long x) { - super(-16, x); + super(KType.Timespan, x); } public KTimespan add(KTimespan increment) { return new KTimespan(value + increment.value); } - public String getDataType() { - return "Timespan"; - } - @Override public StringBuilder format(StringBuilder builder, KFormatContext context) { builder = super.format(builder, context); @@ -1509,8 +1409,8 @@ public Time toTime() { } - private static java.text.DecimalFormat i2Formatter = new java.text.DecimalFormat("00"); - private static java.text.DecimalFormat i3Formatter = new java.text.DecimalFormat("000"); + private static final java.text.DecimalFormat i2Formatter = new java.text.DecimalFormat("00"); + private static final java.text.DecimalFormat i3Formatter = new java.text.DecimalFormat("000"); private static String i2(int i) { return i2Formatter.format(i); @@ -1522,23 +1422,13 @@ private static String i3(int i) { public static abstract class KBaseVector extends KBase { protected Object array; - private int length; + private final int length; private byte attr = 0; - private final String typeName; - private final String typeChar; - private final Class elementClass; - protected KBaseVector(Object array, int type, String typeName, String typeChar) { + protected KBaseVector(Object array, KType type) { super(type); this.array = array; this.length = Array.getLength(array); - this.typeName = typeName; - this.typeChar = typeChar; - elementClass = (Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; - } - - public Class getElementClass() { - return elementClass; } public abstract E at(int i); @@ -1569,7 +1459,7 @@ public Object getArray() { //default implementation protected StringBuilder formatVector(StringBuilder builder, KFormatContext context) { - if (getLength() == 0) builder.append("`").append(typeName).append("$()"); + if (getLength() == 0) builder.append("`").append(getType().getName()).append("$()"); else { if (getLength() == 1) builder.append(enlist); KFormatContext childContext = context.showType() ? new KFormatContext(context).setShowType(false) : context; @@ -1577,7 +1467,7 @@ protected StringBuilder formatVector(StringBuilder builder, KFormatContext conte if (i > 0) builder.append(" "); at(i).format(builder, childContext); } - if (context.showType()) builder.append(typeChar); + if (context.showType()) builder.append(getType().getVectorFormatEnding()); } return builder; } @@ -1600,7 +1490,7 @@ public void serialiseData(OutputStream o) throws IOException { @Override public int hashCode() { - return length*getType(); + return length * getType().getType(); } @Override @@ -1612,12 +1502,9 @@ public boolean equals(Object obj) { } public static class KShortVector extends KBaseVector { - public String getDataType() { - return "Short Vector"; - } public KShortVector(short... array) { - super(array, 5, "short", "h"); + super(array, KType.ShortVector); } public KShort at(int i) { @@ -1626,12 +1513,9 @@ public KShort at(int i) { } public static class KIntVector extends KBaseVector { - public String getDataType() { - return "Int Vector"; - } public KIntVector(int... array) { - super(array, 6, "int", "i"); + super(array, KType.IntVector); } public KInteger at(int i) { @@ -1640,12 +1524,9 @@ public KInteger at(int i) { } public static class KList extends KBaseVector { - public String getDataType() { - return "List"; - } public KList(KBase... array) { - super(array, 0, "", ""); + super(array, KType.List); } public KBase at(int i) { @@ -1676,12 +1557,9 @@ public void serialiseData(OutputStream o) throws IOException { } public static class KDoubleVector extends KBaseVector { - public String getDataType() { - return "Double Vector"; - } public KDoubleVector(double... array) { - super(array, 9, "float", "f"); + super(array, KType.DoubleVector); } public KDouble at(int i) { @@ -1690,12 +1568,9 @@ public KDouble at(int i) { } public static class KFloatVector extends KBaseVector { - public String getDataType() { - return "Float Vector"; - } public KFloatVector(float... array) { - super(array, 8, "real", "e"); + super(array, KType.FloatVector); } public KFloat at(int i) { @@ -1705,12 +1580,9 @@ public KFloat at(int i) { } public static class KLongVector extends KBaseVector { - public String getDataType() { - return "Long Vector"; - } public KLongVector(long... array) { - super(array, 7, "long", ""); + super(array, KType.LongVector); } public KLong at(int i) { @@ -1719,12 +1591,9 @@ public KLong at(int i) { } public static class KMonthVector extends KBaseVector { - public String getDataType() { - return "Month Vector"; - } public KMonthVector(int... array) { - super(array, 13, "month", "m"); + super(array, KType.MonthVector); } public Month at(int i) { @@ -1733,12 +1602,9 @@ public Month at(int i) { } public static class KDateVector extends KBaseVector { - public String getDataType() { - return "Date Vector"; - } public KDateVector(int... array) { - super(array, 14, "date", ""); + super(array, KType.DateVector); } public KDate at(int i) { @@ -1747,12 +1613,9 @@ public KDate at(int i) { } public static class KGuidVector extends KBaseVector { - public String getDataType() { - return "Guid Vector"; - } public KGuidVector(UUID... array) { - super(array, 2, "guid", ""); + super(array, KType.GuidVector); } public KGuid at(int i) { @@ -1761,12 +1624,9 @@ public KGuid at(int i) { } public static class KMinuteVector extends KBaseVector { - public String getDataType() { - return "Minute Vector"; - } public KMinuteVector(int... array) { - super(array, 17, "minute", ""); + super(array, KType.MinuteVector); } public Minute at(int i) { @@ -1775,12 +1635,9 @@ public Minute at(int i) { } public static class KDatetimeVector extends KBaseVector { - public String getDataType() { - return "Datetime Vector"; - } public KDatetimeVector(double... array) { - super(array, 15, "datetime", ""); + super(array, KType.DatetimeVector); } public KDatetime at(int i) { @@ -1789,12 +1646,9 @@ public KDatetime at(int i) { } public static class KTimestampVector extends KBaseVector { - public String getDataType() { - return "Timestamp Vector"; - } public KTimestampVector(long... array) { - super(array, 12, "timestamp", ""); + super(array, KType.TimestampVector); } public KTimestamp at(int i) { @@ -1803,12 +1657,9 @@ public KTimestamp at(int i) { } public static class KTimespanVector extends KBaseVector { - public String getDataType() { - return "Timespan Vector"; - } public KTimespanVector(long... array) { - super(array, 16, "timespan", ""); + super(array, KType.TimespanVector); } public KTimespan at(int i) { @@ -1817,12 +1668,9 @@ public KTimespan at(int i) { } public static class KSecondVector extends KBaseVector { - public String getDataType() { - return "Second Vector"; - } public KSecondVector(int... array) { - super(array, 18, "second", ""); + super(array, KType.SecondVector); } public Second at(int i) { @@ -1831,12 +1679,9 @@ public Second at(int i) { } public static class KTimeVector extends KBaseVector { - public String getDataType() { - return "Time Vector"; - } public KTimeVector(int... array) { - super(array, 19, "time", ""); + super(array, KType.TimeVector); } public KTime at(int i) { @@ -1845,12 +1690,9 @@ public KTime at(int i) { } public static class KBooleanVector extends KBaseVector { - public String getDataType() { - return "Boolean Vector"; - } public KBooleanVector(boolean... array) { - super(array, 1, "boolean", "b"); + super(array, KType.BooleanVector); } public KBoolean at(int i) { @@ -1871,12 +1713,9 @@ protected StringBuilder formatVector(StringBuilder builder, KFormatContext conte } public static class KByteVector extends KBaseVector { - public String getDataType() { - return "Byte Vector"; - } public KByteVector(byte... array) { - super(array, 4, "byte", "x"); + super(array, KType.ByteVector); } public KByte at(int i) { @@ -1900,12 +1739,9 @@ protected StringBuilder formatVector(StringBuilder builder, KFormatContext conte } public static class KSymbolVector extends KBaseVector { - public String getDataType() { - return "Symbol Vector"; - } public KSymbolVector(String... array) { - super(array, 11, "symbol", "s"); + super(array, KType.SymbolVector); } public KSymbol at(int i) { @@ -1925,12 +1761,9 @@ protected StringBuilder formatVector(StringBuilder builder, KFormatContext conte } public static class KCharacterVector extends KBaseVector { - public String getDataType() { - return "Character Vector"; - } public KCharacterVector(String value) { - super(value.toCharArray(), 10, "char", "c"); + super(value.toCharArray(), KType.CharVector); } public KCharacter at(int i) { diff --git a/src/studio/kdb/KTableModel.java b/src/studio/kdb/KTableModel.java index e632f64..3426a15 100755 --- a/src/studio/kdb/KTableModel.java +++ b/src/studio/kdb/KTableModel.java @@ -23,7 +23,7 @@ public static KTableModel getModel(K.KBase obj) { } } - if ((obj instanceof K.KBaseVector) && obj.getType() != 10 && obj.getType() != 4) { + if ((obj instanceof K.KBaseVector) && obj.getType() != KType.CharVector && obj.getType() != KType.ByteVector) { return new ListModel((K.KBaseVector)obj); } return null; @@ -89,8 +89,8 @@ public boolean isSortedDesc(int column) { public Class getColumnClass(int col) { return getColumn(col).getClass(); } - public Class getColumnElementClass(int col) { - return getColumn(col).getElementClass(); + public KType getColumnType(int col) { + return getColumn(col).getType(); } //@TODO: add separate method which return K.KBase diff --git a/src/studio/kdb/KType.java b/src/studio/kdb/KType.java new file mode 100644 index 0000000..1d84d92 --- /dev/null +++ b/src/studio/kdb/KType.java @@ -0,0 +1,114 @@ +package studio.kdb; + +public enum KType { + Boolean(-1,"boolean", 'b'), + Guid(-2, "guid", 'g'), + Byte(-4, "byte", 'x'), + Short(-5, "short", 'h'), + Int(-6, "int", 'i'), + Long(-7, "long", 'j'), + Float(-8, "real", 'e'), + Double(-9, "float", 'f'), + Char(-10, "char", 'c'), + Symbol(-11, "symbol", 's'), + Timestamp(-12, "timestamp", 'p'), + Month(-13, "month", 'm'), + Date(-14, "date", 'd'), + Datetime(-15, "datetime", 'z'), + Timespan(-16, "timespan", 'n'), + Minute(-17, "minute", 'u'), + Second(-18, "second", 'v'), + Time(-19, "time", 't'), + + List(0), + BooleanVector(Boolean, true), + GuidVector(Guid), + ByteVector(Byte, true), + ShortVector(Short, true), + IntVector(Int, true), + LongVector(Long), + FloatVector(Float, true), + DoubleVector(Double, true), + CharVector(Char), + SymbolVector(Symbol), + TimestampVector(Timestamp), + MonthVector(Month, true), + DateVector(Date), + DatetimeVector(Datetime), + TimespanVector(Timespan), + MinuteVector(Minute), + SecondVector(Second), + TimeVector(Time), + + Table(98), + Dict(99), + Function(100), + UnaryPrimitive(101), + BinaryPrimitive(102), + TernaryOperator(103), + Projection(104), + Composition(105), + Each(106), + Over(107), + Scan(108), + Prior(109), + EachRight(110), + EachLeft(111), + + ; + + private final int type; + private final String name; + private final char typeChar; + private final KType elementType; + private final boolean requireFormatEnding; + + public int getType() { + return type; + } + + public String getName() { + return name; + } + + public char getTypeChar() { + return typeChar; + } + + public String getVectorFormatEnding() { + if (!requireFormatEnding) return ""; + return "" + typeChar; + } + + public boolean isVector() { + return elementType != null; + } + + public KType getElementType() { + return elementType; + } + + KType(int type) { + this(type, "", ' '); + } + + KType(int type, String name, char typeChar) { + this(type, name, typeChar, null, false); + } + + KType(int type, String name, char typeChar, KType elementType, boolean requireFormatEnding) { + this.type = type; + this.elementType = elementType; + this.name = name; + this.typeChar = typeChar; + this.requireFormatEnding = requireFormatEnding; + } + + KType(KType elementType) { + this(elementType, false); + } + KType(KType elementType, boolean requireFormatEnding) { + this(-elementType.type, elementType.name, elementType.typeChar, elementType, requireFormatEnding); + } + +} diff --git a/src/studio/ui/QGrid.java b/src/studio/ui/QGrid.java index 15c2fa4..29c049e 100755 --- a/src/studio/ui/QGrid.java +++ b/src/studio/ui/QGrid.java @@ -243,7 +243,7 @@ private void doubleClick(int row, int col) { K.KBase b = (K.KBase) table.getValueAt(row, col); - int type = b.getType(); + int type = b.getType().getType(); if ( (type >= -19 && type <= -1) || (type >= 101 && type <= 103 ) || type == 10 || type == 4) { diff --git a/src/studio/ui/chart/Chart.java b/src/studio/ui/chart/Chart.java index b4ecc38..d423024 100755 --- a/src/studio/ui/chart/Chart.java +++ b/src/studio/ui/chart/Chart.java @@ -17,10 +17,7 @@ import org.jfree.data.xy.IntervalXYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; -import studio.kdb.Config; -import studio.kdb.K; -import studio.kdb.KTableModel; -import studio.kdb.ToDouble; +import studio.kdb.*; import studio.ui.StudioOptionPane; import studio.ui.Toolbar; import studio.ui.Util; @@ -60,7 +57,7 @@ public class Chart implements ComponentListener { private static final List charts = new ArrayList<>(); - private final static Set supportedClasses = new HashSet<>(); + private final static Set supportedClasses = new HashSet<>(); static { supportedClasses.addAll(DurationEditor.VALUE_CLASSES); @@ -82,8 +79,8 @@ public Chart(KTableModel table) { private void initComponents() { List namesList = new ArrayList<>(); for (int index = 0; index < table.getColumnCount(); index++) { - Class clazz = table.getColumnElementClass(index); - if (supportedClasses.contains(clazz)) { + KType type = table.getColumnType(index).getElementType(); + if (supportedClasses.contains(type)) { indexes.add(index); namesList.add(table.getColumnName(index)); } @@ -233,12 +230,12 @@ public void componentHidden(ComponentEvent e) { updateFrameBounds(); } - public Class getDomainClass() { - return table.getColumnElementClass(xIndex); + public KType getDomainKType() { + return table.getColumnType(xIndex).getElementType(); } - public Class getRangeClass() { - return table.getColumnElementClass(yIndex); + public KType getRangeKType() { + return table.getColumnType(yIndex).getElementType(); } public void refreshPlot() { @@ -251,17 +248,17 @@ public void refreshPlot() { } int xIndex = indexes.get(pnlConfig.getDomainIndex()); - Class xClazz = table.getColumnElementClass(xIndex); + KType xType = table.getColumnType(xIndex).getElementType(); if (this.xIndex != xIndex) { NumberAxis xAxis = new NumberAxis(""); - xAxis.setNumberFormatOverride(new KFormat(xClazz)); + xAxis.setNumberFormatOverride(new KFormat(xType)); xAxis.setAutoRangeIncludesZero(false); plot.setDomainAxis(xAxis); this.xIndex = xIndex; } - Class yClazz = null; + KType yType = null; plot.setDomainPannable(true); plot.setRangePannable(true); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); @@ -271,11 +268,11 @@ public void refreshPlot() { int yIndex = indexes.get(index); if (yIndex == xIndex) continue;; - if (yClazz == null) { - yClazz = table.getColumnElementClass(yIndex); + if (yType == null) { + yType = table.getColumnType(yIndex).getElementType(); if (this.yIndex != yIndex) { NumberAxis yAxis = new NumberAxis(""); - yAxis.setNumberFormatOverride(new KFormat(yClazz)); + yAxis.setNumberFormatOverride(new KFormat(yType)); yAxis.setAutoRangeIncludesZero(false); plot.setRangeAxis(yAxis); this.yIndex = yIndex; @@ -285,7 +282,7 @@ public void refreshPlot() { IntervalXYDataset dataset = getDateset(xIndex, yIndex); XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, - new KFormat(xClazz), new KFormat(yClazz)); + new KFormat(xType), new KFormat(yType)); XYItemRenderer renderer; LegendIcon icon = pnlConfig.getLegendIcon(index); @@ -308,7 +305,7 @@ public void refreshPlot() { datasetIndex++; } - chartPanel.setVisible(yClazz!=null); + chartPanel.setVisible(yType!=null); contentPane.revalidate(); contentPane.repaint(); } diff --git a/src/studio/ui/chart/ChartConfigPanel.java b/src/studio/ui/chart/ChartConfigPanel.java index 03f84d3..5652c3a 100644 --- a/src/studio/ui/chart/ChartConfigPanel.java +++ b/src/studio/ui/chart/ChartConfigPanel.java @@ -94,7 +94,7 @@ public void addLine(Line line) { lines.add(line); listLines.add(title, line.getIcon()); - new LineInfoFrame(line, chart.getDomainClass(), chart.getRangeClass()); + new LineInfoFrame(line, chart.getDomainKType(), chart.getRangeKType()); } private void refresh() { diff --git a/src/studio/ui/chart/DurationEditor.java b/src/studio/ui/chart/DurationEditor.java index dec8a54..e943ccc 100644 --- a/src/studio/ui/chart/DurationEditor.java +++ b/src/studio/ui/chart/DurationEditor.java @@ -1,6 +1,6 @@ package studio.ui.chart; -import studio.kdb.K; +import studio.kdb.KType; import studio.ui.chart.event.ValueChangedEvent; import studio.ui.chart.event.ValueChangedListener; @@ -15,34 +15,34 @@ public class DurationEditor extends JPanel { - public static final List> VALUE_CLASSES = + public static final List VALUE_CLASSES = List.of( - K.KInteger.class, - K.KDouble.class, - K.KFloat.class, - K.KShort.class, - K.KLong.class + KType.Int, + KType.Double, + KType.Float, + KType.Short, + KType.Long ); - public static final List> TEMPORAL_CLASSES = + public static final List TEMPORAL_CLASSES = List.of( - K.KDate.class, - K.KTime.class, - K.KTimestamp.class, - K.KTimespan.class, - K.KDatetime.class, - K.Month.class, - K.Second.class, - K.Minute.class + KType.Date, + KType.Time, + KType.Timestamp, + KType.Timespan, + KType.Datetime, + KType.Month, + KType.Second, + KType.Minute ); private final EventListenerList listenerList = new EventListenerList(); - public static DurationEditor create(Class unitClass) { - if (VALUE_CLASSES.contains(unitClass)) return new DurationEditor(); - if (TEMPORAL_CLASSES.contains(unitClass)) return new TimespanEditor(unitClass); + public static DurationEditor create(KType unitType) { + if (VALUE_CLASSES.contains(unitType)) return new DurationEditor(); + if (TEMPORAL_CLASSES.contains(unitType)) return new TimespanEditor(unitType); - throw new UnsupportedOperationException("DurationEditor for class " + unitClass + " is not supported"); + throw new UnsupportedOperationException("DurationEditor for type " + unitType + " is not supported"); } diff --git a/src/studio/ui/chart/KFormat.java b/src/studio/ui/chart/KFormat.java index 125a747..93bec6b 100644 --- a/src/studio/ui/chart/KFormat.java +++ b/src/studio/ui/chart/KFormat.java @@ -2,6 +2,7 @@ import studio.kdb.K; import studio.kdb.KFormatContext; +import studio.kdb.KType; import java.text.DecimalFormat; import java.text.FieldPosition; @@ -10,12 +11,12 @@ public class KFormat extends NumberFormat { - private Class kClass; + private KType type; private static final DecimalFormat fractionFormat = new DecimalFormat("+0.#####;-0.#####"); - public KFormat(Class kClass) { - this.kClass = kClass; + public KFormat(KType type) { + this.type = type; } @Override @@ -32,32 +33,32 @@ public Number parse(String source, ParsePosition parsePosition) { public StringBuffer format(double value, StringBuffer toAppendTo, FieldPosition pos) { boolean addFraction = true; K.KBase kValue; - if (kClass == K.KInteger.class || - kClass == K.KDouble.class || - kClass == K.KFloat.class || - kClass == K.KShort.class || - kClass == K.KLong.class) { + if (type == KType.Int || + type == KType.Double || + type == KType.Float || + type == KType.Short || + type == KType.Long) { kValue = new K.KDouble(value); addFraction = false; - } else if (kClass == K.KDatetime.class) { + } else if (type == KType.Datetime) { kValue = new K.KDatetime(value); addFraction = false; - } else if (kClass == K.KDate.class) { + } else if (type == KType.Date) { kValue = new K.KDate(getInt(value)); - } else if (kClass == K.KTime.class) { + } else if (type == KType.Time) { kValue = new K.KTime(getInt(value)); - } else if (kClass == K.KTimestamp.class) { + } else if (type == KType.Timestamp) { kValue = new K.KTimestamp(getLong(value)); - } else if (kClass == K.KTimespan.class) { + } else if (type == KType.Timespan) { kValue = new K.KTimespan(getLong(value)); - } else if (kClass == K.Month.class) { + } else if (type == KType.Month) { kValue = new K.Month(getInt(value)); - } else if (kClass == K.Second.class) { + } else if (type == KType.Second) { kValue = new K.Second(getInt(value)); - } else if (kClass == K.Minute.class) { + } else if (type == KType.Minute) { kValue = new K.Minute(getInt(value)); } else { - throw new IllegalArgumentException("Unsupported class: " + kClass); + throw new IllegalArgumentException("Unsupported type: " + type); } toAppendTo.append(kValue.toString(KFormatContext.NO_TYPE)); diff --git a/src/studio/ui/chart/LineInfoFrame.java b/src/studio/ui/chart/LineInfoFrame.java index e93a79d..2329d99 100644 --- a/src/studio/ui/chart/LineInfoFrame.java +++ b/src/studio/ui/chart/LineInfoFrame.java @@ -1,6 +1,6 @@ package studio.ui.chart; -import studio.kdb.K; +import studio.kdb.KType; import studio.ui.GroupLayoutSimple; import javax.swing.*; @@ -31,10 +31,10 @@ public class LineInfoFrame extends JFrame { private boolean lockDX = true; private boolean lockX = true; - public LineInfoFrame(Line line, Class xClazz, Class yClazz) { + public LineInfoFrame(Line line, KType xType, KType yType) { this.line = line; - txtDX = DurationEditor.create(xClazz); - txtDY = DurationEditor.create(yClazz); + txtDX = DurationEditor.create(xType); + txtDY = DurationEditor.create(yType); line.addChangeListener(e -> refresh()); initComponents(); diff --git a/src/studio/ui/chart/TimespanEditor.java b/src/studio/ui/chart/TimespanEditor.java index b40f1a4..a3f132d 100644 --- a/src/studio/ui/chart/TimespanEditor.java +++ b/src/studio/ui/chart/TimespanEditor.java @@ -1,6 +1,7 @@ package studio.ui.chart; import studio.kdb.K; +import studio.kdb.KType; import javax.swing.*; import java.awt.*; @@ -8,14 +9,14 @@ public class TimespanEditor extends DurationEditor { - private final Class unitClass; + private final KType unittype; private K.KTimespan value = K.KTimespan.NULL; private final JComboBox comboUnit = new JComboBox<>(K.KTimespan.getSupportedUnits()); - public TimespanEditor(Class unitClass) { - this.unitClass = unitClass; + public TimespanEditor(KType unitType) { + this.unittype = unitType; add(comboUnit, BorderLayout.EAST); comboUnit.addActionListener(e -> refresh()); @@ -37,7 +38,7 @@ private void unitAutoSelect(K.KTimespan value) { } public void setValue(double value) { - K.KTimespan newValue = K.KTimespan.duration(value, unitClass); + K.KTimespan newValue = K.KTimespan.duration(value, unittype); if (newValue.equals(this.value)) return; if (this.value.equals(K.KTimespan.NULL)) { @@ -51,7 +52,7 @@ public void setValue(double value) { } public double getValue() { - return value.toUnitValue(unitClass); + return value.toUnitValue(unittype); } protected void refresh() { @@ -62,20 +63,4 @@ protected void txtValueChanged(double newValue) { value = K.KTimespan.duration(newValue, (ChronoUnit) comboUnit.getSelectedItem()); } - - - public static void main(String[] args) { - - JFrame f = new JFrame("Test"); - TimespanEditor editor = new TimespanEditor(K.Second.class); - editor.setValue(600); - editor.addValueChangedListener(e -> System.out.printf("New value: %f\n", e.getValue()) ); - - f.setContentPane(editor); - - f.setSize(200,50); - f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - f.setVisible(true); - } - } diff --git a/test/studio/kdb/KTest.java b/test/studio/kdb/KTest.java index 2c60afc..fbe5a61 100644 --- a/test/studio/kdb/KTest.java +++ b/test/studio/kdb/KTest.java @@ -430,7 +430,7 @@ public void testDateToTimestamp() { @Test public void testElementClass() { K.KList list = new K.KList(new K.KInteger(10), K.KInteger.ZERO); - assertEquals(K.KBase.class, list.getElementClass()); + assertNull(list.getType().getElementType()); } }