RecentRoms.java

1
package com.rox;
2
3
import java.nio.file.Path;
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.prefs.BackingStoreException;
7
import java.util.prefs.Preferences;
8
9
/** The ROMs opened most recently, newest first - kept between runs by a {@link Store}. */
10
final class RecentRoms {
11
    static final int MAX = 10;
12
13
    /** Where the list lives between runs. */
14
    interface Store {
15
        List<String> load();
16
17
        void save(List<String> paths);
18
    }
19
20
    private final Store store;
21
    private final List<Path> roms = new ArrayList<>();
22
23
    RecentRoms(final Store store){
24
        this.store = store;
25 1 1. <init> : removed call to java/util/stream/Stream::forEach → KILLED
        store.load().stream().map(RecentRoms::normalised).distinct().limit(MAX).forEach(roms::add);
26
    }
27
28
    /** Newest first. */
29
    List<Path> list(){
30 1 1. list : replaced return value with Collections.emptyList for com/rox/RecentRoms::list → KILLED
        return List.copyOf(roms);
31
    }
32
33
    /** Puts {@code rom} first - moving it there if it's already listed - dropping the oldest past {@link #MAX}. */
34
    void add(final Path rom){
35
        final Path path = normalised(rom.toString());
36
        roms.remove(path);
37 1 1. add : removed call to java/util/List::add → KILLED
        roms.add(0, path);
38 2 1. add : changed conditional boundary → KILLED
2. add : negated conditional → KILLED
        if (roms.size() > MAX){
39
            roms.remove(MAX);
40
        }
41 1 1. add : removed call to com/rox/RecentRoms::save → KILLED
        save();
42
    }
43
44
    void remove(final Path rom){
45 1 1. remove : negated conditional → KILLED
        if (roms.remove(normalised(rom.toString()))){
46 1 1. remove : removed call to com/rox/RecentRoms::save → KILLED
            save();
47
        }
48
    }
49
50
    void clear(){
51 1 1. clear : removed call to java/util/List::clear → KILLED
        roms.clear();
52 1 1. clear : removed call to com/rox/RecentRoms::save → KILLED
        save();
53
    }
54
55
    /**
56
     * How {@code rom} reads in a menu: its file name - with its folder too, when another listed ROM
57
     * shares that name.
58
     */
59
    String label(final Path rom){
60
        final Path name = rom.getFileName();
61 4 1. label : changed conditional boundary → KILLED
2. label : negated conditional → KILLED
3. lambda$label$0 : replaced boolean return with false for com/rox/RecentRoms::lambda$label$0 → KILLED
4. lambda$label$0 : replaced boolean return with true for com/rox/RecentRoms::lambda$label$0 → KILLED
        final boolean nameShared = roms.stream().filter(other -> other.getFileName().equals(name)).count() > 1;
62 2 1. label : negated conditional → KILLED
2. label : replaced return value with "" for com/rox/RecentRoms::label → KILLED
        return nameShared ? name + " (" + rom.getParent() + ")" : name.toString();
63
    }
64
65
    private void save(){
66 1 1. save : removed call to com/rox/RecentRoms$Store::save → KILLED
        store.save(roms.stream().map(Path::toString).toList());
67
    }
68
69
    private static Path normalised(final String path){
70 1 1. normalised : replaced return value with null for com/rox/RecentRoms::normalised → KILLED
        return Path.of(path).toAbsolutePath().normalize();
71
    }
72
73
    /** A {@link Store} in the user's Java preferences, one key per ROM. */
74
    static Store in(final Preferences node){
75 1 1. in : replaced return value with null for com/rox/RecentRoms::in → KILLED
        return new Store(){
76
            @Override
77
            public List<String> load(){
78
                final List<String> paths = new ArrayList<>();
79 2 1. load : changed conditional boundary → SURVIVED
2. load : negated conditional → KILLED
                for (int i = 0; i < MAX; i++){
80
                    final String path = node.get(key(i), null);
81 1 1. load : negated conditional → KILLED
                    if (path != null){
82
                        paths.add(path);
83
                    }
84
                }
85 1 1. load : replaced return value with Collections.emptyList for com/rox/RecentRoms$1::load → KILLED
                return paths;
86
            }
87
88
            @Override
89
            public void save(final List<String> paths){
90 2 1. save : changed conditional boundary → SURVIVED
2. save : negated conditional → KILLED
                for (int i = 0; i < MAX; i++){
91 2 1. save : negated conditional → KILLED
2. save : changed conditional boundary → KILLED
                    if (i < paths.size()){
92 1 1. save : removed call to java/util/prefs/Preferences::put → KILLED
                        node.put(key(i), paths.get(i));
93
                    } else {
94 1 1. save : removed call to java/util/prefs/Preferences::remove → KILLED
                        node.remove(key(i));
95
                    }
96
                }
97
                try {
98 1 1. save : removed call to java/util/prefs/Preferences::flush → KILLED
                    node.flush();
99
                } catch (BackingStoreException e){
100 1 1. save : removed call to java/io/PrintStream::println → SURVIVED
                    System.err.println("Couldn't save the recent ROMs list: " + e.getMessage());
101
                }
102
            }
103
        };
104
    }
105
106
    private static String key(final int index){
107 1 1. key : replaced return value with "" for com/rox/RecentRoms::key → KILLED
        return "rom" + index;
108
    }
109
}

