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 java.util.ListIterator;
20
21 /**
22 * Class representing a row within a sheet or a view.
23 */
24 public abstract class PrometheusSheetRow {
25 /**
26 * The underlying sheet.
27 */
28 private final PrometheusSheetSheet theSheet;
29
30 /**
31 * The underlying view.
32 */
33 private final PrometheusSheetView theView;
34
35 /**
36 * The index of this row.
37 */
38 private final int theRowIndex;
39
40 /**
41 * Is the row readOnly?
42 */
43 private final boolean isReadOnly;
44
45 /**
46 * Constructor.
47 *
48 * @param pSheet the sheet for the row
49 * @param pRowIndex the Row index
50 * @param pReadOnly is the row readOnly?
51 */
52 protected PrometheusSheetRow(final PrometheusSheetSheet pSheet,
53 final int pRowIndex,
54 final boolean pReadOnly) {
55 /* Store parameters */
56 theSheet = pSheet;
57 theView = null;
58 theRowIndex = pRowIndex;
59 isReadOnly = pReadOnly;
60 }
61
62 /**
63 * Constructor.
64 *
65 * @param pView the view for the row
66 * @param pRowIndex the Row index
67 */
68 protected PrometheusSheetRow(final PrometheusSheetView pView,
69 final int pRowIndex) {
70 /* Store parameters */
71 theSheet = pView.getSheet();
72 theView = pView;
73 theRowIndex = pRowIndex;
74 isReadOnly = true;
75 }
76
77 /**
78 * Obtain the underlying sheet.
79 *
80 * @return the underlying sheet
81 */
82 public PrometheusSheetSheet getSheet() {
83 return theSheet;
84 }
85
86 /**
87 * Obtain the underlying view.
88 *
89 * @return the underlying view
90 */
91 public PrometheusSheetView getView() {
92 return theView;
93 }
94
95 /**
96 * Obtain the row index.
97 *
98 * @return row index
99 */
100 public int getRowIndex() {
101 return theRowIndex;
102 }
103
104 /**
105 * Is the row readOnly?
106 *
107 * @return true/false
108 */
109 public boolean isReadOnly() {
110 return isReadOnly;
111 }
112
113 /**
114 * Get the next row.
115 *
116 * @return the next row
117 */
118 public abstract PrometheusSheetRow getNextRow();
119
120 /**
121 * Get the previous row.
122 *
123 * @return the previous row
124 */
125 public abstract PrometheusSheetRow getPreviousRow();
126
127 /**
128 * Determine number of cells in this row.
129 *
130 * @return the number of cells.
131 */
132 public abstract int getCellCount();
133
134 /**
135 * Determine index of the max valued cell.
136 *
137 * @return the index.
138 */
139 public abstract int getMaxValuedCellIndex();
140
141 /**
142 * Set hidden status.
143 *
144 * @param isHidden is the column hidden?
145 */
146 public void setHidden(final boolean isHidden) {
147 if (!isReadOnly) {
148 setHiddenValue(isHidden);
149 }
150 }
151
152 /**
153 * Set hidden status.
154 *
155 * @param isHidden is the column hidden?
156 */
157 protected abstract void setHiddenValue(boolean isHidden);
158
159 /**
160 * Is the column hidden?
161 *
162 * @return true/false
163 */
164 public abstract boolean isHidden();
165
166 /**
167 * Get the cell at the required index.
168 *
169 * @param pIndex the column index.
170 * @return the cell
171 */
172 public abstract PrometheusSheetCell getReadOnlyCellByIndex(int pIndex);
173
174 /**
175 * Get the cell at the required index.
176 *
177 * @param pIndex the column index.
178 * @return the cell
179 */
180 public PrometheusSheetCell getMutableCellByIndex(final int pIndex) {
181 return !isReadOnly && pIndex >= 0
182 ? getWriteableCellByIndex(pIndex)
183 : null;
184 }
185
186 /**
187 * Get the cell at the required index.
188 *
189 * @param pIndex the column index.
190 * @return the cell
191 */
192 protected abstract PrometheusSheetCell getWriteableCellByIndex(int pIndex);
193
194 /**
195 * Obtain an iterator of non-null cells for the row in the view.
196 *
197 * @param pFirstIndex the first cell in the view
198 * @param pLastIndex the last cell in the view
199 * @return the iterator
200 */
201 protected abstract ListIterator<PrometheusSheetCell> iteratorForRange(int pFirstIndex,
202 int pLastIndex);
203 }