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
039        return doExtractProperties(properties, optionPrefix, remove);
040    }
041
042    static Map<String, Object> doExtractProperties(Map<String, Object> properties, String optionPrefix, boolean remove) {
043        Map<String, Object> rc = new LinkedHashMap<>(properties.size());
044
045        for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) {
046            Map.Entry<String, Object> entry = it.next();
047            String name = entry.getKey();
048            if (name.startsWith(optionPrefix)) {
049                Object value = properties.get(name);
050                name = name.substring(optionPrefix.length());
051                rc.put(name, value);
052
053                if (remove) {
054                    it.remove();
055                }
056            }
057        }
058
059        return rc;
060    }
061
062    public static boolean hasProperties(Map<String, Object> properties, String optionPrefix) {
063        ObjectHelper.notNull(properties, "properties");
064
065        if (ObjectHelper.isNotEmpty(optionPrefix)) {
066            for (Object o : properties.keySet()) {
067                String name = (String) o;
068                if (name.startsWith(optionPrefix)) {
069                    return true;
070                }
071            }
072            // no parameters with this prefix
073            return false;
074        } else {
075            return !properties.isEmpty();
076        }
077    }
078
079    public static Properties asProperties(String... properties) {
080        if ((properties.length & 1) != 0) {
081            throw new InternalError("length is odd");
082        }
083
084        Properties answer = new Properties();
085        for (int i = 0; i < properties.length; i += 2) {
086            answer.setProperty(
087                    Objects.requireNonNull(properties[i]),
088                    Objects.requireNonNull(properties[i + 1]));
089        }
090
091        return answer;
092    }
093
094    public static Properties asProperties(Map<String, Object> properties) {
095        Properties answer = new Properties();
096        answer.putAll(properties);
097
098        return answer;
099    }
100
101}