View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.prometheus.service.sheet;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.oceanus.convert.OceanusDataConverter;
21  import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
22  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusDecimal;
23  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
24  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusPrice;
25  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
26  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRatio;
27  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusUnits;
28  import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheet.PrometheusSheetSheetCtl;
29  
30  /**
31   * Class representing a cell within a sheet or a view.
32   */
33  public abstract class PrometheusSheetCell {
34      /**
35       * The underlying sheet.
36       */
37      private final PrometheusSheetSheetCtl theSheet;
38  
39      /**
40       * The position of the cell.
41       */
42      private final PrometheusSheetCellPosition thePosition;
43  
44      /**
45       * Is the cell readOnly?
46       */
47      private final boolean isReadOnly;
48  
49      /**
50       * Constructor.
51       *
52       * @param pSheet    the sheet for the cell
53       * @param pRowIndex the row index
54       * @param pColIndex the column index
55       * @param pReadOnly is the cell readOnly?
56       */
57      protected PrometheusSheetCell(final PrometheusSheetSheetCtl pSheet,
58                                    final int pRowIndex,
59                                    final int pColIndex,
60                                    final boolean pReadOnly) {
61          /* Store parameters */
62          theSheet = pSheet;
63          thePosition = new PrometheusSheetCellPosition(pColIndex, pRowIndex);
64          isReadOnly = pReadOnly;
65      }
66  
67      /**
68       * Obtain the underlying sheet.
69       *
70       * @return the underlying sheet
71       */
72      public PrometheusSheetSheetCtl getSheet() {
73          return theSheet;
74      }
75  
76      /**
77       * Obtain the cell position.
78       *
79       * @return position
80       */
81      public PrometheusSheetCellPosition getPosition() {
82          return thePosition;
83      }
84  
85      /**
86       * Is the cell readOnly?
87       *
88       * @return true/false
89       */
90      public boolean isReadOnly() {
91          return isReadOnly;
92      }
93  
94      /**
95       * Obtain the cell index.
96       *
97       * @return the index
98       */
99      public int getCellIndex() {
100         return thePosition.getColumnIndex();
101     }
102 
103     /**
104      * Obtain boolean value of the cell.
105      *
106      * @return the boolean value
107      */
108     public abstract Boolean getBoolean();
109 
110     /**
111      * Obtain date value of the cell.
112      *
113      * @return the date value
114      * @throws OceanusException on error
115      */
116     public abstract OceanusDate getDate() throws OceanusException;
117 
118     /**
119      * Obtain integer value of the cell.
120      *
121      * @return the integer value
122      * @throws OceanusException on error
123      */
124     public abstract Integer getInteger() throws OceanusException;
125 
126     /**
127      * Obtain long value of the cell.
128      *
129      * @return the long value
130      * @throws OceanusException on error
131      */
132     public abstract Long getLong() throws OceanusException;
133 
134     /**
135      * Obtain long value of the cell.
136      *
137      * @return the long value
138      * @throws OceanusException on error
139      */
140     public Long getLongFromString() throws OceanusException {
141         try {
142             final String myValue = getString();
143             return Long.parseLong(myValue);
144         } catch (NumberFormatException e) {
145             throw new PrometheusSheetException("Failed to parse Long", e);
146         }
147     }
148 
149     /**
150      * Obtain money value of the cell.
151      *
152      * @return the money value
153      * @throws OceanusException on error
154      */
155     public abstract OceanusMoney getMoney() throws OceanusException;
156 
157     /**
158      * Obtain price value of the cell.
159      *
160      * @return the price value
161      * @throws OceanusException on error
162      */
163     public abstract OceanusPrice getPrice() throws OceanusException;
164 
165     /**
166      * Obtain rate value of the cell.
167      *
168      * @return the rate value
169      * @throws OceanusException on error
170      */
171     public abstract OceanusRate getRate() throws OceanusException;
172 
173     /**
174      * Obtain units value of the cell.
175      *
176      * @return the units value
177      * @throws OceanusException on error
178      */
179     public abstract OceanusUnits getUnits() throws OceanusException;
180 
181     /**
182      * Obtain ratio value of the cell.
183      *
184      * @return the ratio value
185      * @throws OceanusException on error
186      */
187     public abstract OceanusRatio getRatio() throws OceanusException;
188 
189     /**
190      * Obtain string value of the cell.
191      *
192      * @return the string value
193      */
194     public abstract String getString();
195 
196     /**
197      * Obtain byte array value of the cell.
198      *
199      * @return the byte array value
200      */
201     public byte[] getBytes() {
202         final String myValue = getString();
203         return myValue == null
204                 ? null
205                 : OceanusDataConverter.base64ToByteArray(myValue);
206     }
207 
208     /**
209      * Obtain char array value of the cell.
210      *
211      * @return the char array value
212      * @throws OceanusException on error
213      */
214     public char[] getCharArray() throws OceanusException {
215         final byte[] myValue = getBytes();
216         return myValue == null
217                 ? null
218                 : OceanusDataConverter.bytesToCharArray(myValue);
219     }
220 
221     /**
222      * Set null value for the cell.
223      *
224      * @throws OceanusException on error
225      */
226     public void setNull() throws OceanusException {
227         /* Ignore readOnly */
228         if (!isReadOnly) {
229             setNullValue();
230         }
231     }
232 
233     /**
234      * Set null value for the cell.
235      *
236      * @throws OceanusException on error
237      */
238     protected abstract void setNullValue() throws OceanusException;
239 
240     /**
241      * Set boolean value of the cell.
242      *
243      * @param pValue the integer value
244      * @throws OceanusException on error
245      */
246     public void setBoolean(final Boolean pValue) throws OceanusException {
247         /* Ignore readOnly */
248         if (isReadOnly) {
249             return;
250         }
251 
252         /* Handle null values */
253         if (pValue == null) {
254             setNullValue();
255         } else {
256             setBooleanValue(pValue);
257         }
258     }
259 
260     /**
261      * Set non-null boolean value of the cell.
262      *
263      * @param pValue the integer value
264      * @throws OceanusException on error
265      */
266     protected abstract void setBooleanValue(Boolean pValue) throws OceanusException;
267 
268     /**
269      * Set date value of the cell.
270      *
271      * @param pValue the date value
272      * @throws OceanusException on error
273      */
274     public void setDate(final OceanusDate pValue) throws OceanusException {
275         /* Ignore readOnly */
276         if (isReadOnly) {
277             return;
278         }
279 
280         /* Handle null values */
281         if (pValue == null) {
282             setNullValue();
283         } else {
284             setDateValue(pValue);
285         }
286     }
287 
288     /**
289      * Set non-null date value of the cell.
290      *
291      * @param pValue the integer value
292      * @throws OceanusException on error
293      */
294     protected abstract void setDateValue(OceanusDate pValue) throws OceanusException;
295 
296     /**
297      * Set integer value of the cell.
298      *
299      * @param pValue the integer value
300      * @throws OceanusException on error
301      */
302     public void setInteger(final Integer pValue) throws OceanusException {
303         /* Ignore readOnly */
304         if (isReadOnly) {
305             return;
306         }
307 
308         /* Handle null values */
309         if (pValue == null) {
310             setNullValue();
311         } else {
312             setIntegerValue(pValue);
313         }
314     }
315 
316     /**
317      * Set non-null integer value of the cell.
318      *
319      * @param pValue the integer value
320      * @throws OceanusException on error
321      */
322     protected abstract void setIntegerValue(Integer pValue) throws OceanusException;
323 
324     /**
325      * Set long value of the cell.
326      *
327      * @param pValue the integer value
328      * @throws OceanusException on error
329      */
330     public void setLong(final Long pValue) throws OceanusException {
331         /* Ignore readOnly */
332         if (isReadOnly) {
333             return;
334         }
335 
336         /* Handle null values */
337         if (pValue == null) {
338             setNullValue();
339         } else {
340             setLongValue(pValue);
341         }
342     }
343 
344     /**
345      * Set long value of the cell.
346      *
347      * @param pValue the integer value
348      * @throws OceanusException on error
349      */
350     public void setLongAsString(final Long pValue) throws OceanusException {
351         /* Ignore readOnly */
352         if (isReadOnly) {
353             return;
354         }
355 
356         /* Handle null values */
357         if (pValue == null) {
358             setNullValue();
359         } else {
360             setStringValue(Long.toString(pValue));
361         }
362     }
363 
364     /**
365      * Set non-null long value of the cell.
366      *
367      * @param pValue the long value
368      * @throws OceanusException on error
369      */
370     protected abstract void setLongValue(Long pValue) throws OceanusException;
371 
372     /**
373      * Set string value of the cell.
374      *
375      * @param pValue the string value
376      * @throws OceanusException on error
377      */
378     public void setString(final String pValue) throws OceanusException {
379         /* Ignore readOnly */
380         if (isReadOnly) {
381             return;
382         }
383 
384         /* Handle null values */
385         if (pValue == null) {
386             setNullValue();
387         } else {
388             setStringValue(pValue);
389         }
390     }
391 
392     /**
393      * Set non-null string value of the cell.
394      *
395      * @param pValue the string value
396      * @throws OceanusException on error
397      */
398     protected abstract void setStringValue(String pValue) throws OceanusException;
399 
400     /**
401      * Set decimal value of the cell.
402      *
403      * @param pValue the decimal value
404      * @throws OceanusException on error
405      */
406     public void setDecimal(final OceanusDecimal pValue) throws OceanusException {
407         /* Ignore readOnly */
408         if (isReadOnly) {
409             return;
410         }
411 
412         /* Handle null values */
413         if (pValue == null) {
414             setNullValue();
415         } else {
416             setDecimalValue(pValue);
417         }
418     }
419 
420     /**
421      * Set non-null decimal value of the cell.
422      *
423      * @param pValue the decimal value
424      * @throws OceanusException on error
425      */
426     protected abstract void setDecimalValue(OceanusDecimal pValue) throws OceanusException;
427 
428     /**
429      * Set monetary value of the cell.
430      *
431      * @param pValue the monetary value
432      * @throws OceanusException on error
433      */
434     public void setMonetary(final OceanusMoney pValue) throws OceanusException {
435         /* Ignore readOnly */
436         if (isReadOnly) {
437             return;
438         }
439 
440         /* Handle null values */
441         if (pValue == null) {
442             setNullValue();
443         } else {
444             setMonetaryValue(pValue);
445         }
446     }
447 
448     /**
449      * Set non-null monetary value of the cell.
450      *
451      * @param pValue the monetary value
452      * @throws OceanusException on error
453      */
454     protected abstract void setMonetaryValue(OceanusMoney pValue) throws OceanusException;
455 
456     /**
457      * Set header value of the cell.
458      *
459      * @param pValue the string value
460      * @throws OceanusException on error
461      */
462     public void setHeader(final String pValue) throws OceanusException {
463         /* Handle null values */
464         if (pValue == null) {
465             setNullValue();
466         } else {
467             /* Set value */
468             setHeaderValue(pValue);
469         }
470     }
471 
472     /**
473      * Set non-null header value of the cell.
474      *
475      * @param pValue the header value
476      * @throws OceanusException on error
477      */
478     protected abstract void setHeaderValue(String pValue) throws OceanusException;
479 
480     /**
481      * Set byte array value of the cell.
482      *
483      * @param pValue the byte array value
484      * @throws OceanusException on error
485      */
486     public void setBytes(final byte[] pValue) throws OceanusException {
487         /* Ignore readOnly */
488         if (isReadOnly) {
489             return;
490         }
491 
492         /* Handle null values */
493         if (pValue == null) {
494             setNullValue();
495         } else {
496             setStringValue(OceanusDataConverter.byteArrayToBase64(pValue));
497         }
498     }
499 
500     /**
501      * Set char array value of the cell.
502      *
503      * @param pValue the byte array value
504      * @throws OceanusException on error
505      */
506     public void setCharArray(final char[] pValue) throws OceanusException {
507         /* Ignore readOnly */
508         if (isReadOnly) {
509             return;
510         }
511 
512         /* Handle null values */
513         if (pValue == null) {
514             setNullValue();
515         } else {
516             final byte[] myBytes = OceanusDataConverter.charsToByteArray(pValue);
517             setStringValue(OceanusDataConverter.byteArrayToBase64(myBytes));
518         }
519     }
520 
521     /**
522      * Obtain the required CellStyle.
523      *
524      * @param pValue the value
525      * @return the required CellStyle
526      */
527     protected static PrometheusSheetCellStyleType getCellStyle(final Object pValue) {
528         if (pValue instanceof OceanusPrice) {
529             return PrometheusSheetCellStyleType.PRICE;
530         }
531         if (pValue instanceof OceanusMoney) {
532             return PrometheusSheetCellStyleType.MONEY;
533         }
534         if (pValue instanceof OceanusUnits) {
535             return PrometheusSheetCellStyleType.UNITS;
536         }
537         if (pValue instanceof OceanusRate) {
538             return PrometheusSheetCellStyleType.RATE;
539         }
540         if (pValue instanceof OceanusRatio) {
541             return PrometheusSheetCellStyleType.RATIO;
542         }
543         if (pValue instanceof Boolean) {
544             return PrometheusSheetCellStyleType.BOOLEAN;
545         }
546         if (pValue instanceof Number) {
547             return PrometheusSheetCellStyleType.INTEGER;
548         }
549         if (pValue instanceof OceanusDate) {
550             return PrometheusSheetCellStyleType.DATE;
551         }
552         if (pValue instanceof String) {
553             return PrometheusSheetCellStyleType.STRING;
554         }
555         return null;
556     }
557 }