Skip to content

Commit

Permalink
Adding KType and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Nov 24, 2024
1 parent e00c2ab commit d1917dd
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 351 deletions.
361 changes: 97 additions & 264 deletions src/studio/kdb/K.java

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/studio/kdb/KTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends K.KBase>)obj);
}
return null;
Expand Down Expand Up @@ -89,8 +89,8 @@ public boolean isSortedDesc(int column) {
public Class getColumnClass(int col) {
return getColumn(col).getClass();
}
public Class<? extends K.KBase> getColumnElementClass(int col) {
return getColumn(col).getElementClass();
public KType getColumnType(int col) {
return getColumn(col).getType();
}

//@TODO: add separate method which return K.KBase
Expand Down
114 changes: 114 additions & 0 deletions src/studio/kdb/KType.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 1 addition & 1 deletion src/studio/ui/QGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 16 additions & 19 deletions src/studio/ui/chart/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +57,7 @@ public class Chart implements ComponentListener {
private static final List<Chart> charts = new ArrayList<>();


private final static Set<Class> supportedClasses = new HashSet<>();
private final static Set<KType> supportedClasses = new HashSet<>();

static {
supportedClasses.addAll(DurationEditor.VALUE_CLASSES);
Expand All @@ -82,8 +79,8 @@ public Chart(KTableModel table) {
private void initComponents() {
List<String> 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));
}
Expand Down Expand Up @@ -233,12 +230,12 @@ public void componentHidden(ComponentEvent e) {
updateFrameBounds();
}

public Class<? extends K.KBase> getDomainClass() {
return table.getColumnElementClass(xIndex);
public KType getDomainKType() {
return table.getColumnType(xIndex).getElementType();
}

public Class<? extends K.KBase> getRangeClass() {
return table.getColumnElementClass(yIndex);
public KType getRangeKType() {
return table.getColumnType(yIndex).getElementType();
}

public void refreshPlot() {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -308,7 +305,7 @@ public void refreshPlot() {
datasetIndex++;
}

chartPanel.setVisible(yClazz!=null);
chartPanel.setVisible(yType!=null);
contentPane.revalidate();
contentPane.repaint();
}
Expand Down
2 changes: 1 addition & 1 deletion src/studio/ui/chart/ChartConfigPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
40 changes: 20 additions & 20 deletions src/studio/ui/chart/DurationEditor.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,34 +15,34 @@

public class DurationEditor extends JPanel {

public static final List<Class<? extends K.KBase>> VALUE_CLASSES =
public static final List<KType> 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<Class<? extends K.KBase>> TEMPORAL_CLASSES =
public static final List<KType> 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<? extends K.KBase> 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");
}


Expand Down
35 changes: 18 additions & 17 deletions src/studio/ui/chart/KFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import studio.kdb.K;
import studio.kdb.KFormatContext;
import studio.kdb.KType;

import java.text.DecimalFormat;
import java.text.FieldPosition;
Expand All @@ -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
Expand All @@ -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));
Expand Down
Loading

0 comments on commit d1917dd

Please sign in to comment.