Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* ***********************************************************************
 * VMware ThinApp Factory
 * Copyright (c) 2009-2013 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ***********************************************************************/

public class Main {
    /**
     * Compare two string by chuning digits and non-digits separately and compare between the two.
     * This way, the version strings (Ex: 10.2c.4.24546 sp1) can be properly compared.
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int alnumCompare(String str1, String str2) {
        // Ensure null cases are handled for both s1 and s2.
        if (str1 == str2) {
            return 0;
        } else if (str1 == null) {
            return -1;
        } else if (str2 == null) {
            return 1;
        }
        int s1Length = str1.length();
        int s2Length = str2.length();

        for (int s1Index = 0, s2Index = 0; s1Index < s1Length && s2Index < s2Length;) {
            String thisStr = getDigitOrNonDigitChunk(str1, s1Length, s1Index);
            s1Index += thisStr.length();

            String thatStr = getDigitOrNonDigitChunk(str2, s2Length, s2Index);
            s2Index += thatStr.length();

            // If both strs contain numeric characters, sort them numerically
            int result = 0;
            if (Character.isDigit(thisStr.charAt(0)) && Character.isDigit(thatStr.charAt(0))) {
                // Simple str comparison by length.
                int thisStrLength = thisStr.length();
                result = thisStrLength - thatStr.length();
                // If equal, the first different number counts
                if (result == 0) {
                    for (int i = 0; i < thisStrLength; i++) {
                        result = thisStr.charAt(i) - thatStr.charAt(i);
                        if (result != 0) {
                            return result;
                        }
                    }
                }
            } else {
                result = thisStr.compareToIgnoreCase(thatStr);
            }

            if (result != 0) {
                return result;
            }
        }
        return s1Length - s2Length;
    }

    /**
     * Chunk digits or non-digits from a string starting at an index.
     *
     * @param s
     * @param length
     * @param index
     * @return
     */
    public static String getDigitOrNonDigitChunk(String s, int length, int index) {
        StringBuilder sb = new StringBuilder();
        char c = s.charAt(index);
        sb.append(c);
        index++;
        boolean digitOrNot = Character.isDigit(c);
        while (index < length) {
            c = s.charAt(index);
            if (digitOrNot != Character.isDigit(c)) {
                break;
            }
            sb.append(c);
            index++;
        }
        return sb.toString();
    }
}