shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.walker.openocr.table;
 
import com.walker.openocr.MultipleLine;
import com.walker.openocr.TextBlock;
 
/**
 * 表格中,一个单元格定义。
 * @author 时克英
 * @date 2022-08-31
 */
public class CellObject implements Comparable<CellObject> {
 
    // 单元格编号,如果是用户不关注的,则随机生成
    private String id;
 
    // 行号,对应配置中用户定义的:CellConfigItem#orderNum
    private int rowNum = 0;
 
    // 在同一行中,该单元格所处列,数值越大越靠后(排序使用)
    private int orderColumn = 0;
 
    // 单元格对应的原始文本块数据
    private TextBlock source = null;
 
    private CellConfigItem cellConfigItem = null;
 
    // 单元格解析的最终值
    private String value = null;
 
    public String getValue() {
        return value;
    }
 
    public void setValue(String value) {
        this.value = value;
    }
 
    public CellConfigItem getCellConfigItem() {
        return cellConfigItem;
    }
 
    public void setCellConfigItem(CellConfigItem cellConfigItem) {
        this.cellConfigItem = cellConfigItem;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public int getRowNum() {
        return rowNum;
    }
 
    public void setRowNum(int rowNum) {
        this.rowNum = rowNum;
    }
 
    public int getOrderColumn() {
        return orderColumn;
    }
 
    public void setOrderColumn(int orderColumn) {
        this.orderColumn = orderColumn;
    }
 
    public TextBlock getSource() {
        return source;
    }
 
    public void setSource(TextBlock source) {
        this.source = source;
    }
 
    /**
     * 判断该单元格是否匹配到表格配置项中,如果匹配了说明是用户需要的关键单元格(标题)
     * @return
     * @date 2022-09-01
     */
    public boolean isConfigurable(){
        return this.cellConfigItem != null;
    }
 
    /**
     * 返回:是不是一个单元格,有些数据不在格子里,需要按照规则拆分标题和值。如:name=保险单号:PDFC2261010420000000068
     * @return
     */
    public boolean isConfigNoneCell(){
        this.checkConfigItem();
        return this.cellConfigItem.isNoneCell();
    }
 
    /**
     * 返回:是否完整一行单元格
     * @return
     */
    public boolean isConfigFullRow(){
        this.checkConfigItem();
        return this.cellConfigItem.isFullRow();
    }
 
    public boolean isConfigTwoLine(){
        this.checkConfigItem();
        return this.cellConfigItem.getMultipleLine() == MultipleLine.TwoLine;
    }
    public boolean isConfigMoreLine(){
        this.checkConfigItem();
        return this.cellConfigItem.getMultipleLine() == MultipleLine.More;
    }
    public boolean isConfigSingleLine(){
        this.checkConfigItem();
        return this.cellConfigItem.getMultipleLine() == MultipleLine.None;
    }
 
    /**
     * 返回配置的该单元格值,最小字符串数量。
     * @return
     */
    public int getMinValueSize(){
        this.checkConfigItem();
        return cellConfigItem.getMinValueSize();
    }
 
    private void checkConfigItem(){
        if(this.cellConfigItem == null){
            throw new IllegalArgumentException("单元格不是可配置元素:" + this.id);
        }
    }
 
    @Override
    public int hashCode(){
        return this.id.hashCode();
    }
 
    @Override
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(obj instanceof CellObject){
            CellObject cellObject = (CellObject) obj;
            if(cellObject.id.equals(this.id)){
                return true;
            }
        }
        return false;
    }
 
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder("[id=");
        sb.append(this.id);
        sb.append(", row=").append(this.rowNum).append(", column=").append(this.orderColumn);
        if(this.source != null){
            sb.append(", name=").append(this.source.getText());
        }
        if(this.value != null){
            sb.append(", value=").append(this.value);
        }
        sb.append("]");
        return sb.toString();
    }
 
//    @Override
//    public int compare(CellObject o1, CellObject o2) {
//        return o1.orderColumn - o2.orderColumn;
//    }
 
    @Override
    public int compareTo(CellObject o) {
        return this.orderColumn - o.orderColumn;
    }
}