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

aspectran / aspectran / #4053

10 Feb 2025 01:14PM CUT coverage: 35.271% (-0.02%) from 35.294%
#4053

push

github

topframe
Update

0 of 1 new or added line in 1 file covered. (0.0%)

155 existing lines in 3 files now uncovered.

14245 of 40387 relevant lines covered (35.27%)

0.35 hits per line

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

28.87
/utils/src/main/java/com/aspectran/utils/StringifyContext.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
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
package com.aspectran.utils;
17

18
import com.aspectran.utils.annotation.jsr305.NonNull;
19
import com.aspectran.utils.annotation.jsr305.Nullable;
20

21
import java.text.ParseException;
22
import java.text.SimpleDateFormat;
23
import java.time.LocalDate;
24
import java.time.LocalDateTime;
25
import java.time.LocalTime;
26
import java.time.ZoneId;
27
import java.time.format.DateTimeFormatter;
28
import java.util.Date;
29
import java.util.Locale;
30

31
/**
32
 * <p>Created: 2024. 11. 30.</p>
33
 */
34
public class StringifyContext implements Cloneable {
35

36
    private Boolean pretty;
37

38
    private Integer indentSize;
39

40
    private Boolean indentTab;
41

42
    private Boolean nullWritable;
43

44
    private String dateTimeFormat;
45

46
    private String dateFormat;
47

48
    private String timeFormat;
49

50
    private Locale locale;
51

52
    private DateTimeFormatter dateTimeFormatter;
53

54
    private DateTimeFormatter dateFormatter;
55

56
    private DateTimeFormatter timeFormatter;
57

58
    public StringifyContext() {
1✔
59
    }
1✔
60

61
    public boolean hasPretty() {
62
        return (pretty != null);
1✔
63
    }
64

65
    public boolean isPretty() {
66
        return BooleanUtils.toBoolean(pretty);
×
67
    }
68

69
    public void setPretty(Boolean pretty) {
70
        this.pretty = pretty;
×
71
    }
×
72

73
    public boolean hasIndentSize() {
74
        return (indentSize != null);
1✔
75
    }
76

77
    public int getIndentSize() {
78
        return (indentSize != null ? indentSize : 0);
×
79
    }
80

81
    public void setIndentSize(int indentSize) {
82
        this.indentSize = indentSize;
×
83
    }
×
84

85
    public boolean isIndentTab() {
86
        return BooleanUtils.toBoolean(indentTab);
×
87
    }
88

89
    public void setIndentTab(boolean indentTab) {
UNCOV
90
        this.indentTab = indentTab;
×
91
    }
×
92

93
    @Nullable
94
    public String getIndentString() {
95
        if (indentTab != null && indentTab) {
×
96
            return "\t";
×
UNCOV
97
        } else if (getIndentSize() == 1) {
×
98
            return " ";
×
UNCOV
99
        } else if (getIndentSize() > 0) {
×
UNCOV
100
            return StringUtils.repeat(' ', indentSize);
×
101
        } else {
UNCOV
102
            return null;
×
103
        }
104
    }
105

106
    public boolean hasNullWritable() {
107
        return (nullWritable != null);
1✔
108
    }
109

110
    public boolean isNullWritable() {
111
        return BooleanUtils.toBoolean(nullWritable);
1✔
112
    }
113

114
    public void setNullWritable(Boolean nullWritable) {
115
        this.nullWritable = nullWritable;
1✔
116
    }
1✔
117

118
    public String getDateTimeFormat() {
119
        return dateTimeFormat;
1✔
120
    }
121

122
    public void setDateTimeFormat(String dateTimeFormat) {
123
        this.dateTimeFormat = dateTimeFormat;
1✔
124
        this.dateTimeFormatter = null;
1✔
125
    }
1✔
126

127
    public String getDateFormat() {
UNCOV
128
        return dateFormat;
×
129
    }
130

131
    public void setDateFormat(String dateFormat) {
132
        this.dateFormat = dateFormat;
1✔
133
        this.dateFormatter = null;
1✔
134
    }
1✔
135

136
    public String getTimeFormat() {
137
        return timeFormat;
×
138
    }
139

140
    public void setTimeFormat(String timeFormat) {
UNCOV
141
        this.timeFormat = timeFormat;
×
142
        this.timeFormatter = null;
×
UNCOV
143
    }
×
144

145
    public Locale getLocale() {
146
        return locale;
×
147
    }
148

149
    public void setLocale(Locale locale) {
150
        this.locale = locale;
×
151
        this.dateTimeFormatter = null;
×
152
        this.dateFormatter = null;
×
153
        this.timeFormatter = null;
×
154
    }
×
155

156
    public String toString(LocalDateTime localDateTime) {
157
        return toString(localDateTime, null);
1✔
158
    }
159

160
    public String toString(LocalDateTime localDateTime, String format) {
161
        Assert.notNull(localDateTime, "localDateTime must not be null");
1✔
162
        DateTimeFormatter dateTimeFormatter = touchDateTimeFormatter(format);
1✔
163
        if (dateTimeFormatter != null) {
1✔
164
            return localDateTime.format(dateTimeFormatter);
1✔
165
        } else {
UNCOV
166
            return localDateTime.toString();
×
167
        }
168
    }
169

170
    public String toString(LocalDate localDate) {
171
        return toString(localDate, null);
1✔
172
    }
173

174
    public String toString(LocalDate localDate, String format) {
175
        Assert.notNull(localDate, "localDate must not be null");
1✔
176
        DateTimeFormatter dateTimeFormatter = touchDateFormatter(format);
1✔
177
        if (dateTimeFormatter != null) {
1✔
178
            return localDate.format(dateTimeFormatter);
1✔
179
        } else {
UNCOV
180
            return localDate.toString();
×
181
        }
182
    }
183

184
    public String toString(LocalTime localTime) {
UNCOV
185
        return toString(localTime, null);
×
186
    }
187

188
    public String toString(LocalTime localTime, String format) {
UNCOV
189
        Assert.notNull(localTime, "localTime must not be null");
×
UNCOV
190
        DateTimeFormatter dateTimeFormatter = touchTimeFormatter(format);
×
UNCOV
191
        if (dateTimeFormatter != null) {
×
192
            return localTime.format(dateTimeFormatter);
×
193
        } else {
UNCOV
194
            return localTime.toString();
×
195
        }
196
    }
197

198
    public String toString(Date date) {
199
        return toString(date, null);
1✔
200
    }
201

202
    public String toString(Date date, String format) {
203
        Assert.notNull(date, "date must not be null");
1✔
204
        DateTimeFormatter dateTimeFormatter = touchDateTimeFormatter(format);
1✔
205
        if (dateTimeFormatter != null) {
1✔
206
            LocalDateTime ldt = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
1✔
207
            return ldt.format(dateTimeFormatter);
1✔
208
        } else {
UNCOV
209
            return date.toString();
×
210
        }
211
    }
212

213
    public LocalDateTime toLocalDateTime(String dateTime) {
UNCOV
214
        return toLocalDateTime(dateTime, null);
×
215
    }
216

217
    public LocalDateTime toLocalDateTime(String dateTime, String format) {
UNCOV
218
        Assert.notNull(dateTime, "dateTime must not be null");
×
UNCOV
219
        DateTimeFormatter dateTimeFormatter = touchDateTimeFormatter(format);
×
UNCOV
220
        return LocalDateTime.parse(dateTime, dateTimeFormatter);
×
221
    }
222

223
    public LocalDate toLocalDate(String date) {
UNCOV
224
        return toLocalDate(date, null);
×
225
    }
226

227
    public LocalDate toLocalDate(String date, String format) {
UNCOV
228
        Assert.notNull(date, "date must not be null");
×
UNCOV
229
        DateTimeFormatter dateTimeFormatter = touchDateFormatter(format);
×
230
        return LocalDate.parse(date, dateTimeFormatter);
×
231
    }
232

233
    public LocalTime toLocalTime(String time) {
UNCOV
234
        return toLocalTime(time, null);
×
235
    }
236

237
    public LocalTime toLocalTime(String time, String format) {
UNCOV
238
        Assert.notNull(time, "time must not be null");
×
UNCOV
239
        DateTimeFormatter dateTimeFormatter = touchTimeFormatter(format);
×
240
        return LocalTime.parse(time, dateTimeFormatter);
×
241
    }
242

243
    public Date toDate(String date) throws ParseException {
UNCOV
244
        return toDate(date, null);
×
245
    }
246

247
    public Date toDate(String date, String format) throws ParseException {
UNCOV
248
        Assert.notNull(date, "date must not be null");
×
UNCOV
249
        if (format != null) {
×
250
            return createSimpleDateFormat(format, locale).parse(date);
×
251
        } else {
252
            return createSimpleDateFormat(dateTimeFormat, locale).parse(date);
×
253
        }
254
    }
255

256
    private DateTimeFormatter touchDateTimeFormatter(String format) {
257
        if (format != null) {
1✔
UNCOV
258
            return createDateTimeFormatter(format, locale);
×
259
        } else if (dateTimeFormat != null && dateTimeFormatter == null) {
1✔
260
            dateTimeFormatter = createDateTimeFormatter(dateTimeFormat, locale);
1✔
261
        }
262
        return dateTimeFormatter;
1✔
263
    }
264

265
    private DateTimeFormatter touchDateFormatter(String format) {
266
        if (format != null) {
1✔
UNCOV
267
            return createDateTimeFormatter(format, locale);
×
268
        } else if (dateFormat != null && dateFormatter == null) {
1✔
269
            dateFormatter = createDateTimeFormatter(dateFormat, locale);
1✔
270
        }
271
        return dateFormatter;
1✔
272
    }
273

274
    private DateTimeFormatter touchTimeFormatter(String format) {
UNCOV
275
        if (format != null) {
×
UNCOV
276
            return createDateTimeFormatter(format, locale);
×
UNCOV
277
        } else if (timeFormat != null && timeFormatter == null) {
×
UNCOV
278
            timeFormatter = createDateTimeFormatter(timeFormat, locale);
×
279
        }
UNCOV
280
        return timeFormatter;
×
281
    }
282

283
    @NonNull
284
    private DateTimeFormatter createDateTimeFormatter(String format, Locale locale) {
285
        if (locale != null) {
1✔
UNCOV
286
            return DateTimeFormatter.ofPattern(format, locale);
×
287
        } else {
288
            return DateTimeFormatter.ofPattern(format);
1✔
289
        }
290
    }
291

292
    @NonNull
293
    private SimpleDateFormat createSimpleDateFormat(String format, Locale locale) {
UNCOV
294
        Assert.notNull(format, "format must not be null");
×
UNCOV
295
        if (locale != null) {
×
UNCOV
296
            return new SimpleDateFormat(format, locale);
×
297
        } else {
298
            return new SimpleDateFormat(format);
×
299
        }
300
    }
301

302
    public void merge(StringifyContext from) {
UNCOV
303
        if (pretty == null && from.pretty != null) {
×
UNCOV
304
            pretty = from.pretty;
×
305
        }
306
        if (indentSize == null && from.indentSize != null) {
×
307
            indentSize = from.indentSize;
×
308
        }
UNCOV
309
        if (indentTab == null && from.indentTab != null) {
×
310
            indentTab = from.indentTab;
×
311
        }
UNCOV
312
        if (nullWritable == null && from.nullWritable != null) {
×
UNCOV
313
            nullWritable = from.nullWritable;
×
314
        }
UNCOV
315
        if (dateTimeFormat == null && from.dateTimeFormat != null) {
×
UNCOV
316
            dateTimeFormat = from.dateTimeFormat;
×
317
        }
318
        if (dateFormat == null && from.dateFormat != null) {
×
319
            dateFormat = from.dateFormat;
×
320
        }
UNCOV
321
        if (timeFormat == null && from.timeFormat != null) {
×
UNCOV
322
            timeFormat = from.timeFormat;
×
323
        }
UNCOV
324
        if (locale == null && from.locale != null) {
×
325
            locale = from.locale;
×
326
        }
327
        if (dateTimeFormatter == null && from.dateTimeFormatter != null) {
×
328
            dateTimeFormatter = from.dateTimeFormatter;
×
329
        }
330
        if (dateFormatter == null && from.dateFormatter != null) {
×
331
            dateFormatter = from.dateFormatter;
×
332
        }
333
        if (timeFormatter == null && from.timeFormatter != null) {
×
334
            timeFormatter = from.timeFormatter;
×
335
        }
336
    }
×
337

338
    @Override
339
    public StringifyContext clone() {
340
        try {
UNCOV
341
            return (StringifyContext)super.clone();
×
UNCOV
342
        } catch (CloneNotSupportedException e) {
×
UNCOV
343
            throw new RuntimeException(e);
×
344
        }
345
    }
346

347
    @Override
348
    public String toString() {
UNCOV
349
        ToStringBuilder tsb = new ToStringBuilder();
×
UNCOV
350
        tsb.append("pretty", pretty);
×
NEW
351
        tsb.append("indentSize", indentSize);
×
UNCOV
352
        tsb.append("indentTab", indentTab);
×
UNCOV
353
        tsb.append("nullWritable", nullWritable);
×
UNCOV
354
        tsb.append("dateTimeFormat", dateTimeFormat);
×
UNCOV
355
        tsb.append("dateFormat", dateFormat);
×
UNCOV
356
        tsb.append("timeFormat", timeFormat);
×
UNCOV
357
        tsb.append("locale", locale);
×
UNCOV
358
        return tsb.toString();
×
359
    }
360

361
}
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