package jp.satomichan.nucalgen; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFDataFormat; public class CellStylePool { private Workbook workbook; CellStylePool(Workbook workbook){ this.workbook = workbook; } private Map cellStyleMap = new HashMap(); CellStyle getCellStyle(String format) { return this.getCellStyle(format, true); } CellStyle getCellStyle(String format, Boolean locked) { CompKey key = new CompKey(format, locked); if(this.cellStyleMap.containsKey(key)) { return this.cellStyleMap.get(key); }else { CellStyle cs = this.workbook.createCellStyle(); XSSFDataFormat xssfFormat = (XSSFDataFormat) this.workbook.createDataFormat(); cs.setDataFormat(xssfFormat.getFormat(format)); cs.setLocked(locked); this.cellStyleMap.put(key, cs); return this.cellStyleMap.get(key); } } class CompKey { private String format; private Boolean locked; CompKey(String format, Boolean locked){ this.setFormat(format); this.setLocked(locked); } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public Boolean getLocked() { return locked; } public void setLocked(Boolean locked) { this.locked = locked; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + getEnclosingInstance().hashCode(); result = prime * result + ((format == null) ? 0 : format.hashCode()); result = prime * result + ((locked == null) ? 0 : locked.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; CompKey other = (CompKey) obj; if (!getEnclosingInstance().equals(other.getEnclosingInstance())) return false; if (format == null) { if (other.format != null) return false; } else if (!format.equals(other.format)) return false; if (locked == null) { if (other.locked != null) return false; } else if (!locked.equals(other.locked)) return false; return true; } private CellStylePool getEnclosingInstance() { return CellStylePool.this; } } }