001/*
002 * Units of Measurement Common Library for Java
003 * Copyright (c) 2005-2023, Werner Keil and others.
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-385, Indriya nor the names of their 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 tech.uom.lib.common.util;
031
032import static java.lang.Character.isDigit;
033import static java.lang.Character.isSpaceChar;
034import java.util.Comparator;
035
036/**
037 * Compares two {@linkplain Number} objects with each other
038 * @author Werner
039 * @version 1.0
040 * @since 1.0.2
041 */
042public class NumberComparator implements Comparator<Number> {
043
044    private NumberComparator() {
045        // Singleton
046    }
047
048    protected static char charAt(String s, int i) {
049        if (i >= s.length()) {
050            return '\000';
051        }
052
053        return s.charAt(i);
054    }
055
056    protected int compareRight(String a, String b) {
057        int bias = 0;
058        int ia = 0;
059        int ib = 0;
060        while (true) {
061            char ca = charAt(a, ia);
062            char cb = charAt(b, ib);
063
064            if ((!isDigit(ca)) && (!isDigit(cb))) {
065                return bias;
066            }
067            if (!isDigit(ca)) {
068                return -1;
069            }
070            if (!isDigit(cb)) {
071                return 1;
072            }
073            if (ca < cb) {
074                if (bias == 0) {
075                    bias = -1;
076                }
077            } else if (ca > cb) {
078                if (bias == 0) {
079                    bias = 1;
080                }
081            } else if ((ca == 0) && (cb == 0)) {
082                return bias;
083            }
084            ia++;
085            ib++;
086        }
087    }
088
089    public int compare(Number o1, Number o2) {
090        String a = o1.toString();
091        String b = o2.toString();
092
093        int ia = 0;
094        int ib = 0;
095        int nza;
096        int nzb;
097        while (true) {
098            nza = nzb = 0;
099
100            char ca = charAt(a, ia);
101            char cb = charAt(b, ib);
102
103            while ((isSpaceChar(ca)) || (ca == '0')) {
104                if (ca == '0') {
105                    nza++;
106                } else {
107                    nza = 0;
108                }
109
110                ca = charAt(a, ++ia);
111            }
112
113            while ((isSpaceChar(cb)) || (cb == '0')) {
114                if (cb == '0') {
115                    nzb++;
116                } else {
117                    nzb = 0;
118                }
119
120                cb = charAt(b, ++ib);
121            }
122            int result;
123            if ((isDigit(ca)) && (isDigit(cb)) && ((result = compareRight(a.substring(ia), b.substring(ib))) != 0)) {
124                return result;
125            }
126
127            if ((ca == 0) && (cb == 0)) {
128                return nza - nzb;
129            }
130
131            if (ca < cb) {
132                return -1;
133            }
134            if (ca > cb) {
135                return 1;
136            }
137
138            ia++;
139            ib++;
140        }
141    }
142
143    private static NumberComparator instance;
144
145    public static NumberComparator getInstance() {
146        if (instance == null) {
147            instance = new NumberComparator();
148        }
149        return instance;
150    }
151}