All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sling.commons.osgi.PropertiesUtil Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * 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.
 */
package org.apache.sling.commons.osgi;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * The PropertiesUtil is a utility class providing some
 * useful utility methods for converting property types.
 *
 * @since 2.1
 * @deprecated Use {@code org.osgi.util.converter.Converters} instead
 */
@Deprecated
public class PropertiesUtil {

    /**
     * Returns the boolean value of the parameter or the
     * defaultValue if the parameter is null.
     * If the parameter is not a Boolean it is converted
     * by calling Boolean.valueOf on the string value of the
     * object.
     * @param propValue the property value or null
     * @param defaultValue the default boolean value
     * @return Boolean value
     */
    public static boolean toBoolean(Object propValue, boolean defaultValue) {
        propValue = toObject(propValue);
        if (propValue instanceof Boolean) {
            return (Boolean) propValue;
        } else if (propValue != null) {
            return Boolean.parseBoolean(String.valueOf(propValue));
        }

        return defaultValue;
    }

    /**
     * Returns the parameter as a string or the
     * defaultValue if the parameter is null.
     * @param propValue the property value or null
     * @param defaultValue the default string value
     * @return String value
     */
    public static String toString(Object propValue, String defaultValue) {
        propValue = toObject(propValue);
        return (propValue != null) ? propValue.toString() : defaultValue;
    }

    /**
     * Returns the parameter as a long or the
     * defaultValue if the parameter is null or if
     * the parameter is not a Long and cannot be converted to
     * a Long from the parameter's string value.
     * @param propValue the property value or null
     * @param defaultValue the default long value
     * @return Long value
     */
    public static long toLong(Object propValue, long defaultValue) {
        propValue = toObject(propValue);
        if (propValue instanceof Long) {
            return (Long) propValue;
        } else if (propValue != null) {
            try {
                return Long.parseLong(String.valueOf(propValue));
            } catch (NumberFormatException nfe) {
                // don't care, fall through to default value
            }
        }

        return defaultValue;
    }

    /**
     * Returns the parameter as an integer or the
     * defaultValue if the parameter is null or if
     * the parameter is not an Integer and cannot be converted to
     * an Integer from the parameter's string value.
     * @param propValue the property value or null
     * @param defaultValue the default integer value
     * @return Integer value
     */
    public static int toInteger(Object propValue, int defaultValue) {
        propValue = toObject(propValue);
        if (propValue instanceof Integer) {
            return (Integer) propValue;
        } else if (propValue != null) {
            try {
                return Integer.parseInt(String.valueOf(propValue));
            } catch (NumberFormatException nfe) {
                // don't care, fall through to default value
            }
        }

        return defaultValue;
    }

    /**
     * Returns the parameter as a double or the
     * defaultValue if the parameter is null or if
     * the parameter is not a Double and cannot be converted to
     * a Double from the parameter's string value.
     * @param propValue the property value or null
     * @param defaultValue the default double value
     * @return Double value
     */
    public static double toDouble(Object propValue, double defaultValue) {
        propValue = toObject(propValue);
        if (propValue instanceof Double) {
            return (Double) propValue;
        } else if (propValue != null) {
            try {
                return Double.parseDouble(String.valueOf(propValue));
            } catch (NumberFormatException nfe) {
                // don't care, fall through to default value
            }
        }

        return defaultValue;
    }

    /**
     * Returns the parameter as a single value. If the
     * parameter is neither an array nor a java.util.Collection the
     * parameter is returned unmodified. If the parameter is a non-empty array,
     * the first array element is returned. If the property is a non-empty
     * java.util.Collection, the first collection element is returned.
     * Otherwise null is returned.
     * @param propValue the parameter to convert.
     * @return Object value
     */
    public static Object toObject(Object propValue) {
        if (propValue == null) {
            return null;
        } else if (propValue.getClass().isArray()) {
            Object[] prop = (Object[]) propValue;
            return prop.length > 0 ? prop[0] : null;
        } else if (propValue instanceof Collection) {
            Collection prop = (Collection) propValue;
            return prop.isEmpty() ? null : prop.iterator().next();
        } else {
            return propValue;
        }
    }

    /**
     * Returns the parameter as an array of Strings. If
     * the parameter is a scalar value its string value is returned as a single
     * element array. If the parameter is an array, the elements are converted to
     * String objects and returned as an array. If the parameter is a collection, the
     * collection elements are converted to String objects and returned as an array.
     * Otherwise (if the parameter is null) null is
     * returned.
     * @param propValue The object to convert.
     * @return String array value
     */
    public static String[] toStringArray(Object propValue) {
        return toStringArray(propValue, null);
    }

    /**
     * Returns the parameter as an array of Strings. If
     * the parameter is a scalar value its string value is returned as a single
     * element array. If the parameter is an array, the elements are converted to
     * String objects and returned as an array. If the parameter is a collection, the
     * collection elements are converted to String objects and returned as an array.
     * Otherwise (if the property is null) a provided default value is
     * returned.
     * @param propValue The object to convert.
     * @param defaultArray The default array to return.
     * @return String array value
     */
    public static String[] toStringArray(Object propValue, String[] defaultArray) {
        if (propValue == null) {
            // no value at all
            return defaultArray;

        } else if (propValue instanceof String) {
            // single string
            return new String[] { (String) propValue };

        } else if (propValue instanceof String[]) {
            // String[]
            return (String[]) propValue;

        } else if (propValue.getClass().isArray()) {
            // other array
            Object[] valueArray = (Object[]) propValue;
            List values = new ArrayList(valueArray.length);
            for (Object value : valueArray) {
                if (value != null) {
                    values.add(value.toString());
                }
            }
            return values.toArray(new String[values.size()]);

        } else if (propValue instanceof Collection) {
            // collection
            Collection valueCollection = (Collection) propValue;
            List valueList = new ArrayList(valueCollection.size());
            for (Object value : valueCollection) {
                if (value != null) {
                    valueList.add(value.toString());
                }
            }
            return valueList.toArray(new String[valueList.size()]);
        }

        return defaultArray;
    }

    /**
     * Returns the parameter as a map with string keys and string values.
     *
     * The parameter is considered as a collection whose entries are of the form
     * key=value. The conversion has following rules
     * 
    *
  • Entries are of the form key=value
  • *
  • key is trimmed
  • *
  • value is trimmed. If a trimmed value results in an empty string it is treated as null
  • *
  • Malformed entries like 'foo','foo=' are ignored
  • *
  • Map entries maintain the input order
  • *
* * Otherwise (if the property is null) a provided default value is * returned. * @param propValue The object to convert. * @param defaultArray The default array converted to map. * @return Map value */ public static Map toMap(Object propValue, String[] defaultArray) { String[] arrayValue = toStringArray(propValue, defaultArray); if (arrayValue == null) { return null; } //in property values Map result = new LinkedHashMap(); for (String kv : arrayValue) { int indexOfEqual = kv.indexOf('='); if (indexOfEqual > 0) { String key = trimToNull(kv.substring(0, indexOfEqual)); String value = trimToNull(kv.substring(indexOfEqual + 1)); if (key != null) { result.put(key, value); } } } return result; } private static String trimToNull(String str) { String ts = trim(str); return isEmpty(ts) ? null : ts; } private static String trim(String str){ return str == null ? null : str.trim(); } private static boolean isEmpty(String str){ return str == null || str.length() == 0; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy