Java Long Number Create toLong(Object value, long defaultValue)

Here you can find the source of toLong(Object value, long defaultValue)

Description

Converts a generic object value to a long if possible:
  • If value is null the default is returned
  • If value is a Number then its Number#longValue() is returned
  • Otherwise, the value's #toString() is parsed as a long

License

Apache License

Parameter

Parameter Description
value The resolved value - may be null
defaultValue The default to use if null resolved value

Exception

Parameter Description
NumberFormatException if malformed value

Return

The resolved value

Declaration

public static long toLong(Object value, long defaultValue) 

Method Source Code

//package com.java2s;
/*//from   w  ww.j  av  a  2  s . co  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 {
    /**
     * Converts a generic object value to a {@code long} if possible:
     * <UL>
     *      <LI>
     *      If value is {@code null} the default is returned
     *      </LI>
     *
     *      <LI>
     *      If value is a {@link Number} then its {@link Number#longValue()} is returned
     *      </LI>
     *
     *      <LI>
     *      Otherwise, the value's {@link #toString()} is parsed as a {@code long}
     *      </LI>
     * </UL>
     *
     * @param value         The resolved value - may be {@code null}
     * @param defaultValue  The default to use if {@code null} resolved value
     * @return The resolved value
     * @throws NumberFormatException if malformed value
     * @see Long#parseLong(String)
     */
    public static long toLong(Object value, long defaultValue) {
        if (value == null) {
            return defaultValue;
        } else if (value instanceof Number) {
            return ((Number) value).longValue();
        } else { // we parse the string in case it is not a valid long value
            return Long.parseLong(value.toString());
        }
    }

    /**
     * Converts a generic object into a {@link Long}:
     * <UL>
     *      <LI>
     *      If the value is {@code null} then returns {@code null}.
     *      </LI>
     *
     *      <LI>
     *      If the value is already a {@link Long} then it is returned as such.
     *      </LI>
        
     *      <LI>
     *      If value is a {@link Number} then its {@link Number#longValue()} is
     *      wrapped as a {@link Long}
     *      </LI>
     *
     *      <LI>
     *      Otherwise, the value's {@link #toString()} is parsed as a {@link Long}
     *      </LI>
     * </UL>
     *
     * @param value The resolved value - may be {@code null}
     * @return The {@link Long} value or {@code null} if property not found
     * @throws NumberFormatException if malformed value
     * @see Long#valueOf(long)
     * @see Long#valueOf(String)
     */
    public static Long toLong(Object value) {
        if (value == null) {
            return null;
        } else if (value instanceof Long) {
            return (Long) value;
        } else if (value instanceof Number) {
            return ((Number) value).longValue();
        } else { // we parse the string in case it is not a valid long value
            return Long.valueOf(value.toString());
        }
    }
}

Related

  1. toLong(Object value)
  2. toLong(Object value)
  3. toLong(Object value)
  4. toLong(Object value)
  5. toLong(Object value)
  6. toLong(Object x)
  7. toLong(short[] arr)
  8. toLong(String input, int radix, long defaultValue)
  9. toLong(String numeric)