001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.util.Iterator;
020import java.util.LinkedHashMap;
021import java.util.Map;
022import java.util.Objects;
023import java.util.Properties;
024
025public final class PropertiesHelper {
026
027    private PropertiesHelper() {
028    }
029
030    public static Map<String, Object> extractProperties(Map<String, Object> properties, String optionPrefix) {
031        return extractProperties(properties, optionPrefix, true);
032    }
033
034    public static Map<String, Object> extractProperties(Map<String, Object> properties, String optionPrefix, boolean remove) {
035        if (properties == null) {
036            return new LinkedHashMap<>(0);
037        }
038        Map<String, Object> rc = new LinkedHashMap<>(properties.size());
039
040        for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) {
041            Map.Entry<String, Object> entry = it.next();
042            String name = entry.getKey();
043            if (name.startsWith(optionPrefix)) {
044                Object value = properties.get(name);
045                name = name.substring(optionPrefix.length());
046                rc.put(name, value);
047
048                if (remove) {
049                    it.remove();
050                }
051            }
052        }
053
054        return rc;
055    }
056
057    @Deprecated
058    public static Map<String, String> extractStringProperties(Map<String, Object> properties) {
059        Map<String, String> rc = new LinkedHashMap<>(properties.size());
060
061        for (Map.Entry<String, Object> entry : properties.entrySet()) {
062            String name = entry.getKey();
063            String value = entry.getValue().toString();
064            rc.put(name, value);
065        }
066
067        return rc;
068    }
069
070    public static boolean hasProperties(Map<String, Object> properties, String optionPrefix) {
071        ObjectHelper.notNull(properties, "properties");
072
073        if (ObjectHelper.isNotEmpty(optionPrefix)) {
074            for (Object o : properties.keySet()) {
075                String name = (String) o;
076                if (name.startsWith(optionPrefix)) {
077                    return true;
078                }
079            }
080            // no parameters with this prefix
081            return false;
082        } else {
083            return !properties.isEmpty();
084        }
085    }
086
087    public static Properties asProperties(String... properties) {
088        if ((properties.length & 1) != 0) {
089            throw new InternalError("length is odd");
090        }
091
092        Properties answer = new Properties();
093        for (int i = 0; i < properties.length; i += 2) {
094            answer.setProperty(
095                    Objects.requireNonNull(properties[i]),
096                    Objects.requireNonNull(properties[i + 1]));
097        }
098
099        return answer;
100    }
101
102    public static Properties asProperties(Map<String, Object> properties) {
103        Properties answer = new Properties();
104        answer.putAll(properties);
105
106        return answer;
107    }
108
109}