Mutations

25

1.1
Location : <init>
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:startsWithWhatWasSaved()]
removed call to java/util/stream/Stream::forEach → KILLED

30

1.1
Location : list
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:startsWithWhatWasSaved()]
replaced return value with Collections.emptyList for com/rox/RecentRoms::list → KILLED

37

1.1
Location : add
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRelativePathIsListedAsAbsolute()]
removed call to java/util/List::add → KILLED

38

1.1
Location : add
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:keepsOnlyTheNewestTen()]
changed conditional boundary → KILLED

2.2
Location : add
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRelativePathIsListedAsAbsolute()]
negated conditional → KILLED

41

1.1
Location : add
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:theNewestIsFirstAndSaved()]
removed call to com/rox/RecentRoms::save → KILLED

45

1.1
Location : remove
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:removingOneNotListedSavesNothing()]
negated conditional → KILLED

46

1.1
Location : remove
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:removeTakesOneOff()]
removed call to com/rox/RecentRoms::save → KILLED

51

1.1
Location : clear
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:clearEmptiesIt()]
removed call to java/util/List::clear → KILLED

52

1.1
Location : clear
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:clearEmptiesIt()]
removed call to com/rox/RecentRoms::save → KILLED

61

1.1
Location : label
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRomIsLabelledByItsFileName()]
changed conditional boundary → KILLED

2.2
Location : label
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRomIsLabelledByItsFileName()]
negated conditional → KILLED

3.3
Location : lambda$label$0
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:romsSharingAFileNameAreLabelledWithTheirFolders()]
replaced boolean return with false for com/rox/RecentRoms::lambda$label$0 → KILLED

4.4
Location : lambda$label$0
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRomIsLabelledByItsFileName()]
replaced boolean return with true for com/rox/RecentRoms::lambda$label$0 → KILLED

62

1.1
Location : label
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRomIsLabelledByItsFileName()]
negated conditional → KILLED

2.2
Location : label
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:aRomIsLabelledByItsFileName()]
replaced return value with "" for com/rox/RecentRoms::label → KILLED

66

1.1
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:theNewestIsFirstAndSaved()]
removed call to com/rox/RecentRoms$Store::save → KILLED

70

1.1
Location : normalised
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:startsWithWhatWasSaved()]
replaced return value with null for com/rox/RecentRoms::normalised → KILLED

75

1.1
Location : in
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesThatCannotBeWrittenStillKeepTheListForThisRun()]
replaced return value with null for com/rox/RecentRoms::in → KILLED

79

1.1
Location : load
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesKeepTheListBetweenRuns()]
negated conditional → KILLED

2.2
Location : load
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

81

1.1
Location : load
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesThatCannotBeWrittenStillKeepTheListForThisRun()]
negated conditional → KILLED

85

1.1
Location : load
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesKeepTheListBetweenRuns()]
replaced return value with Collections.emptyList for com/rox/RecentRoms$1::load → KILLED

90

1.1
Location : save
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

2.2
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesKeepTheListBetweenRuns()]
negated conditional → KILLED

91

1.1
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesThatCannotBeWrittenStillKeepTheListForThisRun()]
negated conditional → KILLED

2.2
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesThatCannotBeWrittenStillKeepTheListForThisRun()]
changed conditional boundary → KILLED

92

1.1
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesKeepTheListBetweenRuns()]
removed call to java/util/prefs/Preferences::put → KILLED

94

1.1
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesForgetRomsTakenOffTheList()]
removed call to java/util/prefs/Preferences::remove → KILLED

98

1.1
Location : save
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesKeepTheListBetweenRuns()]
removed call to java/util/prefs/Preferences::flush → KILLED

100

1.1
Location : save
Killed by : none
removed call to java/io/PrintStream::println → SURVIVED
Covering tests

107

1.1
Location : key
Killed by : com.rox.RecentRomsTest.[engine:junit-jupiter]/[class:com.rox.RecentRomsTest]/[method:preferencesKeepTheListBetweenRuns()]
replaced return value with "" for com/rox/RecentRoms::key → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1