-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexClass.java
66 lines (53 loc) · 1.46 KB
/
HexClass.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.smlnskgmail.jaman.codewarsjava.kyu6;
// https://www.codewars.com/kata/5483366098aa442def0009af
@SuppressWarnings("checkstyle:EqualsHashCode")
public class HexClass {
private final int input;
public HexClass(int input) {
this.input = input;
}
public int valueOf() {
return input;
}
public String toJSON() {
return toString();
}
public String toString() {
return String.format(
"0x%s",
Integer.toHexString(input).toUpperCase()
);
}
@SuppressWarnings("unused")
public HexClass plus(HexClass other) {
return new HexClass(input + other.input);
}
public HexClass plus(int number) {
return new HexClass(input + number);
}
public HexClass minus(HexClass other) {
return new HexClass(input - other.input);
}
public HexClass minus(int number) {
return new HexClass(input - number);
}
public static int parse(String string) {
return Integer.parseInt(
string.startsWith("0x")
? string.substring(2)
: string,
16
);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HexClass hex = (HexClass) o;
return input == hex.input;
}
}