<commit_before>package ua.com.fielden.platform.web.layout.api.impl;

import java.util.Optional;

import org.apache.commons.lang.StringUtils;

import ua.com.fielden.platform.dom.DomElement;
import ua.com.fielden.platform.utils.Pair;
import ua.com.fielden.platform.web.layout.api.IFlexContainerLayout;
import ua.com.fielden.platform.web.layout.api.IFlexLayout;

/**
 * Represents layout cell.
 *
 * @author TG Team
 *
 */
public class CellConfig {

    private final Optional<IFlexContainerLayout> optionalContainer;
    private final Optional<String> layoutWidget;

    private Optional<IFlexLayout> optionalLayout = Optional.empty();

    /**
     * Creates empty cell.
     */
    public CellConfig() {
        this(null, null, (String) null);
    }

    /**
     * Creates container cell.
     *
     * @param container
     */
    public CellConfig(final IFlexContainerLayout container) {
        this(container, null, (String) null);
    }

    /**
     * Creates cell with layout container.
     *
     * @param layout
     */
    public CellConfig(final IFlexLayout layout) {
        this(null, layout, (String) null);
    }

    /**
     * Creates container cell with layout configuration.
     *
     * @param container
     * @param layout
     */
    public CellConfig(final IFlexContainerLayout container, final IFlexLayout layout) {
        this(container, layout, (String) null);
    }

    /**
     * Creates the html cell.
     *
     * @param dom
     */
    public CellConfig(final DomElement dom) {
        this(null, null, "html:" + dom.toString());
    }

    /**
     * Creates html cell with layout configuration.
     *
     * @param dom
     * @param layout
     */
    public CellConfig(final DomElement dom, final IFlexLayout layout) {
        this(null, layout, "html:" + dom.toString());
    }

    /**
     * Creates widget cell (e.g. subheader, select or skip cell)
     *
     * @param layoutWidget
     */
    public CellConfig(final String layoutWidget) {
        this(null, null, layoutWidget);
    }

    /**
     * Creates widget cell (e.g. subheader, select or skip cell) with specified layout.
     *
     * @param layout
     * @param layoutWidget
     */
    public CellConfig(final IFlexLayout layout, final String layoutWidget) {
        this(null, layout, layoutWidget);
    }

    /**
     * Set the layout configuration for this cell if it wasn't specified yet.
     *
     * @param layout
     */
    public void setLayoutIfNotPresent(final IFlexLayout layout) {
        if (!optionalLayout.isPresent()) {
            this.optionalLayout = Optional.ofNullable(layout);
        }
    }

