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; } }