Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * 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.
 */

import java.lang.reflect.Array;

public class Main {
    private static boolean compareScalarValues(Object myValue, Object otherValue) {
        if (myValue == null && otherValue != null || myValue != null && otherValue == null)
            return false;

        if (myValue == null)
            return true;

        if (myValue.getClass().isArray() && !otherValue.getClass().isArray()
                || !myValue.getClass().isArray() && otherValue.getClass().isArray())
            return false;

        if (myValue.getClass().isArray() && otherValue.getClass().isArray()) {
            final int myArraySize = Array.getLength(myValue);
            final int otherArraySize = Array.getLength(otherValue);

            if (myArraySize != otherArraySize)
                return false;

            for (int i = 0; i < myArraySize; i++)
                if (!Array.get(myValue, i).equals(Array.get(otherValue, i)))
                    return false;

            return true;
        }

        if (myValue instanceof Number && otherValue instanceof Number) {
            final Number myNumberValue = (Number) myValue;
            final Number otherNumberValue = (Number) otherValue;

            if (isInteger(myNumberValue) && isInteger(otherNumberValue))
                return myNumberValue.longValue() == otherNumberValue.longValue();
            else if (isFloat(myNumberValue) && isFloat(otherNumberValue))
                return myNumberValue.doubleValue() == otherNumberValue.doubleValue();
        }

        return myValue.equals(otherValue);
    }

    private static boolean isInteger(Number value) {
        return value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long;

    }

    private static boolean isFloat(Number value) {
        return value instanceof Float || value instanceof Double;
    }
}