Java Integer Create toInteger(Object x)

Here you can find the source of toInteger(Object x)

Description

to Integer

License

Open Source License

Declaration

static Integer toInteger(Object x) 

Method Source Code

//package com.java2s;
/**//from   w  w  w .j a  v  a  2s. c  o  m
 * Copyright (c) 2012, Thilo Planz. All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * as published by the Apache Software Foundation (the "License").
 *
 * 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.
 *
 * You should have received a copy of the License along with this program.
 * If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
 */

public class Main {
    static Integer toInteger(Object x) {
        if (x == null)
            return null;
        if (x instanceof Integer)
            return (Integer) x;
        if (x instanceof Long)
            return Integer.valueOf(x.toString());
        if (x instanceof String)
            return Integer.valueOf((String) x);
        throw new IllegalArgumentException("cannot convert `" + x + "` into a Long");
    }

    static String toString(Object x) {
        if (x == null)
            return null;
        if (x instanceof String)
            return (String) x;
        if (x instanceof Number)
            return x.toString();

        throw new IllegalArgumentException("cannot convert `" + x + "` into a String");
    }
}

Related

  1. toInteger(Object value)
  2. toInteger(Object value)
  3. toInteger(Object value)
  4. toInteger(Object value)
  5. toInteger(Object value, int defaultValue)
  6. toInteger(String ascii)
  7. toInteger(String integer)
  8. toInteger(String numeric)
  9. toInteger(String numericString)