Java slf4j Logger getLong(Properties config, String key, long defaultValue)

Here you can find the source of getLong(Properties config, String key, long defaultValue)

Description

Get long from properties.

License

Apache License

Parameter

Parameter Description
config Properties
key key in Properties
defaultValue default value if value is not set

Exception

Parameter Description
IllegalArgumentException an exception

Return

default or value of key

Declaration

public static long getLong(Properties config, String key, long defaultValue) 

Method Source Code


//package com.java2s;
/*/* w  w w  . j a v  a 2s .  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.
 */

import org.slf4j.Logger;
import java.util.Properties;

public class Main {
    /**
     * Get long from properties.
     * This method throws an exception if the long is not valid.
     *
     * @param config Properties
     * @param key key in Properties
     * @param defaultValue default value if value is not set
     * @return default or value of key
     * @throws IllegalArgumentException
     */
    public static long getLong(Properties config, String key, long defaultValue) {
        String val = config.getProperty(key);
        if (val == null) {
            return defaultValue;
        } else {
            try {
                return Long.parseLong(val);
            } catch (NumberFormatException nfe) {
                throw new IllegalArgumentException(
                        "Value for configuration key='" + key + "' is not set correctly. " + "Entered value='" + val
                                + "'. Default value='" + defaultValue + "'");
            }
        }
    }

    /**
     * Get long from properties.
     * This method only logs if the long is not valid.
     *
     * @param config Properties
     * @param key key in Properties
     * @param defaultValue default value if value is not set
     * @return default or value of key
     * @throws IllegalArgumentException
     */
    public static long getLong(Properties config, String key, long defaultValue, Logger logger) {
        try {
            return getLong(config, key, defaultValue);
        } catch (IllegalArgumentException iae) {
            logger.warn(iae.getMessage());
            return defaultValue;
        }
    }
}

Related

  1. getLogger(Class clazz)
  2. getLogger(String className)
  3. getLoggerForCurrentClass()
  4. getLoggerInstance(String callerCLass)
  5. getLoggerLevel(Logger logger)
  6. getMessage(String message, Object... args)
  7. getTraceLevel(Logger logger)
  8. ic static void i(String fmt, Object... args)
  9. info(Class clz, String logStr)