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

smartsheet / smartsheet-java-sdk / #43

24 Aug 2023 10:26PM UTC coverage: 50.427% (-0.02%) from 50.442%
#43

push

github-actions

web-flow
Fix Checkstyle violations in api/models Classes (#57)

This will fix ~900 violations

189 of 189 new or added lines in 59 files covered. (100.0%)

3423 of 6788 relevant lines covered (50.43%)

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
 */
35
@JsonSerialize(using = Format.FormatSerializer.class)
36
public class Format {
37

38
    //The default format.
39
    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✔
40
    static final int UNSET = Integer.MIN_VALUE;
41
    int[] formatArray;
42

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

241
        /**
242
         * Construct the {@link FormatTokenizer}.
243
         */
244
        public FormatTokenizer(String str) {
1✔
245
            chars = str.toCharArray();
1✔
246
            pos = -1;
1✔
247
        }
1✔
248

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

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

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

289
    /**
290
     * Builder class for a Format object
291
     */
292
    public static class FormatBuilder {
1✔
293
        int[] formatArray = new int[] {
1✔
294
            UNSET, UNSET, UNSET, UNSET,
295
            UNSET, UNSET, UNSET, UNSET,
296
            UNSET, UNSET, UNSET, UNSET,
297
            UNSET, UNSET, UNSET, UNSET,
298
            UNSET,
299
        };
300

301
        /**
302
         * Build
303
         */
304
        public Format build() {
305
            String delimiter = "";
1✔
306
            StringBuilder formatStringBuilder = new StringBuilder(30);
1✔
307
            for (Integer value : formatArray) {
1✔
308
                formatStringBuilder.append(delimiter);
1✔
309
                delimiter = ",";
1✔
310

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

316
            return new Format(formatStringBuilder.toString());
1✔
317
        }
318

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

536
            generator.writeString(stringBuilder.toString());
1✔
537
        }
1✔
538
    }
539
}
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