1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30  */
  31 
  32 /*
  33  * This source code is provided to illustrate the usage of a given feature
  34  * or technique and has been deliberately simplified. Additional steps
  35  * required for a production-quality application, such as security checks,
  36  * input validation and proper error handling, might not be present in
  37  * this sample code.
  38  */
  39 
  40 package comparator;
  41 
  42 import java.util.Comparator;
  43 
  44 class AlphaNumericCharArrayComparator implements Comparator<char[]>
  45 {
  46     final Comparator<Character> charComparator;
  47 
  48     public AlphaNumericCharArrayComparator() {
  49         this(Character::compare);
  50     }
  51 
  52     public AlphaNumericCharArrayComparator(Comparator<Character> charComparator) {
  53         this.charComparator = charComparator;
  54     }
  55 
  56     /**
  57      * Counts how many consequtive characters in the array {@code o}
  58      * are digits, starting from the position {@code from}.
  59      */
  60     private int countDigits(char[] o, int from) {
  61         for (int i = from; ; ++i) {
  62             if (i == o.length || !Character.isDigit(o[i])) {
  63                 return (i - from);
  64             }
  65         }
  66     }
  67 
  68     @Override
  69     public int compare(char[] o1, char[] o2) {
  70         final int minLength = Integer.min(o1.length, o2.length);
  71         int numStart1 = 0;
  72         int index = 0;
  73 
  74         // skip common prefix of the arguments
  75         while (index < minLength &&
  76                 charComparator.compare(o1[index], o2[index]) == 0)
  77         {
  78             if (!Character.isDigit(o1[index++])) {
  79                 numStart1 = index;
  80             }
  81         }
  82 
  83         int numCount1 = index - numStart1;
  84         if (numCount1 > 0 ||
  85                 (index < minLength &&
  86                         Character.isDigit(o1[index]) &&
  87                         Character.isDigit(o2[index])))
  88         {
  89             // first different char is digit
  90             int numCount2 = numCount1;
  91             int numStart2 = numStart1;
  92 
  93             numCount1 += countDigits(o1, index);
  94             numCount2 += countDigits(o2, index);
  95 
  96             int numCountWithZeros1 = numCount1;
  97             int numCountWithZeros2 = numCount2;
  98 
  99             while (numCount1 < numCount2) {
 100                 if (charComparator.compare(o2[numStart2], '0') != 0) {
 101                     return -1;
 102                 }
 103                 numStart2++;
 104                 numCount2--;
 105             }
 106 
 107             while (numCount2 < numCount1) {
 108                 if (charComparator.compare(o1[numStart1], '0') != 0) {
 109                     return 1;
 110                 }
 111                 numStart1++;
 112                 numCount1--;
 113             }
 114 
 115             // here numCount1 == numCount2;
 116             while (numCount1-- > 0) {
 117                 int res = charComparator.compare(o1[numStart1++], o2[numStart2++]);
 118                 if (res != 0) {
 119                     return res;
 120                 }
 121             }
 122 
 123             if (numCountWithZeros1 != numCountWithZeros2) {
 124                 return (numCountWithZeros1 < numCountWithZeros2) ? -1 : 1;
 125             }
 126 
 127         }
 128 
 129         // otherwise, compare literally
 130         for (;; ++index) {
 131             if (index < o1.length) {
 132                 if (index < o2.length) {
 133                     int res = charComparator.compare(o1[index], o2[index]);
 134                     if (res != 0) {
 135                         return res;
 136                     }
 137                 } else {
 138                     return 1;
 139                 }
 140             } else {
 141                 if (index < o2.length) {
 142                     return -1;
 143                 }
 144                 return 0;
 145             }
 146         }
 147     }
 148 }