Java Properties Get getPropertyLong(Properties properties, String context, String key, Long def)

Here you can find the source of getPropertyLong(Properties properties, String context, String key, Long def)

Description

Delegates to #getProperty(Properties,String,String,String) but returns the value as an Long where Long#valueOf(String) defines the actual value

License

Apache License

Exception

Parameter Description
RuntimeException if the property is not set and def is null

Return

the property as a under the given key

Declaration

public static Long getPropertyLong(Properties properties, String context, String key, Long def)
        throws RuntimeException 

Method Source Code


//package com.java2s;
/*/*from www .  j  a  v a  2 s.c  o  m*/
 * Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
 * 
 * Licensed 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 java.text.MessageFormat;
import java.util.Properties;

public class Main {
    /**
     * Delegates to {@link #getProperty(Properties, String, String, String)} but returns the value as an {@link Long}
     * where {@link Long#valueOf(String)} defines the actual value
     * 
     * @return the property as a {@link Long} under the given key
     * 
     * @throws RuntimeException
     *             if the property is not set and def is null
     */
    public static Long getPropertyLong(Properties properties, String context, String key, Long def)
            throws RuntimeException {
        return Long.valueOf(getProperty(properties, context, key, def == null ? null : def.toString()));
    }

    /**
     * Delegates to {@link #getProperty(String, String, String)} but returns the value as an {@link Long} where
     * {@link Long#valueOf(String)} defines the actual value
     * 
     * @return the property as a {@link Long} under the given key
     * 
     * @throws RuntimeException
     *             if the property is not set and def is null
     */
    public static Long getPropertyLong(String context, String key, Long def) throws RuntimeException {
        return Long.valueOf(getProperty(context, key, def == null ? null : def.toString()));
    }

    /**
     * Returns the property with the given key from the given {@link Properties}. If def is null, and the property is
     * not set, then a {@link RuntimeException} is thrown
     * 
     * @param properties
     *            the {@link Properties} from which to retrieve the property
     * @param context
     *            The context should be the name of the caller, so that stack trace gives enough detail as to which
     *            class expected the configuration property if no property was found
     * @param key
     *            the key of the property to return
     * @param def
     *            the default value, if null, then an exception will be thrown if the property is not set
     * 
     * @return the property under the given key
     * 
     * @throws RuntimeException
     *             if the property is not set and def is null
     */
    public static String getProperty(Properties properties, String context, String key, String def)
            throws RuntimeException {
        String property = properties.getProperty(key, def);
        if (property == null) {
            String msg = "[{0}] Property {1} is not set, and no default was given!"; //$NON-NLS-1$
            msg = MessageFormat.format(msg, context, key);
            throw new RuntimeException(msg);
        }

        return property;
    }

    /**
     * Returns the {@link System#getProperty(String)} with the given key. If def is null, and the property is not set,
     * then a {@link RuntimeException} is thrown
     * 
     * @param context
     *            The context should be the name of the caller, so that stack trace gives enough detail as to which
     *            class expected the configuration property if no property was found
     * @param key
     *            the key of the property to return
     * @param def
     *            the default value, if null, then an exception will be thrown if the property is not set
     * 
     * @return the property under the given key
     * 
     * @throws RuntimeException
     *             if the property is not set and def is null
     */
    public static String getProperty(String context, String key, String def) throws RuntimeException {
        return getProperty(System.getProperties(), context, key, def);
    }
}

Related

  1. getProperty(Properties props, String prefix, String key)
  2. getProperty(Properties props, String propertyName)
  3. getProperty(Properties props, String propname)
  4. getProperty(String[] system_props, Properties props, String prop_name, boolean ignore_sysprops, String default_value)
  5. getPropertyDouble(Properties p, String key)
  6. getPropertyString(Properties properties, String propertyName)
  7. getPropertyText(String property, Object... replacements)
  8. getPropertyValue(Properties props, String key)
  9. getPropertyValueAsInt(Properties props, String key, int defaultValue)