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; 022 023public final class PropertiesHelper { 024 025 private PropertiesHelper() { 026 } 027 028 public static Map<String, Object> extractProperties(Map<String, Object> properties, String optionPrefix) { 029 return extractProperties(properties, optionPrefix, true); 030 } 031 032 public static Map<String, Object> extractProperties(Map<String, Object> properties, String optionPrefix, boolean remove) { 033 ObjectHelper.notNull(properties, "properties"); 034 035 Map<String, Object> rc = new LinkedHashMap<>(properties.size()); 036 037 for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) { 038 Map.Entry<String, Object> entry = it.next(); 039 String name = entry.getKey(); 040 if (name.startsWith(optionPrefix)) { 041 Object value = properties.get(name); 042 name = name.substring(optionPrefix.length()); 043 rc.put(name, value); 044 045 if (remove) { 046 it.remove(); 047 } 048 } 049 } 050 051 return rc; 052 } 053 054 @Deprecated 055 public static Map<String, String> extractStringProperties(Map<String, Object> properties) { 056 ObjectHelper.notNull(properties, "properties"); 057 058 Map<String, String> rc = new LinkedHashMap<>(properties.size()); 059 060 for (Map.Entry<String, Object> entry : properties.entrySet()) { 061 String name = entry.getKey(); 062 String value = entry.getValue().toString(); 063 rc.put(name, value); 064 } 065 066 return rc; 067 } 068 069 public static boolean hasProperties(Map<String, Object> properties, String optionPrefix) { 070 ObjectHelper.notNull(properties, "properties"); 071 072 if (ObjectHelper.isNotEmpty(optionPrefix)) { 073 for (Object o : properties.keySet()) { 074 String name = (String) o; 075 if (name.startsWith(optionPrefix)) { 076 return true; 077 } 078 } 079 // no parameters with this prefix 080 return false; 081 } else { 082 return !properties.isEmpty(); 083 } 084 } 085 086}