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

trydofor / professional-mirana / #110

05 Feb 2025 02:51AM UTC coverage: 87.133% (-0.03%) from 87.164%
#110

push

trydofor
✨ applyLocale to i18nAware #52

1 of 4 new or added lines in 3 files covered. (25.0%)

1 existing line in 1 file now uncovered.

7144 of 8199 relevant lines covered (87.13%)

0.87 hits per line

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

91.67
/src/main/java/pro/fessional/mirana/i18n/I18nString.java
1
package pro.fessional.mirana.i18n;
2

3
import org.jetbrains.annotations.Contract;
4
import org.jetbrains.annotations.NotNull;
5
import org.jetbrains.annotations.Nullable;
6

7
import java.beans.Transient;
8
import java.text.MessageFormat;
9
import java.util.Arrays;
10
import java.util.Locale;
11
import java.util.Objects;
12

13
/**
14
 * <pre>
15
 * String can be used as i18n template (MessageFormat by default)
16
 * - i18nCode - template id, default empty
17
 * - i18nArgs - template arguments, default empty
18
 * - i18nHint - default text or template, not in hash and equals, default empty
19
 * - i18nCache - cached value for performance
20
 * </pre>
21
 *
22
 * @author trydofor
23
 * @see MessageFormat
24
 * @since 2019-09-19
25
 */
26
public class I18nString implements I18nAware {
27
    private static final long serialVersionUID = 19791023L;
28
    private static final Object[] EMPTY_ARGS = {};
1✔
29

30
    @NotNull
31
    private final String i18nCode;
32
    @NotNull
33
    private final Object[] i18nArgs;
34
    @NotNull
35
    private String i18nHint;
36

37
    private transient String i18nCache = null;
1✔
38

39
    public I18nString(String i18nCode) {
40
        this(i18nCode, "", EMPTY_ARGS);
1✔
41
    }
1✔
42

43
    public I18nString(String i18nCode, String i18nHint) {
44
        this(i18nCode, i18nHint, EMPTY_ARGS);
1✔
45
    }
1✔
46

47
    public I18nString(String i18nCode, String i18nHint, Object... args) {
1✔
48
        this.i18nCode = i18nCode == null ? "" : i18nCode;
1✔
49
        this.i18nArgs = args == null ? EMPTY_ARGS : args;
1✔
50
        this.i18nHint = i18nHint == null ? "" : i18nHint;
1✔
51
    }
1✔
52

53
    @Override
54
    @NotNull
55
    public String getI18nCode() {
56
        return i18nCode;
1✔
57
    }
58

59
    @Override
60
    @NotNull
61
    public Object[] getI18nArgs() {
62
        return i18nArgs;
1✔
63
    }
64

65
    @Override
66
    @NotNull
67
    public String getI18nHint() {
68
        return i18nHint;
1✔
69
    }
70

71
    @Contract("_ -> this")
72
    public I18nString setI18nHint(String i18nHint) {
73
        this.i18nHint = i18nHint == null ? "" : i18nHint;
1✔
74
        return this;
1✔
75
    }
76

77
    @Transient
78
    @Contract("_->this")
79
    public I18nString setI18nHintBy(@Nullable Locale locale) {
80
        return setI18nHint(toString(locale));
1✔
81
    }
82

83
    @Transient
84
    @Contract("_,_->this")
85
    public I18nString setI18nHintBy(@Nullable Locale locale, @NotNull I18nSource source) {
86
        return setI18nHint(toString(locale, source));
1✔
87
    }
88

89
    @Nullable
90
    @Transient
91
    public String getI18nCache() {
92
        return i18nCache;
1✔
93
    }
94

95
    @Contract("_->this")
96
    @Transient
97
    public I18nString setI18nCache(String i18nCache) {
98
        this.i18nCache = i18nCache;
1✔
99
        return this;
1✔
100
    }
101

102
    @Transient
103
    @Contract("_,_->this")
104
    public I18nString setI18nCacheBy(@Nullable Locale locale, @NotNull I18nSource source) {
105
        return setI18nCache(toString(locale, source));
1✔
106
    }
107

108
    @Transient
109
    public boolean isEmpty() {
110
        return i18nCode.isEmpty();
1✔
111
    }
112

113
    @Override
114
    public void applyLocale(Locale locale, @NotNull I18nSource source) {
NEW
115
        this.i18nCache = toString(locale, source);
×
NEW
116
    }
×
117

118
    @Override
119
    public String toString() {
120
        if (i18nCache != null) return i18nCache;
1✔
121
        return toString(Locale.getDefault());
1✔
122
    }
123

124
    @Override
125
    public boolean equals(Object o) {
126
        if (this == o) return true;
1✔
127
        if (!(o instanceof I18nString)) return false;
1✔
128
        I18nString that = (I18nString) o;
1✔
129
        return Objects.equals(i18nCode, that.i18nCode) && Arrays.equals(i18nArgs, that.i18nArgs);
1✔
130
    }
131

132
    @Override
133
    public int hashCode() {
134
        int result = Objects.hash(i18nCode);
1✔
135
        result = 31 * result + Arrays.hashCode(i18nArgs);
1✔
136
        return result;
1✔
137
    }
138

139
    /**
140
     * the string as i18nCode, empty args and hint
141
     */
142
    public static I18nString of(String str) {
143
        return new I18nString(str);
1✔
144
    }
145

146
    /**
147
     * the string as i18nCode, empty args and hint
148
     */
149
    public static I18nString of(@NotNull I18nAware i18n) {
150
        return new I18nString(i18n.getI18nCode(), i18n.getI18nHint(), i18n.getI18nArgs());
×
151
    }
152
}
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