문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:number [2015/07/10 18:04] kwon37xi [숫자들 간의 Type 무시 비교 / Comparing numbers] |
java:number [2015/07/13 13:59] kwon37xi [숫자들 간의 Type 무시 비교 / Comparing numbers] |
||
---|---|---|---|
줄 5: | 줄 5: | ||
<code java> | <code java> | ||
public int compare(final Number x, final Number y) { | public int compare(final Number x, final Number y) { | ||
- | if(isSpecial(x) || isSpecial(y)) | + | if(isSpecial(x) || isSpecial(y)) |
return Double.compare(x.doubleValue(), | return Double.compare(x.doubleValue(), | ||
- | else | + | |
+ | // x, y의 class가 동일하다면 y.compareTo(y) 만하도록 처리하면 성능이 좋아질 수 있다. | ||
+ | // x, y의 class가 다를 경우에만 BigDecimal로 변환한다. | ||
return toBigDecimal(x).compareTo(toBigDecimal(y)); | return toBigDecimal(x).compareTo(toBigDecimal(y)); | ||
+ | } | ||
} | } | ||
줄 28: | 줄 31: | ||
return new BigDecimal(number.longValue()); | return new BigDecimal(number.longValue()); | ||
if(number instanceof Float || number instanceof Double) | if(number instanceof Float || number instanceof Double) | ||
- | return new BigDecimal(number.doubleValue()); | + | |
+ | | ||
try { | try { | ||
줄 40: | 줄 44: | ||
</ | </ | ||
* '' | * '' | ||
+ | * 비교 대상이 되는 두 객체가 동일 클래스 인스턴스라면 '' | ||
* 특수한 숫자 객체를 제외한 int 계열의 숫자는 바로 '' | * 특수한 숫자 객체를 제외한 int 계열의 숫자는 바로 '' |