Java Number Multiply multiply(V1 value1, V2 value2)

Here you can find the source of multiply(V1 value1, V2 value2)

Description

multiply

License

Apache License

Declaration

public static <V1, V2> Object multiply(V1 value1, V2 value2) 

Method Source Code

//package com.java2s;
/**/*from   www  .  java2 s.c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 {
    public static <V1, V2> Object multiply(V1 value1, V2 value2) {
        Object ret = null;
        if (null == value1 && null == value2) {
            ret = 0;
        } else if (null == value1) {
            ret = value2;
        } else if (null == value2) {
            ret = value1;
        } else {
            if (value1 instanceof Integer) {
                if (value2 instanceof Integer) {
                    ret = (Integer) value1 * (Integer) value2;
                } else if (value2 instanceof Long) {
                    ret = (Integer) value1 * (Long) value2;
                } else if (value2 instanceof Float) {
                    ret = (Integer) value1 * (Float) value2;
                } else if (value2 instanceof Double) {
                    ret = (Integer) value1 * (Double) value2;
                } else {
                    ret = 0;
                }
            } else if (value1 instanceof Long) {
                if (value2 instanceof Integer) {
                    ret = (Long) value1 * (Integer) value2;
                } else if (value2 instanceof Long) {
                    ret = (Long) value1 * (Long) value2;
                } else if (value2 instanceof Float) {
                    ret = (Long) value1 * (Float) value2;
                } else if (value2 instanceof Double) {
                    ret = (Long) value1 * (Double) value2;
                } else {
                    ret = 0L;
                }
            } else if (value1 instanceof Float) {
                if (value2 instanceof Integer) {
                    ret = (Float) value1 * (Integer) value2;
                } else if (value2 instanceof Long) {
                    ret = (Float) value1 * (Long) value2;
                } else if (value2 instanceof Float) {
                    ret = (Float) value1 * (Float) value2;
                } else if (value2 instanceof Double) {
                    ret = (Float) value1 * (Double) value2;
                } else {
                    ret = 0.0f;
                }
            } else if (value1 instanceof Double) {
                if (value2 instanceof Integer) {
                    ret = (Double) value1 * (Integer) value2;
                } else if (value2 instanceof Long) {
                    ret = (Double) value1 * (Long) value2;
                } else if (value2 instanceof Float) {
                    ret = (Double) value1 * (Float) value2;
                } else if (value2 instanceof Double) {
                    ret = (Double) value1 * (Double) value2;
                } else {
                    ret = 0.0d;
                }
            } else {
                return 0;
            }
        }
        return ret;
    }
}

Related

  1. multiply(int x, int y)
  2. multiply(Number a, Number b)
  3. multiply(Number a, Number b, Class cl)
  4. multiply(Number n1, Number n2)
  5. multiply(Number n1, Number n2)
  6. multiplyForLong(final Number operand1, final int operand2)
  7. multiplyLongs(Long a, Long b)
  8. multiplyNumbers(Number a, Number b)