|
1 | 1 | package org.baeldung.gson.serialization; |
2 | 2 |
|
3 | 3 | public class SourceClass { |
4 | | - public int intValue; |
5 | | - public String stringValue; |
| 4 | + private int intValue; |
| 5 | + private String stringValue; |
6 | 6 |
|
7 | 7 | public SourceClass(final int intValue, final String stringValue) { |
8 | 8 | this.intValue = intValue; |
9 | 9 | this.stringValue = stringValue; |
10 | 10 | } |
11 | 11 |
|
| 12 | + // API |
| 13 | + |
| 14 | + public int getIntValue() { |
| 15 | + return intValue; |
| 16 | + } |
| 17 | + |
| 18 | + public String getStringValue() { |
| 19 | + return stringValue; |
| 20 | + } |
| 21 | + |
| 22 | + // |
| 23 | + |
12 | 24 | @Override |
13 | | - public int hashCode() { |
14 | | - final int prime = 31; |
15 | | - int result = 1; |
16 | | - result = prime * result + intValue; |
17 | | - result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); |
18 | | - return result; |
| 25 | + public String toString() { |
| 26 | + return "SourceClass{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}'; |
19 | 27 | } |
20 | 28 |
|
21 | 29 | @Override |
22 | | - public boolean equals(final Object obj) { |
23 | | - if (this == obj) |
| 30 | + public boolean equals(final Object o) { |
| 31 | + if (this == o) |
24 | 32 | return true; |
25 | | - if (obj == null) |
| 33 | + if (!(o instanceof SourceClass)) |
26 | 34 | return false; |
27 | | - if (getClass() != obj.getClass()) |
28 | | - return false; |
29 | | - final SourceClass other = (SourceClass) obj; |
30 | | - if (intValue != other.intValue) |
| 35 | + |
| 36 | + final SourceClass that = (SourceClass) o; |
| 37 | + |
| 38 | + if (intValue != that.intValue) |
31 | 39 | return false; |
32 | | - if (stringValue == null) { |
33 | | - if (other.stringValue != null) |
34 | | - return false; |
35 | | - } else if (!stringValue.equals(other.stringValue)) |
| 40 | + if (!stringValue.equals(that.stringValue)) |
36 | 41 | return false; |
| 42 | + |
37 | 43 | return true; |
38 | 44 | } |
39 | 45 |
|
40 | 46 | @Override |
41 | | - public String toString() { |
42 | | - return "SourceClass{" + "intValue=" + intValue + ", stringValue='" + stringValue + '\'' + '}'; |
| 47 | + public int hashCode() { |
| 48 | + int result = intValue; |
| 49 | + result = 31 * result + stringValue.hashCode(); |
| 50 | + return result; |
43 | 51 | } |
44 | | - |
45 | 52 | } |
0 commit comments