• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #41

24 Aug 2023 04:59PM UTC coverage: 50.458% (+0.01%) from 50.444%
#41

push

github-actions

web-flow
Fix Checkstyle Violations in "Impl" Classes (#53)

241 of 241 new or added lines in 32 files covered. (100.0%)

3417 of 6772 relevant lines covered (50.46%)

0.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.06
/src/main/java/com/smartsheet/api/models/format/Format.java
1
package com.smartsheet.api.models.format;
2

3
/*
4
 * #[license]
5
 * Smartsheet SDK for Java
6
 * %%
7
 * Copyright (C) 2023 Smartsheet
8
 * %%
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *      http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 * %[license]
21
 */
22

23
import com.fasterxml.jackson.core.JsonGenerator;
24
import com.fasterxml.jackson.databind.JsonSerializer;
25
import com.fasterxml.jackson.databind.SerializerProvider;
26
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
27

28
import java.io.IOException;
29
import java.util.Arrays;
30

31
/**
32
 * This class represents the format as applied to a cell, row or column.
33
 *
34
 * @author kskeem
35
 *
36
 */
37
@JsonSerialize(using = Format.FormatSerializer.class)
38
public class Format {
39

40
    //The default format.
41
    private static final int[] DEFAULT_FORMAT = new int[]{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1✔
42
    static final int UNSET = Integer.MIN_VALUE;
43
    int[] formatArray;
44

45
    /**
46
     * Constructs a {@link Format} object using the format string provided by the Smartsheet API.
47
     * <p>
48
     *  The format of the string is a comma separated list. See
49
     *  <a href="http://www.smartsheet.com/developers/api-documentation#h.mf6r2e3k3ftb">http://www.smartsheet.com/developers/api-documentation#h.mf6r2e3k3ftb</a>
50
     *  for details
51
     * @param original the original
52
     */
53
    public Format(String original) {
1✔
54
        formatArray = new int[DEFAULT_FORMAT.length];
1✔
55
        FormatTokenizer tokenizer = new FormatTokenizer(original);
1✔
56
        for (int i = 0; tokenizer.next() && i < DEFAULT_FORMAT.length; i++) {
1✔
57
            if (tokenizer.isNextUnset()) {
1✔
58
                formatArray[i] = UNSET;
1✔
59
            } else {
60
                formatArray[i] = tokenizer.nextInt();
1✔
61
            }
62
        }
63
    }
1✔
64

65
    /**
66
     * Creates a {@link Format} object with default values.
67
     */
68
    public Format() {
×
69
        formatArray = Arrays.copyOf(DEFAULT_FORMAT, DEFAULT_FORMAT.length);
×
70
    }
×
71

72
    protected <T extends Enum<?>> T getFormatValue(FormatAttribute attribute, T[] values) {
73
        if (formatArray[attribute.ordinal()] >= values.length) {
1✔
74
            return values[DEFAULT_FORMAT[attribute.ordinal()]];
1✔
75
        } else if (formatArray[attribute.ordinal()] == UNSET) {
1✔
76
            return null;
×
77
        } else {
78
            return values[formatArray[attribute.ordinal()]];
1✔
79
        }
80
    }
81

82
    /**
83
     * @return the {@link FontFamily}.
84
     */
85
    public FontFamily getFontFamily() {
86
        return getFormatValue(FormatAttribute.FONT_FAMILY, FontFamily.values());
1✔
87
    }
88

89
    /**
90
     * @return the {@link FontSize}
91
     */
92
    public FontSize getFontSize() {
93
        return getFormatValue(FormatAttribute.FONT_SIZE, FontSize.values());
1✔
94
    }
95

96
    /**
97
     * @return the {@link Bold} format
98
     */
99
    public Bold getBold() {
100
        return getFormatValue(FormatAttribute.BOLD, Bold.values());
1✔
101
    }
102

103
    /**
104
     * @return the {@link Italic} format
105
     */
106
    public Italic getItalic() {
107
        return getFormatValue(FormatAttribute.ITALIC, Italic.values());
1✔
108
    }
109

110
    /**
111
     * @return the {@link Underline} status
112
     */
113
    public Underline getUnderline() {
114
        return getFormatValue(FormatAttribute.UNDERLINE, Underline.values());
1✔
115
    }
116

117
    /**
118
     * @return the {@link Strikethrough} status
119
     */
120
    public Strikethrough getStrikethrough() {
121
        return getFormatValue(FormatAttribute.STRIKETHROUGH, Strikethrough.values());
1✔
122
    }
123

124
    /**
125
     * @return the {@link HorizontalAlignment}
126
     */
127
    public HorizontalAlignment getHorizontalAlignment() {
128
        return getFormatValue(FormatAttribute.H_ALIGN, HorizontalAlignment.values());
1✔
129
    }
130

131
    /**
132
     * @return the {@link VerticalAlignment}
133
     */
134
    public VerticalAlignment getVerticalAlignment() {
135
        return getFormatValue(FormatAttribute.V_ALIGN, VerticalAlignment.values());
1✔
136
    }
137

138
    /**
139
     * @return the {@link Color} of the text.
140
     */
141
    public Color getTextColor() {
142
        return getFormatValue(FormatAttribute.TEXT_COLOR, Color.values());
1✔
143
    }
144

145
    /**
146
     * @return the {@link Color} of the background
147
     */
148
    public Color getBackgroundColor() {
149
        return getFormatValue(FormatAttribute.BACKGROUND_COLOR, Color.values());
1✔
150
    }
151

152
    /**
153
     * @return the {@link Color} of the task bar (gantt view)
154
     */
155
    public Color getTaskbarColor() {
156
        return getFormatValue(FormatAttribute.TASKBAR_COLOR, Color.values());
1✔
157
    }
158

159
    /**
160
     * @return the {@link Currency} format
161
     */
162
    public Currency getCurrency() {
163
        return getFormatValue(FormatAttribute.CURRENCY, Currency.values());
×
164
    }
165

166
    /**
167
     * @return the {@link DecimalCount}
168
     */
169
    public DecimalCount getDecimalCount() {
170
        return getFormatValue(FormatAttribute.DECIMAL_COUNT, DecimalCount.values());
×
171
    }
172

173
    /**
174
     * @return the {@link ThousandsSeparator}
175
     */
176
    public ThousandsSeparator getThousandsSeparator() {
177
        return getFormatValue(FormatAttribute.THOUSANDS_SEPARATOR, ThousandsSeparator.values());
1✔
178
    }
179

180
    /**
181
     * @return the {@link NumberFormat}
182
     */
183
    public NumberFormat getNumberFormat() {
184
        return getFormatValue(FormatAttribute.NUMBER_FORMAT, NumberFormat.values());
1✔
185
    }
186

187
    /**
188
     * @return the {@link TextWrap} status
189
     */
190
    public TextWrap getTextWrap() {
191
        return getFormatValue(FormatAttribute.TEXT_WRAP, TextWrap.values());
1✔
192
    }
193

194
    /**
195
     * @return the {@link DateFormat} status
196
     */
197
    public DateFormat getDateFormat() {
198
        return getFormatValue(FormatAttribute.DATE_FORMAT, DateFormat.values());
×
199
    }
200

201
    /**
202
    * An enum whose "ordinal" property is used to identify the index into the format array.
203
    * Note that this means you !MUST NOT! change the order of these - even if you can't stand that they are not alphabetic
204
    * @author kskeem
205
    */
206
    private enum FormatAttribute {
1✔
207
        FONT_FAMILY,
1✔
208
        FONT_SIZE,
1✔
209
        BOLD,
1✔
210
        ITALIC,
1✔
211
        UNDERLINE,
1✔
212
        STRIKETHROUGH,
1✔
213
        H_ALIGN,
1✔
214
        V_ALIGN,
1✔
215
        TEXT_COLOR,
1✔
216
        BACKGROUND_COLOR,
1✔
217
        TASKBAR_COLOR,
1✔
218
        CURRENCY,
1✔
219
        DECIMAL_COUNT,
1✔
220
        THOUSANDS_SEPARATOR,
1✔
221
        NUMBER_FORMAT,
1✔
222
        TEXT_WRAP,
1✔
223
        DATE_FORMAT
1✔
224
    }
225

226
    /**
227
     * A utility class to help us parse the format string. Format strings are a comma separated list of integers.
228
     * Each position in the comma separated list maps to a specific format attribute. An attribute may not be set,
229
     * and in these situations, the default must be used.
230
     *
231
     * Valid strings (though actual format strings currently have 16 positions - these are for illustrative purposes only):
232
     * "0,1,0,2,0,1,"
233
     * ",,,,,,,,,,,"
234
     * ",,3,,,4,,1,,0"
235
     * "
236
     * This assumes positive integers only.
237
     *
238
     * @author kskeem
239
     *
240
     */
241
    class FormatTokenizer {
242
        char[] chars;
243
        int pos;
244
        static final char SEPARATOR = ',';
245

246
        /**
247
         * Construct the {@link FormatTokenizer}.
248
         */
249
        public FormatTokenizer(String str) {
1✔
250
            chars = str.toCharArray();
1✔
251
            pos = -1;
1✔
252
        }
1✔
253

254
        /**
255
         * Call this to check to see if there are any more ints available for consumption. This will avoid index out of bounds issues.
256
         */
257
        public boolean next() {
258
            pos++;
1✔
259
            return pos < chars.length || (pos == chars.length && chars[pos - 1] == SEPARATOR);
1✔
260
        }
261

262
        /**
263
         * Since we want to know the difference between set to "0" and unset, this  tells us if the next position is set or not.
264
         * Accounts for being at the end of the char array, where there is potentially one more position that needs to be accounted for.
265
         * @return whether the next position is set or not.
266
         */
267
        public boolean isNextUnset() {
268
            if (pos >= chars.length) {
1✔
269
                return true;
1✔
270
            } else {
271
                return chars[pos] == SEPARATOR;
1✔
272
            }
273
        }
274

275
        /**
276
         * Retrieves the next int. If an int is unset, 0 is returned.
277
         * You cannot distinguish between unset and 0 using this method. see isNextUnset() instead.
278
         * @return the next integer value.
279
         */
280
        public int nextInt() {
281
            int value = 0;
1✔
282
            char currentChar;
283
            //Advance through the characters until you hit the separator
284
            while (pos < chars.length && (currentChar = chars[pos++]) != SEPARATOR) {
1✔
285
                //Multiply the value by 10 to enable parsing multi-digit ints.
286
                //Use char math (subtracting '0' from the integer value) to cheaply convert the characters to ints.
287
                value = value * 10 + (currentChar - '0');
1✔
288
            }
289
            pos--;
1✔
290
            return value;
1✔
291
        }
292
    }
293

294
    /**
295
     * Builder class for a Format object
296
     */
297
    public static class FormatBuilder {
1✔
298
        int[] formatArray = new int[] {
1✔
299
            UNSET, UNSET, UNSET, UNSET,
300
            UNSET, UNSET, UNSET, UNSET,
301
            UNSET, UNSET, UNSET, UNSET,
302
            UNSET, UNSET, UNSET, UNSET,
303
            UNSET,
304
        };
305

306
        public Format build() {
307
            String delimiter = "";
1✔
308
            StringBuilder formatStringBuilder = new StringBuilder(30);
1✔
309
            for (Integer value : formatArray) {
1✔
310
                formatStringBuilder.append(delimiter);
1✔
311
                delimiter = ",";
1✔
312

313
                if (value != UNSET) {
1✔
314
                    formatStringBuilder.append(value);
1✔
315
                }
316
            }
317

318
            return new Format(formatStringBuilder.toString());
1✔
319
        }
320

321
        /**
322
         * Sets all properties based upon the supplied format
323
         *
324
         * @param value the value
325
         * @return the format builder
326
         */
327
        public FormatBuilder withFormat(Format value) {
328
            System.arraycopy(value.formatArray, 0, formatArray, 0, formatArray.length);
1✔
329
            return this;
1✔
330
        }
331

332
        /**
333
         * Sets the font family of the format
334
         *
335
         * @param value the value
336
         * @return the format builder
337
         */
338
        public FormatBuilder withFontFamily(FontFamily value) {
339
            formatArray[0] = getOrdinal(value);
1✔
340
            return this;
1✔
341
        }
342

343
        /**
344
         * Sets the font size of the format
345
         *
346
         * @param value the value
347
         * @return the format builder
348
         */
349
        public FormatBuilder withFontSize(FontSize value) {
350
            formatArray[1] = getOrdinal(value);
1✔
351
            return this;
1✔
352
        }
353

354
        /**
355
         * Sets the bold property of the format
356
         *
357
         * @param value the value
358
         * @return the format builder
359
         */
360
        public FormatBuilder withBold(Bold value) {
361
            formatArray[2] = getOrdinal(value);
1✔
362
            return this;
1✔
363
        }
364

365
        /**
366
         * Sets the italics property of the format
367
         *
368
         * @param value the value
369
         * @return the format builder
370
         */
371
        public FormatBuilder withItalic(Italic value) {
372
            formatArray[3] = getOrdinal(value);
1✔
373
            return this;
1✔
374
        }
375

376
        /**
377
         * Sets the underline property of the format
378
         *
379
         * @param value the value
380
         * @return the format builder
381
         */
382
        public FormatBuilder withUnderline(Underline value) {
383
            formatArray[4] = getOrdinal(value);
1✔
384
            return this;
1✔
385
        }
386

387
        /**
388
         * Sets the strike through property of the format
389
         *
390
         * @param value the value
391
         * @return the format builder
392
         */
393
        public FormatBuilder withStrikethrough(Strikethrough value) {
394
            formatArray[5] = getOrdinal(value);
1✔
395
            return this;
1✔
396
        }
397

398
        /**
399
         * Sets the horizontal alignment property of the format
400
         *
401
         * @param value the value
402
         * @return the format builder
403
         */
404
        public FormatBuilder withHorizontalAlignment(HorizontalAlignment value) {
405
            formatArray[6] = getOrdinal(value);
1✔
406
            return this;
1✔
407
        }
408

409
        /**
410
         * Sets the vertical alignment property of the format
411
         *
412
         * @param value the value
413
         * @return the format builder
414
         */
415
        public FormatBuilder withVerticalAlignment(VerticalAlignment value) {
416
            formatArray[7] = getOrdinal(value);
1✔
417
            return this;
1✔
418
        }
419

420
        /**
421
         * Sets the text color property of the format
422
         *
423
         * @param value the value
424
         * @return the format builder
425
         */
426
        public FormatBuilder withTextColor(Color value) {
427
            formatArray[8] = getOrdinal(value);
1✔
428
            return this;
1✔
429
        }
430

431
        /**
432
         * Sets the background color property of the format
433
         *
434
         * @param value the value
435
         * @return the format builder
436
         */
437
        public FormatBuilder withBackgroundColor(Color value) {
438
            formatArray[9] = getOrdinal(value);
1✔
439
            return this;
1✔
440
        }
441

442
        /**
443
         * Sets the taskbar color property of the format
444
         *
445
         * @param value the value
446
         * @return the format builder
447
         */
448
        public FormatBuilder withTaskbarColor(Color value) {
449
            formatArray[10] = getOrdinal(value);
1✔
450
            return this;
1✔
451
        }
452

453
        /**
454
         * Sets the currency of the format
455
         *
456
         * @param value the value
457
         * @return the format builder
458
         */
459
        public FormatBuilder withCurrency(Currency value) {
460
            formatArray[11] = getOrdinal(value);
1✔
461
            return this;
1✔
462
        }
463

464
        /**
465
         * Sets the decimal count of the format
466
         *
467
         * @param value the value
468
         * @return the format builder
469
         */
470
        public FormatBuilder withDecimalCount(DecimalCount value) {
471
            formatArray[12] = getOrdinal(value);
×
472
            return this;
×
473
        }
474

475
        /**
476
         * Sets the thousands separator property of the format
477
         *
478
         * @param value the value
479
         * @return the format builder
480
         */
481
        public FormatBuilder withThousandsSeparator(ThousandsSeparator value) {
482
            formatArray[13] = getOrdinal(value);
1✔
483
            return this;
1✔
484
        }
485

486
        /**
487
         * Sets the number format property of the format
488
         *
489
         * @param value the value
490
         * @return the format builder
491
         */
492
        public FormatBuilder withNumberFormat(NumberFormat value) {
493
            formatArray[14] = getOrdinal(value);
1✔
494
            return this;
1✔
495
        }
496

497
        /**
498
         * Sets the text wrap property of the format
499
         *
500
         * @param value the value
501
         * @return the format builder
502
         */
503
        public FormatBuilder withTextWrap(TextWrap value) {
504
            formatArray[15] = getOrdinal(value);
1✔
505
            return this;
1✔
506
        }
507

508
        /**
509
         * Sets the date format property of the format
510
         *
511
         * @param value the value
512
         * @return the format builder
513
         */
514
        public FormatBuilder withDateFormat(DateFormat value) {
515
            formatArray[16] = getOrdinal(value);
×
516
            return this;
×
517
        }
518

519
        private int getOrdinal(Enum<?> enumValue) {
520
            return (enumValue != null) ? enumValue.ordinal() : UNSET;
1✔
521
        }
522
    }
523

524
    public static class FormatSerializer extends JsonSerializer<Format> {
1✔
525
        @Override
526
        public void serialize(Format format, JsonGenerator generator, SerializerProvider provider) throws IOException {
527
            StringBuilder stringBuilder = new StringBuilder(30);
1✔
528
            String separator = "";
1✔
529
            for (int formatValue : format.formatArray) {
1✔
530
                stringBuilder.append(separator);
1✔
531
                separator = ",";
1✔
532

533
                if (formatValue != UNSET) {
1✔
534
                    stringBuilder.append(formatValue);
1✔
535
                }
536
            }
537

538
            generator.writeString(stringBuilder.toString());
1✔
539
        }
1✔
540
    }
541
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc