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

ljacqu / FileDuplicateFinder / 14006930974

22 Mar 2025 08:35AM UTC coverage: 23.055%. Remained the same
14006930974

push

github

ljacqu
Add Nullable annotation next to return value in methods

111 of 610 branches covered (18.2%)

Branch coverage included in aggregate %.

378 of 1511 relevant lines covered (25.02%)

1.28 hits per line

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

0.0
/src/main/java/ch/jalu/fileduplicatefinder/config/FileUtilConfiguration.java
1
package ch.jalu.fileduplicatefinder.config;
2

3
import ch.jalu.configme.SettingsManager;
4
import ch.jalu.configme.SettingsManagerBuilder;
5
import ch.jalu.configme.utils.Utils;
6
import ch.jalu.fileduplicatefinder.config.property.JfuOptionalProperty;
7
import ch.jalu.fileduplicatefinder.config.property.JfuProperty;
8
import org.jetbrains.annotations.Nullable;
9

10
import java.nio.file.Path;
11
import java.nio.file.Paths;
12
import java.util.Objects;
13
import java.util.Scanner;
14
import java.util.function.Function;
15

16
/**
17
 * Provides values for all configurations.
18
 */
19
public class FileUtilConfiguration {
20

21
    private final SettingsManager settingsManager;
22
    private final ScannerPropertySource scannerPropertySource;
23

24
    /**
25
     * Constructor.
26
     *
27
     * @param scanner scanner instance to get user input when needed
28
     * @param userPropertyFile custom path to the configuration file (nullable); a default name is used if null
29
     */
30
    public FileUtilConfiguration(Scanner scanner, @Nullable Path userPropertyFile) {
×
31
        Path configFile = Objects.requireNonNullElseGet(userPropertyFile,
×
32
            () -> Paths.get("./file-utils.properties"));
×
33
        this.settingsManager = createSettingsManager(configFile);
×
34
        this.scannerPropertySource = new ScannerPropertySource(scanner);
×
35
    }
×
36

37
    public <T> T getValue(JfuProperty<T> property) {
38
        return getValue(property, false);
×
39
    }
40

41
    public <T> T getValue(JfuProperty<T> property, boolean forcePrompt) {
42
        return getValue0(property, forcePrompt, Function.identity());
×
43
    }
44

45
    public <T> T getValueOrPrompt(JfuOptionalProperty<T> property) {
46
        return getValueOrPrompt(property, false);
×
47
    }
48

49
    public <T> T getValueOrPrompt(JfuOptionalProperty<T> property, boolean forcePrompt) {
50
        return getValue0(property, forcePrompt, opt -> opt.orElse(null));
×
51
    }
52

53
    public <T> void setValue(JfuProperty<T> property, T value) {
54
        settingsManager.setProperty(property, value);
×
55
    }
×
56

57
    public void save() {
58
        settingsManager.save();
×
59
    }
×
60

61
    private <T, R> R getValue0(JfuProperty<T> property, boolean forcePrompt, Function<T, R> resultTransformer) {
62
        // 1. Get existing value and output in case there is any error (this informs the user in case it was weird in
63
        //    the properties file or on the command line)
64
        ValueOrError<T> valueOrError = fromOverridingSourceOrSettingsManager(property);
×
65
        if (valueOrError.getErrorReason() != null) {
×
66
            System.err.println("Configured value for '" + property.getPath() + "' is invalid: "
×
67
                + valueOrError.getErrorReason());
×
68
        }
69

70
        // 2. If there is a value, return it if we don't need to force the user to respecify the value.
71
        R oldValue = valueOrError.getValue() == null ? null : resultTransformer.apply(valueOrError.getValue());
×
72
        if (!forcePrompt && oldValue != null) {
×
73
            return oldValue;
×
74
        }
75

76
        // 3. Inform the user what he has to input
77
        String prevValue = oldValue == null
×
78
            ? ""
×
79
            : " (current value: \"" + property.toExportValue(valueOrError.getValue()) + "\")";
×
80
        System.out.println("Please enter a value for '" + property.getPath() + "'" + prevValue + ":");
×
81

82
        // 4. Get input and validate (repeating the process until we have a valid input)
83
        while (true) {
84
            String strValue = scannerPropertySource.promptStringAndRegister(property.getPath());
×
85
            valueOrError = property.fromString(strValue);
×
86

87
            if (valueOrError.getErrorReason() != null) {
×
88
                System.err.println(valueOrError.getErrorReason());
×
89
            } else {
90
                R result = resultTransformer.apply(valueOrError.getValue());
×
91
                if (result == null) {
×
92
                    System.err.println("Please provide a value");
×
93
                } else {
94
                    return result;
×
95
                }
96
            }
97

98
            System.out.println("Please enter a value for '" + property.getPath() + "':");
×
99
        }
×
100
    }
101

102
    private <T> ValueOrError<T> fromOverridingSourceOrSettingsManager(JfuProperty<T> property) {
103
        String overridingValue = getValueFromOverridingSources(property.getPath());
×
104
        if (overridingValue != null) {
×
105
            return property.fromString(overridingValue);
×
106
        }
107
        return ValueOrError.forValue(settingsManager.getProperty(property));
×
108
    }
109

110
    private @Nullable String getValueFromOverridingSources(String path) {
111
        String value = scannerPropertySource.getValue(path);
×
112
        if (value != null) {
×
113
            return value;
×
114
        }
115

116
        return System.getProperty(path);
×
117
    }
118

119
    private static SettingsManager createSettingsManager(Path configFile) {
120
        Utils.createFileIfNotExists(configFile);
×
121
        return SettingsManagerBuilder
×
122
            .withResource(new PropertyFileResource(configFile))
×
123
            .configurationData(FileUtilSettings.class)
×
124
            .useDefaultMigrationService()
×
125
            .create();
×
126
    }
127
}
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