001/* 002 * Units of Measurement Common Library for Java 003 * Copyright (c) 2005-2016, Jean-Marie Dautelle, Werner Keil, V2COM. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-363, Unit-API nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tec.uom.lib.common.util; 031 032import static java.lang.Character.isDigit; 033import static java.lang.Character.isSpaceChar; 034 035import java.util.Comparator; 036 037/** 038 * Compares two {@linkplain Number} objects with each other 039 * @author Werner 040 * @version 0.1 041 * @since 1.0.2 042 */ 043public class NumberComparator implements Comparator<Number> { 044 045 private NumberComparator() { 046 // Singleton 047 } 048 049 protected static char charAt(String s, int i) { 050 if (i >= s.length()) { 051 return '\000'; 052 } 053 054 return s.charAt(i); 055 } 056 057 protected int compareRight(String a, String b) { 058 int bias = 0; 059 int ia = 0; 060 int ib = 0; 061 while (true) { 062 char ca = charAt(a, ia); 063 char cb = charAt(b, ib); 064 065 if ((!isDigit(ca)) && (!isDigit(cb))) { 066 return bias; 067 } 068 if (!isDigit(ca)) { 069 return -1; 070 } 071 if (!isDigit(cb)) { 072 return 1; 073 } 074 if (ca < cb) { 075 if (bias == 0) { 076 bias = -1; 077 } 078 } else if (ca > cb) { 079 if (bias == 0) 080 bias = 1; 081 } else if ((ca == 0) && (cb == 0)) 082 return bias; 083 ia++; 084 ib++; 085 } 086 } 087 088 public int compare(Number o1, Number o2) { 089 String a = o1.toString(); 090 String b = o2.toString(); 091 092 int ia = 0; 093 int ib = 0; 094 int nza; 095 int nzb; 096 while (true) { 097 nza = nzb = 0; 098 099 char ca = charAt(a, ia); 100 char cb = charAt(b, ib); 101 102 while ((isSpaceChar(ca)) || (ca == '0')) { 103 if (ca == '0') { 104 nza++; 105 } else { 106 nza = 0; 107 } 108 109 ca = charAt(a, ++ia); 110 } 111 112 while ((isSpaceChar(cb)) || (cb == '0')) { 113 if (cb == '0') { 114 nzb++; 115 } else { 116 nzb = 0; 117 } 118 119 cb = charAt(b, ++ib); 120 } 121 int result; 122 if ((isDigit(ca)) && (isDigit(cb)) && ((result = compareRight(a.substring(ia), b.substring(ib))) != 0)) { 123 return result; 124 } 125 126 if ((ca == 0) && (cb == 0)) { 127 return nza - nzb; 128 } 129 130 if (ca < cb) { 131 return -1; 132 } 133 if (ca > cb) { 134 return 1; 135 } 136 137 ia++; 138 ib++; 139 } 140 } 141 142 private static NumberComparator INSTANCE; 143 144 public static NumberComparator getInstance() { 145 if (INSTANCE == null) { 146 INSTANCE = new NumberComparator(); 147 } 148 return INSTANCE; 149 } 150}