All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.swiftboot.sheet.meta.Expression Maven / Gradle / Ivy

There is a newer version: 2.4.7
Show newest version
package org.swiftboot.sheet.meta;

import org.apache.commons.lang3.StringUtils;

import static org.apache.commons.lang3.StringUtils.*;

/**
 * Expression like Excel's cell expression.
 * More specific parsing will be in {@link Translator}
 *
 * @author swiftech
 * @see Translator
 */
public class Expression {

    private String sheetName = EMPTY;
    private String cellsExp;

    public Expression(String expression) {
        if (StringUtils.isBlank(expression)) {
            throw new RuntimeException("Expression cannot be empty");
        }
        String[] split = splitByWholeSeparator(expression, "'.");
        if (split != null) {
            if (split.length == 2) {
                this.sheetName = stripStart(split[0], "$'");
                this.cellsExp = split[1];
            }
            else {
                this.cellsExp = expression;
            }
        }
    }

    public String getSheetName() {
        return sheetName;
    }

    public boolean isRange() {
        return contains(cellsExp, ":");
    }

    public boolean isHorizontalRange() {
        return contains(cellsExp, "-");
    }

    public boolean isVerticalRange() {
        return contains(cellsExp, "|");
    }

    public boolean isSinglePosition() {
        return !isRange() && !isHorizontalRange() && !isVerticalRange();
    }

    public String[] splitAsFreeRange() {
        String[] split = split(this.cellsExp, ':');
        if (split == null || split.length != 2) {
            throw new RuntimeException(String.format("Illegal expression: %s", this.cellsExp));
        }
        return split;
    }

    public String[] splitAsHorizontalRange() {
        String[] split = split(this.cellsExp, '-');
        if (split == null || split.length != 2 || !StringUtils.isNumeric(split[1])) {
            throw new RuntimeException(String.format("Illegal expression: %s", this.cellsExp));
        }
        return split;
    }

    public String[] splitAsVerticalRange() {
        String[] split = split(this.cellsExp, '|');
        if (split == null || split.length != 2 || !StringUtils.isNumeric(split[1])) {
            throw new RuntimeException(String.format("Illegal expression: %s", this.cellsExp));
        }
        return split;
    }

    public String getCellsExp() {
        return cellsExp;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy