Skip to content

Instantly share code, notes, and snippets.

@yevgnenll
Created November 23, 2016 07:35
Show Gist options
  • Save yevgnenll/cf396b9b76770f5121ddcb7b60c52a6e to your computer and use it in GitHub Desktop.
Save yevgnenll/cf396b9b76770f5121ddcb7b60c52a6e to your computer and use it in GitHub Desktop.
/**
* 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;
}
}
@yevgnenll
Copy link
Author

yevgnenll commented Nov 23, 2016

volatile 설명

lazy initialization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment