]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/NumberComparator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / NumberComparator.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.util;
13
14 import java.util.Comparator;
15
16 public class NumberComparator implements Comparator<Number> {
17         
18         public static final NumberComparator INSTANCE = new NumberComparator();
19
20         @Override
21         public int compare(Number n1, Number n2) {
22                 boolean integerCompare = 
23                         (n1 instanceof Float==false) && 
24                         (n1 instanceof Double==false) && 
25                         (n2 instanceof Float==false) && 
26                         (n2 instanceof Double==false); 
27                 
28                 if (integerCompare) {
29                         long v1 = n1.longValue();
30                         long v2 = n2.longValue();
31                         return (v1>v2 ? -1 : (v1==v2 ? 0 : 1));
32                 } else {
33                         double v1 = n1.doubleValue();
34                         double v2 = n2.doubleValue();
35                         return (v1>v2 ? -1 : (v1==v2 ? 0 : 1));
36                 }
37         }
38
39 }
40