Browse Source

Merge pull request #107 from csmith/master

Track the dirty state of the alias manager.
pull/108/head
Greg Holmes 9 years ago
parent
commit
e8930ab7fd

+ 6
- 1
src/com/dmdirc/commandparser/aliases/AliasLoader.java View File

42
      * Loads aliases from the store into the manager.
42
      * Loads aliases from the store into the manager.
43
      */
43
      */
44
     public void load() {
44
     public void load() {
45
+        final boolean dirty = manager.isDirty();
45
         for (Alias alias : store.readAliases()) {
46
         for (Alias alias : store.readAliases()) {
46
             manager.addAlias(alias);
47
             manager.addAlias(alias);
47
         }
48
         }
49
+        manager.setDirty(dirty);
48
     }
50
     }
49
 
51
 
50
     /**
52
     /**
51
      * Saves aliases from the manager to the store.
53
      * Saves aliases from the manager to the store.
52
      */
54
      */
53
     public void save() {
55
     public void save() {
54
-        store.writeAliases(manager.getAliases());
56
+        if (manager.isDirty()) {
57
+            store.writeAliases(manager.getAliases());
58
+            manager.setDirty(false);
59
+        }
55
     }
60
     }
56
 
61
 
57
 }
62
 }

+ 23
- 0
src/com/dmdirc/commandparser/aliases/AliasManager.java View File

45
     private final CommandController commandController;
45
     private final CommandController commandController;
46
     /** Map of known alias names to their corresponding aliases. */
46
     /** Map of known alias names to their corresponding aliases. */
47
     private final Map<String, Alias> aliases = new ConcurrentSkipListMap<>();
47
     private final Map<String, Alias> aliases = new ConcurrentSkipListMap<>();
48
+    /** Whether or not changes have been made compared to the stored version. */
49
+    private boolean dirty;
48
 
50
 
49
     @Inject
51
     @Inject
50
     public AliasManager(final CommandController commandController) {
52
     public AliasManager(final CommandController commandController) {
65
 
67
 
66
         aliases.put(alias.getName(), alias);
68
         aliases.put(alias.getName(), alias);
67
         commandController.registerCommand(new AliasCommandHandler(commandController, alias), alias);
69
         commandController.registerCommand(new AliasCommandHandler(commandController, alias), alias);
70
+        dirty = true;
68
     }
71
     }
69
 
72
 
70
     /**
73
     /**
77
     public void removeAlias(final Alias alias) {
80
     public void removeAlias(final Alias alias) {
78
         if (aliases.containsKey(alias.getName())) {
81
         if (aliases.containsKey(alias.getName())) {
79
             commandController.unregisterCommand(aliases.remove(alias.getName()));
82
             commandController.unregisterCommand(aliases.remove(alias.getName()));
83
+            dirty = true;
80
         }
84
         }
81
     }
85
     }
82
 
86
 
90
     public void removeAlias(final String name) {
94
     public void removeAlias(final String name) {
91
         if (aliases.containsKey(name)) {
95
         if (aliases.containsKey(name)) {
92
             removeAlias(aliases.get(name));
96
             removeAlias(aliases.get(name));
97
+            dirty = true;
93
         }
98
         }
94
     }
99
     }
95
 
100
 
122
         return Optional.fromNullable(aliases.get(name));
127
         return Optional.fromNullable(aliases.get(name));
123
     }
128
     }
124
 
129
 
130
+    /**
131
+     * Gets the dirty state of the manager.
132
+     *
133
+     * @return True if the manager is dirty with respect to the store, false otherwise.
134
+     */
135
+    public boolean isDirty() {
136
+        return dirty;
137
+    }
138
+
139
+    /**
140
+     * Manually sets the dirty state of the manager. This should be done after the manager's
141
+     * aliases are loaded or saved.
142
+     *
143
+     * @param dirty True if the manager is now dirty with respect to the store, false otherwise.
144
+     */
145
+    public void setDirty(final boolean dirty) {
146
+        this.dirty = dirty;
147
+    }
125
 }
148
 }

Loading…
Cancel
Save