Skip to content

Commit

Permalink
Performance improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Feb 23, 2025
1 parent 4946c64 commit a45e6fb
Show file tree
Hide file tree
Showing 16 changed files with 573 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,72 @@ public static CssProperty of(String name, String value) {
return new CssProperty(name, value);
}

/**
* Creates a new {@link CssProperty} instance with the specified name and value.
*
* @param name The name of the CSS property.
* @param value The value of the CSS property.
* @return A new {@link CssProperty} instance.
*/
public static CssProperty of(String name, Number value) {
return new CssProperty(name, String.valueOf(value));
}

/**
* Creates a new {@link CssProperty} instance with the specified name and value.
*
* @param name The name of the CSS property.
* @param value The value of the CSS property.
* @return A new {@link CssProperty} instance.
*/
public static CssProperty of(String name, int value) {
return new CssProperty(name, String.valueOf(value));
}

/**
* Creates a new {@link CssProperty} instance with the specified name and value.
*
* @param name The name of the CSS property.
* @param value The value of the CSS property.
* @return A new {@link CssProperty} instance.
*/
public static CssProperty of(String name, double value) {
return new CssProperty(name, String.valueOf(value));
}

/**
* Creates a new {@link CssProperty} instance with the specified name and value.
*
* @param name The name of the CSS property.
* @param value The value of the CSS property.
* @return A new {@link CssProperty} instance.
*/
public static CssProperty of(String name, short value) {
return new CssProperty(name, String.valueOf(value));
}

/**
* Creates a new {@link CssProperty} instance with the specified name and value.
*
* @param name The name of the CSS property.
* @param value The value of the CSS property.
* @return A new {@link CssProperty} instance.
*/
public static CssProperty of(String name, float value) {
return new CssProperty(name, String.valueOf(value));
}

/**
* Creates a new {@link CssProperty} instance with the specified name and value.
*
* @param name The name of the CSS property.
* @param value The value of the CSS property.
* @return A new {@link CssProperty} instance.
*/
public static CssProperty of(String name, boolean value) {
return new CssProperty(name, String.valueOf(value));
}

