Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**
     * Treat a variable as an integer in JavaScript style. Note this function can
     * only handle integer and boolean currently.
     *
     * @param var the variable to get value from
     * @return the integer value
     * @throws IllegalArgumentException the data type of {@code var} is neither
     * integer nor boolean.
     */
    public static Integer getVariableValueAsInteger(Object var) {
        if (var == null) {
            throw new NullPointerException("argument 'var' cannot be null");
        }

        Integer returnResult = -1;

        if (var instanceof Integer) {
            returnResult = (Integer) var;
        } else if (var instanceof Boolean) {
            // Javascript treat true as 1
            returnResult = (Boolean) var ? 1 : 0;
        } else {
            throw new IllegalArgumentException("'var' is neither integer nor boolean");
        }

        return returnResult;
    }
}