Created
November 23, 2016 07:35
-
-
Save yevgnenll/cf396b9b76770f5121ddcb7b60c52a6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by yevgnen on 2016-11-23. | |
*/ | |
public final class PhoneNumber { | |
private final short areaCode; | |
private final short prefix; | |
private final short lineNumber; | |
private volatile int hashCode; | |
public PhoneNumber(int areaCode, int prefix, int lineNumber){ | |
rangeCheck(areaCode, 999, "area code"); | |
rangeCheck(prefix, 999, "prefix"); | |
rangeCheck(lineNumber, 9999, "line number"); | |
this.areaCode = (short)areaCode; | |
this.prefix = (short)prefix; | |
this.lineNumber = (short)lineNumber; | |
} | |
private static void rangeCheck(int arg, int max, String name){ | |
if(arg < 0 || arg > max) | |
throw new IllegalArgumentException(name + ": " + arg); | |
} | |
@Override | |
public boolean equals(Object o){ | |
if(o == this) | |
return true; | |
if(!(o instanceof PhoneNumber)) | |
return false; | |
PhoneNumber pn = (PhoneNumber) o; | |
return pn.lineNumber == lineNumber | |
&& pn.prefix == prefix | |
&& pn.areaCode == areaCode; | |
} | |
@Override | |
public int hashCode(){ | |
int result = hashCode; | |
if(result == 0) { | |
result = 17; | |
result = 31 * result + areaCode; | |
result = 31 * result + prefix; | |
result = 31 * result + lineNumber; | |
hashCode = result; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
volatile 설명
lazy initialization