    /**
     * Generates cell string for this configuration.
     *
     * @param vertical
     *            - flex-direction of the container in which this cell is placed. The direction calculation is based on layout configuration and default direction.
     * @param isVerticalDefault
     *            - the default flex-direction of the container in which this cell is placed.
     * @param gap
     * @return
     */
    public String render(final boolean vertical, final boolean isVerticalDefault, final int gap) {
        final boolean shouldIncludeGap = gap != 0;
        final Pair<String, String> tempStyle = new Pair<>(vertical ? "margin-bottom" : "margin-right", gap + "px");
        final String gapStyleString = shouldIncludeGap ? "\"" + tempStyle.getKey() + ":" + tempStyle.getValue() + "\"" : "";
        final String widgetString = layoutWidget.map(lw -> "\"" + lw + "\"").orElse("");
        final String layoutString = optionalLayout.map(layout -> layout.render(vertical, gap)).orElse(gapStyleString);
        final Optional<Boolean> optionalVertical = optionalLayout.flatMap(l -> l.isVerticalLayout());
        final String containerString = optionalContainer.map(c -> c.render(optionalVertical.orElse(!isVerticalDefault), !isVerticalDefault)).orElse("");

        String layout = widgetString;
        if (!StringUtils.isEmpty(layout) && !StringUtils.isEmpty(layoutString)) {
            layout += ", ";
        }
        layout += layoutString;
        if (!StringUtils.isEmpty(layout) && !StringUtils.isEmpty(containerString)) {
            layout += ", ";<commit_msg>#637 Simple refactoring to make the computation of 'gapStyleString' more comprehensible and more performant by avoiding instantiation of unnecessary objects.<commit_after>package ua.com.fielden.platform.web.layout.api.impl;

import java.util.Optional;

import org.apache.commons.lang.StringUtils;

import ua.com.fielden.platform.dom.DomElement;
import ua.com.fielden.platform.utils.Pair;
import ua.com.fielden.platform.web.layout.api.IFlexContainerLayout;
import ua.com.fielden.platform.web.layout.api.IFlexLayout;

/**
 * Represents layout cell.
 *
 * @author TG Team
 *
 */
public class CellConfig {

    private final Optional<IFlexContainerLayout> optionalContainer;
    private final Optional<String> layoutWidget;

    private Optional<IFlexLayout> optionalLayout = Optional.empty();

    /**
     * Creates empty cell.
     */
    public CellConfig() {
        this(null, null, (String) null);
    }

    /**
     * Creates container cell.
     *
     * @param container
     */
    public CellConfig(final IFlexContainerLayout container) {
        this(container, null, (String) null);
    }

    /**
     * Creates cell with layout container.
     *
     * @param layout
     */
    public CellConfig(final IFlexLayout layout) {
        this(null, layout, (String) null);
    }

    /**
     * Creates container cell with layout configuration.
     *
     * @param container
     * @param layout
     */
    public CellConfig(final IFlexContainerLayout container, final IFlexLayout layout) {
        this(container, layout, (String) null);
    }

    /**
     * Creates the html cell.
     *
     * @param dom
     */
    public CellConfig(final DomElement dom) {
        this(null, null, "html:" + dom.toString());
    }

    /**
     * Creates html cell with layout configuration.
     *
     * @param dom
     * @param layout
     */
    public CellConfig(final DomElement dom, final IFlexLayout layout) {
        this(null, layout, "html:" + dom.toString());
    }

    /**
     * Creates widget cell (e.g. subheader, select or skip cell)
     *
     * @param layoutWidget
     */
    public CellConfig(final String layoutWidget) {
        this(null, null, layoutWidget);
    }

    /**
     * Creates widget cell (e.g. subheader, select or skip cell) with specified layout.
     *
     * @param layout
     * @param layoutWidget
     */
    public CellConfig(final IFlexLayout layout, final String layoutWidget) {
        this(null, layout, layoutWidget);
    }

    /**
     * Set the layout configuration for this cell if it wasn't specified yet.
     *
     * @param layout
     */
    public void setLayoutIfNotPresent(final IFlexLayout layout) {
        if (!optionalLayout.isPresent()) {
            this.optionalLayout = Optional.ofNullable(layout);
        }
    }

    /**
     * Generates cell string for this configuration.
     *
     * @param vertical
     *            - flex-direction of the container in which this cell is placed. The direction calculation is based on layout configuration and default direction.
     * @param isVerticalDefault
     *            - the default flex-direction of the container in which this cell is placed.
     * @param gap
     * @return
     */
    public String render(final boolean vertical, final boolean isVerticalDefault, final int gap) {
        final String widgetString = layoutWidget.map(lw -> "\"" + lw + "\"").orElse("");
        final String gapStyleString = gap == 0 ? "" : "\"" + (vertical ? "margin-bottom" : "margin-right") + ":" + gap + "px\"";
        final String layoutString = optionalLayout.map(layout -> layout.render(vertical, gap)).orElse(gapStyleString);
        final Optional<Boolean> optionalVertical = optionalLayout.flatMap(l -> l.isVerticalLayout());
        final String containerString = optionalContainer.map(c -> c.render(optionalVertical.orElse(!isVerticalDefault), !isVerticalDefault)).orElse("");

        String layout = widgetString;
        if (!StringUtils.isEmpty(layout) && !StringUtils.isEmpty(layoutString)) {
            layout += ", ";
        }
        layout += layoutString;
        if (!StringUtils.isEmpty(layout) && !StringUtils.isEmpty(containerString)) {
            layout += ", ";
{'fraction_non_alphanumeric': 0.11332621336181321, 'fraction_numerical': 0.0005933309600094933, 'mean_word_length': 2.632758620689655, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2020372', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2261, 'n_words': 626}