/**
* Constructs a {@link CssProperty} with a specified name and value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
*/
public interface DominoStyle<E extends Element, R> {

/**
* Sets a CSS property with the given name and string value.
*
* @param property {@link CssProperty}
* @return The updated style.
*/
R setCssProperty(CssProperty property);

/**
* Sets a CSS property with the given name and string value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public static <E extends Element, T extends IsElement<E>> Style<E> of(T isElemen
return new Style<>(isElement.element());
}

@Override
public Style<E> setCssProperty(CssProperty property) {
style.setProperty(property.getName(), property.getValue());
return this;
}

/**
* Sets a CSS property with the specified name and value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import static org.dominokit.domino.ui.utils.Domino.*;

import elemental2.dom.HTMLDivElement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.*;
import org.dominokit.domino.ui.utils.ComponentMeta;
import org.dominokit.domino.ui.utils.DominoCSSRule;
import org.dominokit.domino.ui.utils.DynamicStyleSheet;
Expand Down Expand Up @@ -107,11 +103,15 @@ public Optional<ColumnCssRule> getColumnCssRule(String key) {
* @return A collection of {@link ColumnCssRule} representing all the CSS rules.
*/
public Collection<ColumnCssRule> cssRules() {
return cssRules.keySet().stream()
.map(this::getColumnCssRule)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
List<ColumnCssRule> list = new ArrayList<>();
for (String s : cssRules.keySet()) {
Optional<ColumnCssRule> columnCssRule = getColumnCssRule(s);
if (columnCssRule.isPresent()) {
ColumnCssRule cssRule = columnCssRule.get();
list.add(cssRule);
}
}
return list;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.dominokit.domino.ui.datatable;

import static org.dominokit.domino.ui.utils.Domino.*;

import elemental2.dom.Node;

/** A functional interface for supplying header elements in a data table. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import static org.dominokit.domino.ui.utils.ElementsFactory.elements;

import elemental2.dom.DOMRect;
import elemental2.dom.HTMLElement;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.dominokit.domino.ui.datatable.plugins.DataTablePlugin;
import org.dominokit.domino.ui.datatable.plugins.column.ResizeColumnMeta;
import org.dominokit.domino.ui.elements.THeadElement;
Expand Down Expand Up @@ -85,24 +85,24 @@ public class TableConfig<T>
.addCss(dui_datatable_utility_elements)
.apply(
div -> {
getPlugins().stream()
.map(plugin -> plugin.getUtilityElements(dataTable, cellInfo))
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Collection::stream)
.forEach(
node -> {
String order =
Optional.ofNullable(
elements.elementOf(node).getAttribute("order"))
.orElse("0");
div.appendChild(
elements
.div()
.setCssProperty("order", Integer.parseInt(order))
.addCss(dui_datatable_utility_element)
.appendChild(node));
});
List<DataTablePlugin<T>> pluginsList = getPlugins();
for (DataTablePlugin<T> plugin : pluginsList) {
Optional<List<HTMLElement>> optionalElements =
plugin.getUtilityElements(dataTable, cellInfo);
if (optionalElements.isPresent()) {
List<HTMLElement> nodes = optionalElements.get();
for (HTMLElement node : nodes) {
String orderAttr = elements.elementOf(node).getAttribute("order");
String order = (orderAttr != null) ? orderAttr : "0";
div.appendChild(
elements
.div()
.setCssProperty("order", Integer.parseInt(order))
.addCss(dui_datatable_utility_element)
.appendChild(node));
}
}
}

cellInfo
.getColumnConfig()
Expand Down Expand Up @@ -139,7 +139,16 @@ public class TableConfig<T>
public void drawHeaders(DataTable<T> dataTable, THeadElement thead) {
this.dataTable = dataTable;

int maxDepth = columns.stream().mapToInt(ColumnConfig::getColumnsDepth).max().orElse(0);
boolean seen = false;
int best = 0;
for (ColumnConfig<T> column : columns) {
int columnsDepth = column.getColumnsDepth();
if (!seen || columnsDepth > best) {
seen = true;
best = columnsDepth;
}
}
int maxDepth = seen ? best : 0;

TableRowElement[] headers = new TableRowElement[maxDepth + 1];
for (int i = 0; i < headers.length; i++) {
Expand Down Expand Up @@ -372,7 +381,14 @@ public void setRowAppender(RowAppender<T> rowAppender) {
* @return A sorted list of {@link DataTablePlugin}.
*/
public List<DataTablePlugin<T>> getPlugins() {
return plugins.stream().sorted().collect(Collectors.toList());
List<DataTablePlugin<T>> sortedPlugins = new ArrayList<>();
// Add all plugins using a simple loop
for (DataTablePlugin<T> plugin : plugins) {
sortedPlugins.add(plugin);
}
// Sort the list using Collections.sort(), which sorts according to natural ordering
Collections.sort(sortedPlugins);
return sortedPlugins;
}

/**
Expand All @@ -399,7 +415,15 @@ void onAfterHeaders(DataTable<T> dataTable) {
* @return A list of {@link ColumnConfig} representing the leaf columns.
*/
public List<ColumnConfig<T>> getColumns() {
return columns.stream().flatMap(col -> col.leafColumns().stream()).collect(Collectors.toList());
List<ColumnConfig<T>> allColumns = new ArrayList<>();
for (ColumnConfig<T> col : columns) {
// Retrieve the leaf columns from the current column
List<ColumnConfig<T>> leafColumns = col.leafColumns();
for (ColumnConfig<T> leaf : leafColumns) {
allColumns.add(leaf);
}
}
return allColumns;
}

/**
Expand All @@ -408,9 +432,14 @@ public List<ColumnConfig<T>> getColumns() {
* @return A list of {@link ColumnConfig} representing all columns, flattened.
*/
public List<ColumnConfig<T>> getFlattenColumns() {
return columns.stream()
.flatMap(col -> col.flattenColumns().stream())
.collect(Collectors.toList());
List<ColumnConfig<T>> flattenColumns = new ArrayList<>();
for (ColumnConfig<T> col : columns) {
List<ColumnConfig<T>> colFlattenColumns = col.flattenColumns();
for (ColumnConfig<T> flattened : colFlattenColumns) {
flattenColumns.add(flattened);
}
}
return flattenColumns;
}

/**
Expand All @@ -419,7 +448,14 @@ public List<ColumnConfig<T>> getFlattenColumns() {
* @return A list of {@link ColumnConfig} representing all columns, flattened.
*/
public List<ColumnConfig<T>> getLeafColumns() {
return columns.stream().flatMap(col -> col.leafColumns().stream()).collect(Collectors.toList());
List<ColumnConfig<T>> leafColumnsList = new ArrayList<>();
for (ColumnConfig<T> col : columns) {
List<ColumnConfig<T>> childLeafColumns = col.leafColumns();
for (ColumnConfig<T> leaf : childLeafColumns) {
leafColumnsList.add(leaf);
}
}
return leafColumnsList;
}

/**
Expand All @@ -437,7 +473,13 @@ public List<ColumnConfig<T>> getColumnsGrouped() {
* @return A list of {@link ColumnConfig} representing visible columns.
*/
public List<ColumnConfig<T>> getVisibleColumns() {
return columns.stream().filter(column -> !column.isHidden()).collect(Collectors.toList());
List<ColumnConfig<T>> list = new ArrayList<>();
for (ColumnConfig<T> column : columns) {
if (!column.isHidden()) {
list.add(column);
}
}
return list;
}

/**
Expand All @@ -448,10 +490,13 @@ public List<ColumnConfig<T>> getVisibleColumns() {
* @throws ColumnNofFoundException If no column is found with the specified name.
*/
public ColumnConfig<T> getColumnByName(String name) {
Optional<ColumnConfig<T>> first =
getFlattenColumns().stream()
.filter(columnConfig -> columnConfig.getName().equals(name))
.findFirst();
Optional<ColumnConfig<T>> first = Optional.empty();
for (ColumnConfig<T> columnConfig : getFlattenColumns()) {
if (columnConfig.getName().equals(name)) {
first = Optional.of(columnConfig);
break;
}
}
if (first.isPresent()) {
return first.get();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.dominokit.domino.ui.events;

import static org.dominokit.domino.ui.utils.Domino.*;

import elemental2.dom.CustomEvent;
import elemental2.dom.CustomEventInit;
import jsinterop.base.Js;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ public abstract class AbstractFormElement<T extends AbstractFormElement<T, V>, V
protected final LazyChild<SpanElement> helperTextElement;
protected Function<String, SpanElement> errorElementSupplier;

protected Set<Validator<T>> validators = new LinkedHashSet<>();
private Set<Validator<T>> validators;
protected AutoValidator autoValidator;
protected final List<String> errors = new ArrayList<>();
protected String requiredErrorMessage;

protected final FormsLabels labels = DominoUIConfig.CONFIG.getDominoUILabels();
protected final RequiredValidator<T> requiredValidator;
protected Set<ChangeListener<? super V>> changeListeners = new LinkedHashSet<>();
protected Set<ClearListener<? super V>> clearListeners = new LinkedHashSet<>();
private Set<ChangeListener<? super V>> changeListeners;
private Set<ClearListener<? super V>> clearListeners;
private boolean autoValidate = false;
private boolean required = false;
private boolean changeListenersPaused = false;
Expand Down Expand Up @@ -434,6 +434,9 @@ public ValidationResult validate(T formElement) {
*/
@Override
public Set<Validator<T>> getValidators() {
if (isNull(validators)) {
this.validators = new LinkedHashSet<>();
}
return validators;
}

Expand Down Expand Up @@ -666,6 +669,9 @@ public T resumeChangeListeners() {
*/
@Override
public Set<ChangeListener<? super V>> getChangeListeners() {
if (isNull(changeListeners)) {
this.changeListeners = new LinkedHashSet<>();
}
return changeListeners;
}

Expand Down Expand Up @@ -732,6 +738,9 @@ public T togglePauseClearListeners(boolean toggle) {
*/
@Override
public Set<ClearListener<? super V>> getClearListeners() {
if (isNull(clearListeners)) {
this.clearListeners = new LinkedHashSet<>();
}
return clearListeners;
}

Expand Down
Loading

0 comments on commit a45e6fb

Please sign in to comment.