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

smartsheet / smartsheet-java-sdk / #55

02 Oct 2024 07:40PM UTC coverage: 60.548% (+0.7%) from 59.836%
#55

push

github

web-flow
Release prep for 3.2.1 (#103)

4156 of 6864 relevant lines covered (60.55%)

0.61 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
/*
2
 * Copyright (C) 2024 Smartsheet
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.smartsheet.api.models.format;
18

19
import com.fasterxml.jackson.core.JsonGenerator;
20
import com.fasterxml.jackson.databind.JsonSerializer;
21
import com.fasterxml.jackson.databind.SerializerProvider;
22
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
23

24
import java.io.IOException;
25
import java.util.Arrays;
26

27
/**
28
 * This class represents the format as applied to a cell, row or column.
29
 */
30
@JsonSerialize(using = Format.FormatSerializer.class)
31
public class Format {
32

33
    //The default format.
34
    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✔
35
    static final int UNSET = Integer.MIN_VALUE;
36
    int[] formatArray;
37

38
    /**
39
     * Constructs a {@link Format} object using the format string provided by the Smartsheet API.
40
     * <p>
41
     * The format of the string is a comma separated list. See
42
     * <a href="https://smartsheet.redoc.ly/#section/API-Basics/Formatting">https://smartsheet.redoc.ly/#section/API-Basics/Formatting</a>
43
     * for details
44
     *
45
     * @param original the original
46
     */
47
    public Format(String original) {
1✔
48
        formatArray = new int[DEFAULT_FORMAT.length];
1✔
49
        FormatTokenizer tokenizer = new FormatTokenizer(original);
1✔
50
        for (int i = 0; tokenizer.next() && i < DEFAULT_FORMAT.length; i++) {
1✔
51
            if (tokenizer.isNextUnset()) {
1✔
52
                formatArray[i] = UNSET;
1✔
53
            } else {
54
                formatArray[i] = tokenizer.nextInt();
1✔
55
            }
56
        }
57
    }
1✔
58

59
    /**
60
     * Creates a {@link Format} object with default values.
61
     */
62
    public Format() {
×
63
        formatArray = Arrays.copyOf(DEFAULT_FORMAT, DEFAULT_FORMAT.length);
×
64
    }
×
65

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

76
    /**
77
     * @return the {@link FontFamily}.
78
     */
79
    public FontFamily getFontFamily() {
80
        return getFormatValue(FormatAttribute.FONT_FAMILY, FontFamily.values());
1✔
81
    }
82

83
    /**
84
     * @return the {@link FontSize}
85
     */
86
    public FontSize getFontSize() {
87
        return getFormatValue(FormatAttribute.FONT_SIZE, FontSize.values());
1✔
88
    }
89

90
    /**
91
     * @return the {@link Bold} format
92
     */
93
    public Bold getBold() {
94
        return getFormatValue(FormatAttribute.BOLD, Bold.values());
1✔
95
    }
96

97
    /**
98
     * @return the {@link Italic} format
99
     */
100
    public Italic getItalic() {
101
        return getFormatValue(FormatAttribute.ITALIC, Italic.values());
1✔
102
    }
103

104
    /**
105
     * @return the {@link Underline} status
106
     */
107
    public Underline getUnderline() {
108
        return getFormatValue(FormatAttribute.UNDERLINE, Underline.values());
1✔
109
    }
110

111
    /**
112
     * @return the {@link Strikethrough} status
113
     */
114
    public Strikethrough getStrikethrough() {
115
        return getFormatValue(FormatAttribute.STRIKETHROUGH, Strikethrough.values());
1✔
116
    }
117

118
    /**
119
     * @return the {@link HorizontalAlignment}
120
     */
121
    public HorizontalAlignment getHorizontalAlignment() {
122
        return getFormatValue(FormatAttribute.H_ALIGN, HorizontalAlignment.values());
1✔
123
    }
124

125
    /**
126
     * @return the {@link VerticalAlignment}
127
     */
128
    public VerticalAlignment getVerticalAlignment() {
129
        return getFormatValue(FormatAttribute.V_ALIGN, VerticalAlignment.values());
1✔
130
    }
131

132
    /**
133
     * @return the {@link Color} of the text.
134
     */
135
    public Color getTextColor() {
136
        return getFormatValue(FormatAttribute.TEXT_COLOR, Color.values());
1✔
137
    }
138

139
    /**
140
     * @return the {@link Color} of the background
141
     */
142
    public Color getBackgroundColor() {
143
        return getFormatValue(FormatAttribute.BACKGROUND_COLOR, Color.values());
1✔
144
    }
145

146
    /**
147
     * @return the {@link Color} of the task bar (gantt view)
148
     */
149
    public Color getTaskbarColor() {
150
        return getFormatValue(FormatAttribute.TASKBAR_COLOR, Color.values());
1✔
151
    }
152

153
    /**
154
     * @return the {@link Currency} format
155
     */
156
    public Currency getCurrency() {
157
        return getFormatValue(FormatAttribute.CURRENCY, Currency.values());
×
158
    }
159

160
    /**
161
     * @return the {@link DecimalCount}
162
     */
163
    public DecimalCount getDecimalCount() {
164
        return getFormatValue(FormatAttribute.DECIMAL_COUNT, DecimalCount.values());
×
165
    }
166

167
    /**
168
     * @return the {@link ThousandsSeparator}
169
     */
170
    public ThousandsSeparator getThousandsSeparator() {
171
        return getFormatValue(FormatAttribute.THOUSANDS_SEPARATOR, ThousandsSeparator.values());
1✔
172
    }
173

174
    /**
175
     * @return the {@link NumberFormat}
176
     */
177
    public NumberFormat getNumberFormat() {
178
        return getFormatValue(FormatAttribute.NUMBER_FORMAT, NumberFormat.values());
1✔
179
    }
180

181
    /**
182
     * @return the {@link TextWrap} status
183
     */
184
    public TextWrap getTextWrap() {
185
        return getFormatValue(FormatAttribute.TEXT_WRAP, TextWrap.values());
1✔
186
    }
187

188
    /**
189
     * @return the {@link DateFormat} status
190
     */
191
    public DateFormat getDateFormat() {
192
        return getFormatValue(FormatAttribute.DATE_FORMAT, DateFormat.values());
×
193
    }
194

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

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

236
        /**
237
         * Construct the {@link FormatTokenizer}.
238
         */
239
        public FormatTokenizer(String str) {
1✔
240
            chars = str.toCharArray();
1✔
241
            pos = -1;
1✔
242
        }
1✔
243

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

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

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

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

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

308
                if (value != UNSET) {
1✔
309
                    formatStringBuilder.append(value);
1✔
310
                }
311
            }
312

313
            return new Format(formatStringBuilder.toString());
1✔
314
        }
315

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

528
                if (formatValue != UNSET) {
1✔
529
                    stringBuilder.append(formatValue);
1✔
530
                }
531
            }
532

533
            generator.writeString(stringBuilder.toString());
1✔
534
        }
1✔
535
    }
536
}
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

© 2025 Coveralls, Inc