Skip to content

Commit

Permalink
54 declare a column as a pivot type and use it in the pivot operation (
Browse files Browse the repository at this point in the history
…#55)

* feat: Add a new header type and process it in IntelliTable

* refacto: Can pass pivot types through transformable sheet

* fix: Fix header cloning

* fix: Fix header cloning

* feat: Add a new header type and process it in IntelliTable

---------

Co-authored-by: Romuald Rousseau <[email protected]>
  • Loading branch information
RomualdRousseau and Romuald Rousseau authored Nov 21, 2024
1 parent 893a68c commit 95d473a
Show file tree
Hide file tree
Showing 18 changed files with 415 additions and 272 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.github.romualdrousseau.archery.commons.python;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class PythonSimpleDateFormat extends SimpleDateFormat {

private final Locale locale;

public PythonSimpleDateFormat() {
super();
this("", Locale.US);
}

public PythonSimpleDateFormat(final String pattern) {
super(PythonSimpleDateFormat.toJava(pattern));
}

public PythonSimpleDateFormat(final String pattern, DateFormatSymbols formatSymbols) {
super(PythonSimpleDateFormat.toJava(pattern), formatSymbols);
this(pattern, PythonSimpleDateFormat.toJavaLocale(pattern));
}

public PythonSimpleDateFormat(final String pattern, Locale locale) {
super(PythonSimpleDateFormat.toJava(pattern), locale);
this.locale = locale;
}

public static String toPython(final String javaPattern) {
Expand Down Expand Up @@ -68,7 +66,6 @@ public static String toJava(final String pythonPattern) {
.replaceAll("%w", "u")
.replaceAll("%u", "u")
.replaceAll("%U", "ww")
.replaceAll("%V", "ww")
.replaceAll("%H", "HH")
.replaceAll("%-H", "H")
.replaceAll("%I", "hh")
Expand All @@ -78,4 +75,16 @@ public static String toJava(final String pythonPattern) {
.replaceAll("%S", "ss")
.replaceAll("%-S", "s");
}

public static Locale toJavaLocale(final String pythonPattern) {
if (pythonPattern.contains("%w") || pythonPattern.contains("%W")) {
return Locale.UK;
} else {
return Locale.US;
}
}

public Locale getLocale() {
return this.locale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void processHeader(final BaseCell cell, final String symbol) {
this.firstRowHeader = false;
}
} else if (this.firstRowHeader) {
if (!this.disablePivot && symbol.equals("e") && cell.isPivotHeader() && cell.getColumnIndex() > 0) {
if (!this.disablePivot && symbol.equals("e") && cell.isPivotKeyHeader() && cell.getColumnIndex() > 0) {
var foundPivot = this.dataTable.findFirstPivotHeader();
if (foundPivot == null) {
foundPivot = new PivotKeyHeader(this.dataTable, cell);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void processHeader(final BaseCell cell, final String symbol) {
this.firstRowHeader = false;
}
} else if (this.firstRowHeader) {
if (!this.disablePivot && symbol.equals("e") && cell.isPivotHeader() && cell.getColumnIndex() > 0) {
if (!this.disablePivot && symbol.equals("e") && cell.isPivotKeyHeader() && cell.getColumnIndex() > 0) {
var foundPivot = this.dataTable.findFirstPivotHeader();
if (foundPivot == null) {
foundPivot = new PivotKeyHeader(this.dataTable, cell);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,24 +290,46 @@ public void disablePivot() {
this.sheet.disablePivot();
}

/**
* This method sets the pivot option for the sheet using the given option
* string.
*
* @param option the option string: "NONE", "WITH_TYPE", "WITH_TYPE_AND_VALUE"
*/
public void setPivotOption(final String option) {
this.sheet.setPivotOption(Enum.valueOf(PivotOption.class, option));
}

/**
* This method sets the pivot entities for the sheet using the given list of
* entities.
*
* @param pivotEntityList the list of entities as a list of string
*
* @deprecated use {@link TransformableSheet#setPivotKeyEntityList(List<String>)}
*/
public void setPivotEntityList(final List<String> pivotEntityList) {
this.sheet.setPivotEntityList(pivotEntityList);
this.sheet.setPivotKeyEntityList(pivotEntityList);
}

/**
* This method sets the pivot option for the sheet using the given option
* string.
* This method sets the pivot key for the sheet using the given list of
* entities.
*
* @param option the option string: "NONE", "WITH_TYPE", "WITH_TYPE_AND_VALUE"
* @param pivotKeyEntityList the list of entities as a list of string
*/
public void setPivotOption(final String option) {
this.sheet.setPivotOption(Enum.valueOf(PivotOption.class, option));
public void setPivotKeyEntityList(final List<String> pivotKeyEntityList) {
this.sheet.setPivotKeyEntityList(pivotKeyEntityList);
}

/**
* This method sets the pivot type for the sheet using the given list of
* entities.
*
* @param pivotTypeEntityList the list of entities as a list of string
*/
public void setPivotTypeEntityList(final List<String> pivotTypeEntityList) {
this.sheet.setPivotTypeEntityList(pivotTypeEntityList);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,19 @@ public Tensor getEntityVector() {
return this.entityVector;
}

public boolean isPivotHeader() {
return this.getPivotEntityAsString().isPresent();
public boolean isPivotKeyHeader() {
return this.getPivotKeyEntityAsString().isPresent();
}

public Optional<String> getPivotEntityAsString() {
public boolean isPivotTypeHeader() {
final var pivotTypeEntityList = this.sheet.getPivotTypeEntityList();
return pivotTypeEntityList.stream().anyMatch(x -> this.value.matches(x));
}

public Optional<String> getPivotKeyEntityAsString() {
if (this.sheet != null) {
final var pivotEntityList = this.sheet.getPivotEntityList();
return this.entities().stream().filter(x -> pivotEntityList.contains(x)).findFirst();
final var pivotKeyEntityList = this.sheet.getPivotKeyEntityList();
return this.entities().stream().filter(x -> pivotKeyEntityList.contains(x)).findFirst();
} else {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

public abstract class BaseHeader implements Header {

private final BaseCell cell;

private BaseTable table;
private int colIndex;
private boolean columnEmpty;

public BaseHeader(final BaseTable table, final BaseCell cell) {
this.table = table;
this.cell = cell;
Expand Down Expand Up @@ -44,7 +50,7 @@ public boolean isColumnMerged() {
return false;
}

public void setColumnEmpty(boolean columnEmpty) {
public void setColumnEmpty(final boolean columnEmpty) {
this.columnEmpty = columnEmpty;
}

Expand Down Expand Up @@ -72,12 +78,16 @@ public boolean hasRowGroup() {
return false;
}

public boolean isPivotHeader() {
return this.cell.isPivotHeader();
public boolean isPivotKeyHeader() {
return this.cell.isPivotKeyHeader();
}

public boolean isPivotTypeHeader() {
return this.cell.isPivotTypeHeader();
}

public Optional<String> getPivotEntityAsString() {
return this.cell.getPivotEntityAsString();
public Optional<String> getPivotKeyEntityAsString() {
return this.cell.getPivotKeyEntityAsString();
}

@Override
Expand All @@ -89,12 +99,7 @@ public boolean equals(final Object o) {
return other != null && this.getName().equalsIgnoreCase(other.getName());
}

public abstract String getValue();

public abstract BaseHeader clone();

private BaseTable table;
private final BaseCell cell;
private int colIndex;
private boolean columnEmpty;
public abstract String getValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,40 @@ public void disablePivot() {
this.pivotEnabled = false;
}

public List<String> getPivotEntityList() {
public PivotOption getPivotOption() {
return this.pivotOption;
}

public void setPivotOption(final PivotOption option) {
this.pivotOption = option;
}

public List<String> getPivotKeyEntityList() {
if (!this.pivotEnabled) {
return Collections.emptyList();
}
if (this.pivotEntityList == null) {
if (this.pivotKeyEntityList == null) {
return this.getDocument().getModel().getPivotEntityList();
}
return this.pivotEntityList;
return this.pivotKeyEntityList;
}

public void setPivotEntityList(final List<String> pivotEntityList) {
this.pivotEntityList = pivotEntityList;
public void setPivotKeyEntityList(final List<String> pivotKeyEntityList) {
this.pivotKeyEntityList = pivotKeyEntityList;
}

public PivotOption getPivotOption() {
return this.pivotOption;
public List<String> getPivotTypeEntityList() {
if (!this.pivotEnabled) {
return Collections.emptyList();
}
if (this.pivotTypeEntityList == null) {
return Collections.emptyList();
}
return this.pivotTypeEntityList;
}

public void setPivotOption(final PivotOption option) {
this.pivotOption = option;
public void setPivotTypeEntityList(final List<String> pivotTypeEntityList) {
this.pivotTypeEntityList = pivotTypeEntityList;
}

public String getPivotKeyFormat() {
Expand Down Expand Up @@ -479,5 +493,6 @@ private int computeLastColumnNum() {
private String pivotTypeFormat;
private String groupValueFormat;
private String columnValueFormat;
private List<String> pivotEntityList;
private List<String> pivotKeyEntityList;
private List<String> pivotTypeEntityList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@

public class DataTableHeader extends BaseHeader {

public DataTableHeader(final BaseHeader parent) {
this(parent.getTable(), parent.getCell());
}
private String name;
private HeaderTag tag;
private List<String> entities;

public DataTableHeader(final BaseTable table, final BaseCell cell) {
this(table, cell, cell.getValue(), null, null);
}

protected DataTableHeader(final BaseTable table, final BaseCell cell, final String name, final HeaderTag tag,
final List<String> entities) {
super(table, cell);
this.name = this.getCell().getValue();
this.entities = null;
this.name = name;
this.tag = tag;
this.entities = entities;
}

@Override
public BaseHeader clone() {
return new DataTableHeader(this.getTable(), this.getCell(), this.name, this.tag, this.entities);
}

@Override
Expand Down Expand Up @@ -50,11 +61,6 @@ public Iterable<String> entities() {
return this.entities;
}

@Override
public BaseHeader clone() {
return new DataTableHeader(this);
}

@Override
public boolean hasTag() {
return this.tag != null;
Expand Down Expand Up @@ -110,8 +116,4 @@ private List<String> sampleEntities() {
.map(i -> entityList.get(i))
.toList();
}

private String name;
private HeaderTag tag;
private List<String> entities;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ public MetaGroupHeader(final BaseTable table, final BaseCell cell) {
super(table, cell);
}

private MetaGroupHeader(final MetaGroupHeader parent) {
super(parent.getTable(), parent.getCell());
@Override
public BaseHeader clone() {
return new MetaGroupHeader(this.getTable(), this.getCell());
}

@Override
public String getName() {
return String.format(this.getTable().getSheet().getGroupValueFormat(), super.getName());
}

@Override
public BaseHeader clone() {
return new MetaGroupHeader(this);
}
}
Loading

0 comments on commit 95d473a

Please sign in to comment.