Here you can find the source of toLong(Object value, long defaultValue)
Parameter | Description |
---|---|
value | The resolved value - may be null |
defaultValue | The default to use if null resolved value |
Parameter | Description |
---|---|
NumberFormatException | if malformed value |
public static long toLong(Object value, long defaultValue)
//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()); } } }