Java Dictionary Usage getState(Dictionary conf, String property)

Here you can find the source of getState(Dictionary conf, String property)

Description

Getter for the state of a boolean type property.

License

Apache License

Parameter

Parameter Description
conf the configuration
property the property

Return

the state or null if the property was not present

Declaration

public static Boolean getState(Dictionary<String, Object> conf, String property) 

Method Source Code

//package com.java2s;
/*//from  w  ww .j  ava 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 java.util.Dictionary;

public class Main {
    /**
     * Getter for the state of a boolean type property. Supported configuration values are <ul>
     * <li><code>true/false</code> ({@link Boolean})
     * <li><code>0/1</code> ({@link Number})
     * <li><code>"0"/"1"</code> and <code>"true"/"false"</code> ({@link String})
     * </ul>
     * @param conf the configuration
     * @param property the property
     * @return the state or <code>null</code> if the property was not present
     */
    public static Boolean getState(Dictionary<String, Object> conf, String property) {
        Object value = conf.get(property);
        if (value instanceof String) {
            String strVal = ((String) value).trim();
            if ("0".equals(strVal)) {
                return false;
            } else if ("1".equals(strVal)) {
                return true;
            } else {
                return new Boolean(strVal);
            }
        } else if (value instanceof Number) {
            int state = ((Number) value).intValue();
            return state == 1 ? Boolean.TRUE : state == 0 ? Boolean.FALSE : null;
        } else if (value instanceof Boolean) {
            return (Boolean) value;
        } else {
            return null;
        }
    }
}

Related

  1. addReferenceIdsToServiceProperties(String prefix, Map referenceProps, Dictionary serviceProps)
  2. containsKey(Dictionary dict, String key)
  3. getBoolean(Dictionary p, Object key, Boolean defaultValue)
  4. getSpringContextHeader(Dictionary headers)
  5. getString(Dictionary p, Object key, String defaultValue)
  6. isNullOrNothing(final Object[] array)
  7. parseRequestOption(Dictionary conf, String confParam, Map requestOptions)
  8. setDefault(Dictionary dict, String key, String value)