name:
src/main/java/io/protobase/f7/models/A1Key.java
-rw-r--r--
2019
1package io.protobase.f7.models;
2
3import com.google.common.base.MoreObjects;
4import io.protobase.f7.errors.ParseException;
5import io.protobase.f7.utils.AlphaUtils;
6
7import java.util.regex.Matcher;
8import java.util.regex.Pattern;
9
10/**
11 * A1 key is mostly a helper-class to name cells when testing.
12 */
13public class A1Key extends BaseObject {
14 private static final Pattern CRUDE_A1_PATTERN = Pattern.compile("^(\\D+)(\\d+)$");
15 private String column;
16 private Integer row;
17
18 public A1Key(String column, Integer row) {
19 this.column = column;
20 this.row = row;
21 }
22
23 /**
24 * Create key from A1 string.
25 *
26 * @param a1Key - key
27 * @return A1Key
28 */
29 public static A1Key fromString(String a1Key) {
30 Matcher matcher = CRUDE_A1_PATTERN.matcher(a1Key);
31 if (matcher.matches()) {
32 return new A1Key(matcher.group(1), Integer.parseInt(matcher.group(2)));
33 }
34 throw new ParseException("Could not parse cell key.");
35 }
36
37 /**
38 * Get the string representation of column.
39 *
40 * @return string.
41 */
42 public String getColumn() {
43 return column;
44 }
45
46 /**
47 * Get the 1-indexed row.
48 *
49 * @return int greater than or equal to 1.
50 */
51 public Integer getRow() {
52 return row;
53 }
54
55 /**
56 * Get the 0-indexed column.
57 *
58 * @return int greater than or equal to 0.
59 */
60 public Integer getColumnIndex() {
61 return AlphaUtils.columnToInt(column);
62 }
63
64 /**
65 * Get the 0-indexed row.
66 *
67 * @return int greater than or equal to 0.
68 */
69 public Integer getRowIndex() {
70 return row - 1;
71 }
72
73 /**
74 * Convert to zero-indexed ColumnRowKey.
75 *
76 * @return key
77 */
78 public ColumnRowKey toColumnRowKey() {
79 return new ColumnRowKey(getColumnIndex(), getRowIndex());
80 }
81
82 @Override
83 public String toString() {
84 return MoreObjects.toStringHelper(this)
85 .add("column", column)
86 .add("row", row)
87 .toString();
88 }
89
90 @Override
91 public Object[] significantAttributes() {
92 return new Object[]{
93 column,
94 row
95 };
96 }
97}