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

stasha / flex / 16

27 Apr 2025 11:53PM UTC coverage: 74.123% (+17.2%) from 56.927%
16

push

circleci

stasha
Re-factored project and directory name and added ordinals and plurals for 56 of countries

164 of 247 branches covered (66.4%)

Branch coverage included in aggregate %.

260 of 286 new or added lines in 10 files covered. (90.91%)

3 existing lines in 2 files now uncovered.

343 of 437 relevant lines covered (78.49%)

0.78 hits per line

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

76.09
/src/main/java/com/stasha/info/flex/formatters/FlexFormat.java
1
package com.stasha.info.flex.formatters;
2

3
import java.text.DateFormat;
4
import java.text.DecimalFormat;
5
import java.text.NumberFormat;
6
import java.text.SimpleDateFormat;
7
import java.util.Date;
8
import java.util.Locale;
9
import java.util.regex.Pattern;
10

11
/**
12
 *
13
 * @author stasha
14
 */
15
public class FlexFormat {
16
    private Pattern SPLIT_GENDER_PATTERN = Pattern.compile("\\s*\\|\\s*");
1✔
17
    
18
    private static final String DEFAULT_LOCALE = "en-US";
19
    private Locale loc;
20
    private DateFormat sdf;
21
    private DateFormat stf;
22
    private NumberFormat nf;
23
    private NumberFormat cf;
24
    private String dateFormat;
25
    private String timeFormat;
26

27
    public FlexFormat() {
1✔
28
        setLocale(DEFAULT_LOCALE);
1✔
29
    }
1✔
30

31
    protected void setLocale(String locale) {
32
        if (locale == null || locale.isBlank() || locale.equals("fmt.locale")) {
1!
33
            locale = DEFAULT_LOCALE;
1✔
34
        }
35

36
        if (loc == null || !loc.toLanguageTag().equals(locale)) {
1!
37
            loc = Locale.forLanguageTag(locale);
1✔
38
            sdf = SimpleDateFormat.getDateInstance(DateFormat.DEFAULT, loc);
1✔
39
            stf = SimpleDateFormat.getTimeInstance(DateFormat.DEFAULT, loc);
1✔
40
            nf = NumberFormat.getInstance(loc);
1✔
41
            cf = NumberFormat.getCurrencyInstance(loc);
1✔
42
            dateFormat = ((SimpleDateFormat) sdf).toPattern();
1✔
43
            timeFormat = ((SimpleDateFormat) stf).toPattern();
1✔
44
        }
45
    }
1✔
46

47
    /**
48
     * Formats a value according to the specified type and locale.
49
     *
50
     * @param value The value to format (e.g., Date, Number, String).
51
     * @param formatType The format type (e.g., "fmt.date", "fmt.currency").
52
     * @param userFormat Optional custom pattern (e.g., "dd.MM.yy").
53
     * @param locale The locale tag (e.g., "en-US") or null for default.
54
     * @return The formatted string or an error message if formatting fails.
55
     */
56
    public String format(Object value, String formatType, String userFormat, String locale) {
57

58
        setLocale(locale);
1✔
59

60
        try {
61
            switch (formatType) {
1✔
62
                case "fmt.date", "fmt.time" -> {
63
                    if (value instanceof Date) {
1!
64
                        DateFormat udf = formatType.equals("fmt.date") ? sdf : stf;
1!
65
                        String pattern = formatType.equals("fmt.date") ? dateFormat : timeFormat;
1!
66
                        if (userFormat != null && !pattern.equals(userFormat)) {
1!
67
                            udf = new SimpleDateFormat(userFormat, loc);
1✔
68
                            if (formatType.equals("fmt.date")) {
1!
69
                                sdf = udf;
1✔
70
                                dateFormat = userFormat;
1✔
71
                            } else {
NEW
72
                                stf = udf;
×
NEW
73
                                timeFormat = userFormat;
×
74
                            }
75
                        }
76
                        return udf.format(value);
1✔
77
                    }
NEW
78
                    return "argument: " + value + " can't be formatted as date";
×
79
                }
80
                case "fmt.decimal" -> {
81
                    if (userFormat != null && !userFormat.equals(((DecimalFormat) nf).toPattern())) {
1!
NEW
82
                        ((DecimalFormat) nf).applyPattern(userFormat);
×
83
                    }
84
                    return nf.format(value);
1✔
85
                }
86
                case "fmt.currency" -> {
87
                    if (userFormat != null && !userFormat.equals(((DecimalFormat) cf).toPattern())) {
1!
NEW
88
                        ((DecimalFormat) cf).applyPattern(userFormat);
×
89
                    }
90
                    return cf.format(value);
1✔
91
                }
92
                case "fmt.uppercase" -> {
93
                    return String.valueOf(value).toUpperCase(loc);
1✔
94
                }
95
                case "m", "f", "n" -> {
96
                    String val = String.valueOf(value);
1✔
97
                    if (val.contains("[")) {
1✔
98
                        String start = val.substring(0, val.lastIndexOf("["));
1✔
99
                        val = val.substring(val.lastIndexOf("[") + 1, val.length() - 1);
1✔
100
                        String[] vls = SPLIT_GENDER_PATTERN.split(val);
1✔
101
                        switch (formatType) {
1!
102
                            case "m" -> {
103
                                return start + vls[0];
1✔
104
                            }
105
                            case "f" -> {
106
                                return start + vls[1];
1✔
107
                            }
108
                            default -> {
NEW
109
                                return start + vls[2];
×
110
                            }
111
                        }
112
                    }
113
                    return String.valueOf(value);
1✔
114
                }
115
                default -> {
116
                    return userFormat != null ? userFormat : String.valueOf(value);
1!
117
                }
118
            }
NEW
119
        } catch (Exception ex) {
×
NEW
120
            return "argument: " + value + " can't be formatted as " + formatType;
×
121
        }
122
    }
123
}
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