瀏覽代碼

Make Profile use AutoValue.

pull/342/head
Chris Smith 9 年之前
父節點
當前提交
80b757c330

+ 1
- 1
src/com/dmdirc/config/profiles/IdentitiesProfileMigrator.java 查看文件

@@ -67,7 +67,7 @@ public class IdentitiesProfileMigrator implements Migrator {
67 67
                 ident = Optional.empty();
68 68
             }
69 69
             profileManager.addProfile(
70
-                    new Profile(p.getName(), p.getOption(DOMAIN_PROFILE, "realname"), ident,
70
+                    Profile.create(p.getName(), p.getOption(DOMAIN_PROFILE, "realname"), ident,
71 71
                             p.getOptionList(DOMAIN_PROFILE, "nicknames")));
72 72
             try {
73 73
                 p.delete();

+ 11
- 60
src/com/dmdirc/config/profiles/Profile.java 查看文件

@@ -22,80 +22,31 @@
22 22
 
23 23
 package com.dmdirc.config.profiles;
24 24
 
25
-import com.google.common.base.MoreObjects;
26
-import com.google.common.collect.Lists;
25
+import com.google.auto.value.AutoValue;
27 26
 
28
-import java.util.Collections;
29 27
 import java.util.List;
30
-import java.util.Objects;
31 28
 import java.util.Optional;
32 29
 
33 30
 /**
34 31
  * Describes a profile, used to describe the settings associated with the local client on a
35 32
  * connection.
36 33
  */
37
-public class Profile {
34
+@AutoValue
35
+public abstract class Profile {
38 36
 
39
-    /** Profile Name, must be a sanitised filename. */
40
-    private final String name;
41
-    /** Real name. */
42
-    private final String realname;
43
-    /** Ident. */
44
-    private final Optional<String> ident;
45
-    /** Nicknames. */
46
-    private final List<String> nicknames;
37
+    Profile() {}
47 38
 
48
-    public Profile(final String name, final String realname, final Optional<String> ident,
49
-            final Iterable<String> nicknames) {
50
-        this.name = name;
51
-        this.realname = realname;
52
-        this.ident = ident;
53
-        this.nicknames = Lists.newArrayList(nicknames);
54
-    }
39
+    public abstract String getName();
55 40
 
56
-    public String getName() {
57
-        return name;
58
-    }
41
+    public abstract String getRealname();
59 42
 
60
-    public String getRealname() {
61
-        return realname;
62
-    }
43
+    public abstract Optional<String> getIdent();
63 44
 
64
-    public Optional<String> getIdent() {
65
-        return ident;
66
-    }
45
+    public abstract List<String> getNicknames();
67 46
 
68
-    public List<String> getNicknames() {
69
-        return Collections.unmodifiableList(nicknames);
47
+    public static Profile create(final String name, final String realName,
48
+            final Optional<String> ident, final List<String> nicknames) {
49
+        return new AutoValue_Profile(name, realName, ident, nicknames);
70 50
     }
71 51
 
72
-    @Override
73
-    public String toString() {
74
-        return MoreObjects.toStringHelper(this)
75
-                .add("name", name)
76
-                .add("realname", realname)
77
-                .add("ident", ident)
78
-                .add("nicknames", nicknames)
79
-                .toString();
80
-    }
81
-
82
-    @Override
83
-    public boolean equals(final Object o) {
84
-        if (this == o) {
85
-            return true;
86
-        }
87
-        if (o == null || getClass() != o.getClass()) {
88
-            return false;
89
-        }
90
-        final Profile profile = (Profile) o;
91
-        return Objects.equals(name, profile.getName())
92
-                && Objects.equals(realname, profile.getRealname())
93
-                && Objects.equals(ident, profile.getIdent())
94
-                && Objects.equals(nicknames, profile.getNicknames());
95
-    }
96
-
97
-    @Override
98
-    public int hashCode() {
99
-        return Objects.hash(name, realname, ident, nicknames);
100
-    }
101 52
 }

+ 1
- 1
src/com/dmdirc/config/profiles/ProfileManager.java 查看文件

@@ -54,7 +54,7 @@ public class ProfileManager {
54 54
         this.eventBus = eventBus;
55 55
         profiles = new ArrayList<>();
56 56
         final String nick = systemInfo.getProperty("user.name").replace(' ', '_');
57
-        defaultProfile = new Profile(nick, nick, Optional.empty(), Lists.newArrayList(nick));
57
+        defaultProfile = Profile.create(nick, nick, Optional.empty(), Lists.newArrayList(nick));
58 58
     }
59 59
 
60 60
     /**

+ 1
- 1
src/com/dmdirc/config/profiles/YamlProfileStore.java 查看文件

@@ -76,7 +76,7 @@ public class YamlProfileStore extends BaseYamlStore<Profile> implements ProfileS
76 76
             final String realname = requiredString(map, "realname");
77 77
             final Optional<String> ident = Optional.ofNullable(optionalString(map, "ident"));
78 78
             final List<String> nicknames = asList(map.get("nicknames"), s -> Optional.of(s.toString()));
79
-            return Optional.of(new Profile(name, realname, ident, nicknames));
79
+            return Optional.of(Profile.create(name, realname, ident, nicknames));
80 80
         } catch (IllegalArgumentException ex) {
81 81
             LOG.info("Unable to read profile", ex);
82 82
             return Optional.empty();

+ 1
- 1
src/com/dmdirc/ui/core/profiles/CoreProfilesDialogModel.java 查看文件

@@ -131,7 +131,7 @@ public class CoreProfilesDialogModel implements ProfilesDialogModel {
131 131
         final List<Profile> profileList = Lists.newArrayList(profileManager.getProfiles());
132 132
         profileList.forEach(profileManager::deleteProfile);
133 133
         profiles.values().forEach(p -> profileManager.addProfile(
134
-                new Profile(p.getName(), p.getRealname(), p.getIdent(), p.getNicknames())));
134
+                Profile.create(p.getName(), p.getRealname(), p.getIdent(), p.getNicknames())));
135 135
     }
136 136
 
137 137
     @Override

+ 3
- 3
test/com/dmdirc/config/profiles/IdentitiesProfileMigratorTest.java 查看文件

@@ -94,7 +94,7 @@ public class IdentitiesProfileMigratorTest {
94 94
         when(identityManager.getProvidersByType("profile")).thenReturn(
95 95
                 Lists.newArrayList(configProvider1));
96 96
         instance.migrate();
97
-        verify(profileManager).addProfile(new Profile("name1", "realname1", Optional.empty(),
97
+        verify(profileManager).addProfile(Profile.create("name1", "realname1", Optional.empty(),
98 98
                 Lists.newArrayList("nickname1")));
99 99
     }
100 100
 
@@ -103,7 +103,7 @@ public class IdentitiesProfileMigratorTest {
103 103
         when(identityManager.getProvidersByType("profile")).thenReturn(
104 104
                 Lists.newArrayList(configProvider2));
105 105
         instance.migrate();
106
-        verify(profileManager).addProfile(new Profile("name2", "realname2", Optional.of("ident2"),
106
+        verify(profileManager).addProfile(Profile.create("name2", "realname2", Optional.of("ident2"),
107 107
                 Lists.newArrayList("nickname2")));
108 108
     }
109 109
 
@@ -112,7 +112,7 @@ public class IdentitiesProfileMigratorTest {
112 112
         when(identityManager.getProvidersByType("profile")).thenReturn(
113 113
                 Lists.newArrayList(configProvider3));
114 114
         instance.migrate();
115
-        verify(profileManager).addProfile(new Profile("name3", "realname3", Optional.of("ident3"),
115
+        verify(profileManager).addProfile(Profile.create("name3", "realname3", Optional.of("ident3"),
116 116
                 Lists.newArrayList("nickname31", "nickname32", "nickname33")));
117 117
     }
118 118
 }

+ 1
- 1
test/com/dmdirc/config/profiles/ProfileManagerTest.java 查看文件

@@ -100,7 +100,7 @@ public class ProfileManagerTest {
100 100
     @Test
101 101
     public void testGetDefaultProfile_EmptyList() {
102 102
         final String nick = "UserName";
103
-        final Profile profile = new Profile(nick, nick, Optional.empty(), Lists.newArrayList(nick));
103
+        final Profile profile = Profile.create(nick, nick, Optional.empty(), Lists.newArrayList(nick));
104 104
         assertEquals(profile, instance.getDefault());
105 105
     }
106 106
 

+ 3
- 9
test/com/dmdirc/config/profiles/YamlProfileStoreTest.java 查看文件

@@ -23,19 +23,16 @@
23 23
 package com.dmdirc.config.profiles;
24 24
 
25 25
 import com.google.common.collect.Lists;
26
-import com.google.common.collect.Sets;
27 26
 import com.google.common.jimfs.Configuration;
28 27
 import com.google.common.jimfs.Jimfs;
29 28
 
30 29
 import java.io.IOException;
31 30
 import java.net.URISyntaxException;
32 31
 import java.nio.file.FileSystem;
33
-import java.nio.file.Files;
34 32
 import java.nio.file.Paths;
35 33
 import java.util.Collection;
36 34
 import java.util.List;
37 35
 import java.util.Optional;
38
-import java.util.Set;
39 36
 
40 37
 import org.junit.Before;
41 38
 import org.junit.Test;
@@ -54,11 +51,11 @@ public class YamlProfileStoreTest {
54 51
 
55 52
     @Before
56 53
     public void setup() {
57
-        profile1 = new Profile("profile1", "realname1", Optional.empty(),
54
+        profile1 = Profile.create("profile1", "realname1", Optional.empty(),
58 55
                 Lists.newArrayList("nickname1", "nickname2"));
59
-        profile2 = new Profile("profile2", "realname2", Optional.of("ident2"),
56
+        profile2 = Profile.create("profile2", "realname2", Optional.of("ident2"),
60 57
                 Lists.newArrayList("nickname1", "nickname3"));
61
-        profile3 = new Profile("profile3", "realname3", Optional.empty(),
58
+        profile3 = Profile.create("profile3", "realname3", Optional.empty(),
62 59
                 Lists.newArrayList("nickname3"));
63 60
     }
64 61
 
@@ -68,7 +65,6 @@ public class YamlProfileStoreTest {
68 65
                 .getResource("profiles.yml").toURI()));
69 66
         final Collection<Profile> profiles = store.readProfiles();
70 67
         assertEquals(3, profiles.size());
71
-        profiles.forEach(System.out::println);
72 68
         assertTrue(profiles.contains(profile1));
73 69
         assertTrue(profiles.contains(profile2));
74 70
         assertTrue(profiles.contains(profile3));
@@ -81,8 +77,6 @@ public class YamlProfileStoreTest {
81 77
                     .resolve("profiles.yml"));
82 78
             final List<Profile> profiles = Lists.newArrayList(profile1, profile2, profile3);
83 79
             store.writeProfiles(profiles);
84
-            System.out.println(Files.readAllLines(fs.getPath("/")
85
-                    .resolve("profiles.yml")));
86 80
             final Collection<Profile> readProfiles = store.readProfiles();
87 81
             assertEquals(3, readProfiles.size());
88 82
             assertTrue(readProfiles.contains(profile1));

+ 1
- 1
test/com/dmdirc/ui/core/profiles/CoreProfilesDialogModelTest.java 查看文件

@@ -81,7 +81,7 @@ public class CoreProfilesDialogModelTest {
81 81
     }
82 82
 
83 83
     private Profile getProfile(final int name) {
84
-        return new Profile("profile" + name, "realname" + name, Optional.of("ident" + name),
84
+        return Profile.create("profile" + name, "realname" + name, Optional.of("ident" + name),
85 85
                 Lists.newArrayList("nickname" + name + '1', "nickname" + name + '2',
86 86
                         "nickname" + name + '3'));
87 87
     }

Loading…
取消
儲存