3232import ru .nucodelabs .geo .target .RelativeErrorAwareTargetFunction ;
3333import ru .nucodelabs .geo .target .impl .SquareDiffTargetFunction ;
3434
35- import java .io .FileInputStream ;
3635import java .io .IOException ;
3736import java .math .RoundingMode ;
3837import java .net .URL ;
3938import java .nio .charset .StandardCharsets ;
39+ import java .nio .file .Files ;
4040import java .nio .file .Paths ;
4141import java .text .DecimalFormat ;
4242import java .text .DecimalFormatSymbols ;
@@ -69,8 +69,8 @@ private Locale currentLocale() {
6969
7070 @ Provides
7171 @ Singleton
72- private ResourceBundle uiProperties () {
73- return ResourceBundle .getBundle ("ui" , currentLocale () );
72+ private ResourceBundle uiProperties (Locale currentLocale ) {
73+ return ResourceBundle .getBundle ("ui" , currentLocale );
7474 }
7575
7676 @ Provides
@@ -149,6 +149,7 @@ private Preferences preferences() {
149149
150150 @ Provides
151151 @ Named (Name .PRECISE_FORMAT )
152+ @ Singleton
152153 DecimalFormat preciseFormat () {
153154 DecimalFormat decimalFormat = new DecimalFormat ();
154155 decimalFormat .setRoundingMode (RoundingMode .HALF_UP );
@@ -162,6 +163,7 @@ DecimalFormat preciseFormat() {
162163 }
163164
164165 @ Provides
166+ @ Singleton
165167 private DecimalFormat decimalFormat () {
166168 DecimalFormat decimalFormat = new DecimalFormat ();
167169 decimalFormat .setRoundingMode (RoundingMode .HALF_UP );
@@ -176,6 +178,7 @@ private DecimalFormat decimalFormat() {
176178 }
177179
178180 @ Provides
181+ @ Singleton
179182 private StringConverter <Double > doubleStringConverter (DecimalFormat decimalFormat ) {
180183 return new StringConverter <>() {
181184 @ Override
@@ -199,6 +202,7 @@ public Double fromString(String string) {
199202 }
200203
201204 @ Provides
205+ @ Singleton
202206 StringConverter <Number > numberStringConverter (StringConverter <Double > doubleStringConverter ) {
203207 return new StringConverter <>() {
204208 @ Override
@@ -216,44 +220,42 @@ public Number fromString(String string) {
216220 @ Provides
217221 @ Singleton
218222 @ Named (Name .DEFAULT_CLR )
219- ClrInfo clrInfo () throws IOException {
223+ LoadedClr colormapFile () throws IOException {
220224 // Firstly lookup next to executable
221- var externalFile = Paths .get ("colormap/default.clr" ). toFile () ;
222- if (! externalFile . exists ( )) {
225+ var externalFile = Paths .get ("colormap/default.clr" );
226+ if (Files . notExists ( externalFile )) {
223227 // Load default from resources if no external .clr file found
224228 var defaultResourceStream = Objects .requireNonNull (
225229 this .getClass ().getClassLoader ().getResourceAsStream ("colormap/default.clr" ),
226- "Resource colormap/default.clr is null"
230+ "Resource colormap/default.clr must not be null"
227231 );
232+ log .info ("Loading default .clr file from resources" );
228233 try (defaultResourceStream ) {
229- log .info ("Loading default .clr file from resources" );
230- return new ClrInfo (
234+ return new LoadedClr (
231235 "Default" ,
232236 new String (defaultResourceStream .readAllBytes (), StandardCharsets .UTF_8 )
233237 );
234238 }
235239 }
236- try (var externalFileStream = new FileInputStream (externalFile )) {
237- log .info ("Loading external .clr file" );
238- return new ClrInfo (
239- externalFile .getAbsolutePath (),
240- new String (externalFileStream .readAllBytes (), StandardCharsets .UTF_8 )
241- );
242- }
240+ log .info ("Loading external .clr file" );
241+ return new LoadedClr (
242+ externalFile .toAbsolutePath ().toString (),
243+ Files .readString (externalFile , StandardCharsets .UTF_8 )
244+ );
243245 }
244246
245247 @ Provides
246248 @ Singleton
247249 @ Named (Name .CLR_SOURCE )
248- String clrSource (@ Named (Name .DEFAULT_CLR ) ClrInfo clrInfo ) {
249- return clrInfo .source ();
250+ String clrSource (@ Named (Name .DEFAULT_CLR ) LoadedClr loadedClr ) {
251+ return loadedClr .source ();
250252 }
251253
252254 @ Provides
253255 @ Named (Name .DEFAULT_CLR )
254256 @ Singleton
255- ClrParser clrParser (@ Named (Name .DEFAULT_CLR ) ClrInfo clrInfo ) {
256- return new ClrParser (clrInfo .content ());
257+ ClrParser clrParser (@ Named (Name .DEFAULT_CLR ) LoadedClr loadedClr ) {
258+ return new ClrParser (loadedClr .content ());
257259 }
258260
259261 @ Provides
@@ -283,6 +285,6 @@ RelativeErrorAwareTargetFunction targetFunction() {
283285 return new SquareDiffTargetFunction ();
284286 }
285287
286- private record ClrInfo (String source , String content ) {
288+ private record LoadedClr (String source , String content ) {
287289 }
288290}
0 commit comments