forked from WegraLee/effective-java-3e-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseInsensitiveString.java
More file actions
39 lines (31 loc) · 1.11 KB
/
Copy pathCaseInsensitiveString.java
File metadata and controls
39 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package effectivejava.chapter3.item14;
import java.util.*;
// 코드 14-1 객체 참조 필드가 하나뿐인 비교자 (90쪽)
public final class CaseInsensitiveString
implements Comparable<CaseInsensitiveString> {
private final String s;
public CaseInsensitiveString(String s) {
this.s = Objects.requireNonNull(s);
}
// 수정된 equals 메서드 (56쪽)
@Override public boolean equals(Object o) {
return o instanceof CaseInsensitiveString &&
((CaseInsensitiveString) o).s.equalsIgnoreCase(s);
}
@Override public int hashCode() {
return s.hashCode();
}
@Override public String toString() {
return s;
}
// 자바가 제공하는 비교자를 사용해 클래스를 비교한다.
public int compareTo(CaseInsensitiveString cis) {
return String.CASE_INSENSITIVE_ORDER.compare(s, cis.s);
}
public static void main(String[] args) {
Set<CaseInsensitiveString> s = new TreeSet<>();
for (String arg : args)
s.add(new CaseInsensitiveString(arg));
System.out.println(s);
}
}