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