OSDN Git Service

リポジトリ内改行コードのLFへの修正
[charactermanaj/CharacterManaJ.git] / src / main / java / charactermanaj / model / CustomLayerOrderKey.java
1 package charactermanaj.model;
2
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 public class CustomLayerOrderKey implements Comparable<CustomLayerOrderKey> {
9
10         private final String id;
11
12         private final String displayName;
13
14         private final Map<String, String> localizedNames;
15
16         public static final String DEFAULT_NAME_KEY = "";
17
18         public CustomLayerOrderKey(String id, String name, Map<String, String> localizedNames) {
19                 if (id == null || name == null) {
20                         throw new NullPointerException("id and name is required.");
21                 }
22                 this.id = id;
23                 this.displayName = name;
24                 if (localizedNames == null) {
25                         localizedNames = Collections.emptyMap();
26                 }
27                 this.localizedNames = Collections.unmodifiableMap(new HashMap<String, String>(localizedNames));
28         }
29
30         public String getId() {
31                 return id;
32         }
33
34         public String getDisplayName() {
35                 return displayName;
36         }
37
38         public Map<String, String> getLocalizedNames() {
39                 return localizedNames;
40         }
41
42         @Override
43         public int hashCode() {
44                 final int prime = 31;
45                 int result = 1;
46                 result = prime * result + ((id == null) ? 0 : id.hashCode());
47                 return result;
48         }
49
50         @Override
51         public boolean equals(Object obj) {
52                 if (this == obj)
53                         return true;
54                 if (obj == null)
55                         return false;
56                 if (getClass() != obj.getClass())
57                         return false;
58                 CustomLayerOrderKey other = (CustomLayerOrderKey) obj;
59                 if (id == null) {
60                         if (other.id != null)
61                                 return false;
62                 } else if (!id.equals(other.id))
63                         return false;
64                 return true;
65         }
66
67         /**
68          * 表示名によるソート順
69          */
70         public static final Comparator<CustomLayerOrderKey> COMPARATOR = new Comparator<CustomLayerOrderKey>() {
71                 @Override
72                 public int compare(CustomLayerOrderKey o1, CustomLayerOrderKey o2) {
73                         // 表示名でソートする
74                         // ※ ソート順でマップ化した場合、表示名が異なるがIDが同一のものができうるため
75                         // ツリーマップ化などを使う場合はロジックでID重複を防ぐ必要がある。
76                         int ret = o1.displayName.compareTo(o2.displayName);
77                         if (ret == 0) {
78                                 ret = o1.id.compareTo(o2.id);
79                         }
80                         return ret;
81                 }
82         };
83
84         @Override
85         public int compareTo(CustomLayerOrderKey o) {
86                 return COMPARATOR.compare(this, o);
87         }
88
89         @Override
90         public String toString() {
91                 return displayName;
92         }
93 }