2020-11-29
[nucalgen] / src / main / java / jp / satomichan / nucalgen / CellStylePool.java
1 package jp.satomichan.nucalgen;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.apache.poi.ss.usermodel.CellStyle;
7 import org.apache.poi.ss.usermodel.Workbook;
8 import org.apache.poi.xssf.usermodel.XSSFDataFormat;
9
10 public class CellStylePool {
11
12         private Workbook workbook;
13
14         CellStylePool(Workbook workbook){
15                 this.workbook = workbook;
16         }
17
18
19         private Map<CompKey, CellStyle> cellStyleMap = new HashMap<CompKey, CellStyle>();
20
21
22         CellStyle getCellStyle(String format) {
23                 return this.getCellStyle(format, true);
24         }
25
26
27         CellStyle getCellStyle(String format, Boolean locked) {
28                 CompKey key = new CompKey(format, locked);
29                 if(this.cellStyleMap.containsKey(key)) {
30                         return this.cellStyleMap.get(key);
31                 }else {
32                         CellStyle cs = this.workbook.createCellStyle();
33                         XSSFDataFormat xssfFormat = (XSSFDataFormat) this.workbook.createDataFormat();
34                         cs.setDataFormat(xssfFormat.getFormat(format));
35                         cs.setLocked(locked);
36                         this.cellStyleMap.put(key, cs);
37
38                         return this.cellStyleMap.get(key);
39                 }
40         }
41
42
43         class CompKey {
44                 private String format;
45                 private Boolean locked;
46
47                 CompKey(String format, Boolean locked){
48                         this.setFormat(format);
49                         this.setLocked(locked);
50                 }
51
52                 public String getFormat() {
53                         return format;
54                 }
55                 public void setFormat(String format) {
56                         this.format = format;
57                 }
58                 public Boolean getLocked() {
59                         return locked;
60                 }
61                 public void setLocked(Boolean locked) {
62                         this.locked = locked;
63                 }
64                 @Override
65                 public int hashCode() {
66                         final int prime = 31;
67                         int result = 1;
68                         result = prime * result + getEnclosingInstance().hashCode();
69                         result = prime * result + ((format == null) ? 0 : format.hashCode());
70                         result = prime * result + ((locked == null) ? 0 : locked.hashCode());
71                         return result;
72                 }
73                 @Override
74                 public boolean equals(Object obj) {
75                         if (this == obj)
76                                 return true;
77                         if (obj == null)
78                                 return false;
79                         if (getClass() != obj.getClass())
80                                 return false;
81                         CompKey other = (CompKey) obj;
82                         if (!getEnclosingInstance().equals(other.getEnclosingInstance()))
83                                 return false;
84                         if (format == null) {
85                                 if (other.format != null)
86                                         return false;
87                         } else if (!format.equals(other.format))
88                                 return false;
89                         if (locked == null) {
90                                 if (other.locked != null)
91                                         return false;
92                         } else if (!locked.equals(other.locked))
93                                 return false;
94                         return true;
95                 }
96                 private CellStylePool getEnclosingInstance() {
97                         return CellStylePool.this;
98                 }
99
100
101         }
102
103
104 }