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 public static boolean hasProperties(Map<String, Object> properties, String optionPrefix) { 058 ObjectHelper.notNull(properties, "properties"); 059 060 if (ObjectHelper.isNotEmpty(optionPrefix)) { 061 for (Object o : properties.keySet()) { 062 String name = (String) o; 063 if (name.startsWith(optionPrefix)) { 064 return true; 065 } 066 } 067 // no parameters with this prefix 068 return false; 069 } else { 070 return !properties.isEmpty(); 071 } 072 } 073 074 public static Properties asProperties(String... properties) { 075 if ((properties.length & 1) != 0) { 076 throw new InternalError("length is odd"); 077 } 078 079 Properties answer = new Properties(); 080 for (int i = 0; i < properties.length; i += 2) { 081 answer.setProperty( 082 Objects.requireNonNull(properties[i]), 083 Objects.requireNonNull(properties[i + 1])); 084 } 085 086 return answer; 087 } 088 089 public static Properties asProperties(Map<String, Object> properties) { 090 Properties answer = new Properties(); 091 answer.putAll(properties); 092 093 return answer; 094 } 095 096}