RegexMatchObject.java 2.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
package pwc.taxtech.atms.dto;

import org.apache.commons.lang3.StringUtils;
import pwc.taxtech.atms.constant.enums.RegexMatchType;

import java.util.AbstractMap;

public class RegexMatchObject {

    /**
     * GET(int columnIndex, int rowIndex);
     */
    private String formularTemplate = "GET({0},{1})";
    private String keyValueTemplate = "KeyValue(\"{0}\")";
    private int index;
    private int length;
    private RegexMatchType matchType;
    private AbstractMap.SimpleEntry<String, Integer> parameters;
    private String expression;


    private int fromNumberSystem26(String s) {
        if (StringUtils.isEmpty(s)) {
            return 0;
        }

        int n = 0;
        for (int i = s.length() - 1, j = 1; i >= 0; i--, j *= 26) {
            char c = Character.toUpperCase(s.charAt(i));
            if (c < 'A' || c > 'Z') {
                return 0;
            }
            n += ((int) c - 64) * j;
        }
        return n;
    }

    public String getFormularTemplate() {
        return formularTemplate;
    }

    public void setFormularTemplate(String formularTemplate) {
        this.formularTemplate = formularTemplate;
    }

    public String getKeyValueTemplate() {
        return keyValueTemplate;
    }

    public void setKeyValueTemplate(String keyValueTemplate) {
        this.keyValueTemplate = keyValueTemplate;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public RegexMatchType getMatchType() {
        return matchType;
    }

    public void setMatchType(RegexMatchType matchType) {
        this.matchType = matchType;
    }

    public AbstractMap.SimpleEntry<String, Integer> getParameters() {
        return parameters;
    }

    public void setParameters(AbstractMap.SimpleEntry<String, Integer> parameters) {
        this.parameters = parameters;
    }

    public String getExpression() {
        if (this.matchType.equals(RegexMatchType.CellNumber.getCode())) {
            return String.format(formularTemplate, fromNumberSystem26(this.parameters.getKey()) - 1, parameters.getValue() - 1);
        } else if (this.matchType.equals(RegexMatchType.KeyValue.getCode())) {
            return String.format(keyValueTemplate, this.parameters.getKey().substring(1));
        } else if (this.matchType.equals(RegexMatchType.Equal.getCode())) {
            return "==";
        }
        return expression;
    }
}