001 /* 002 * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved. 003 * 004 * Project and contact information: http://www.cascading.org/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021 package cascading.property; 022 023 import java.util.HashMap; 024 import java.util.HashSet; 025 import java.util.Map; 026 import java.util.Properties; 027 import java.util.Set; 028 029 /** Class PropertyUtil is a static helper class for handling properties. */ 030 public class PropertyUtil 031 { 032 public static String getProperty( Map<Object, Object> properties, String key ) 033 { 034 return getProperty( properties, key, (String) null ); 035 } 036 037 public static String getStringProperty( Properties defaultProperties, Map<Object, Object> properties, String property ) 038 { 039 return getProperty( properties, property, defaultProperties.getProperty( property ) ); 040 } 041 042 public static <A> A getProperty( Map<Object, Object> properties, String key, A defaultValue ) 043 { 044 if( properties == null ) 045 return defaultValue; 046 047 A value; 048 049 if( properties instanceof Properties ) 050 value = (A) ( (Properties) properties ).getProperty( key ); 051 else 052 value = (A) properties.get( key ); 053 054 return value == null ? defaultValue : value; 055 } 056 057 public static void setProperty( Map<Object, Object> properties, String key, String value ) 058 { 059 if( properties == null || value == null || value.isEmpty() ) 060 return; 061 062 if( properties instanceof Properties ) 063 ( (Properties) properties ).setProperty( key, value ); 064 else 065 properties.put( key, value ); 066 } 067 068 public static Properties createProperties( Iterable<Map.Entry<String, String>> defaultProperties ) 069 { 070 Properties properties = new Properties(); 071 072 for( Map.Entry<String, String> property : defaultProperties ) 073 properties.setProperty( property.getKey(), property.getValue() ); 074 075 return properties; 076 } 077 078 public static Properties createProperties( Map<Object, Object> properties, Properties defaultProperties ) 079 { 080 Properties results = defaultProperties == null ? new Properties() : new Properties( defaultProperties ); 081 082 if( properties == null ) 083 return results; 084 085 Set<Object> keys = new HashSet<Object>( properties.keySet() ); 086 087 // keys will only be grabbed if both key/value are String, so keep orig keys 088 if( properties instanceof Properties ) 089 keys.addAll( ( (Properties) properties ).stringPropertyNames() ); 090 091 for( Object key : keys ) 092 { 093 Object value = properties.get( key ); 094 095 if( value == null && properties instanceof Properties && key instanceof String ) 096 value = ( (Properties) properties ).getProperty( (String) key ); 097 098 if( value == null ) // don't stuff null values 099 continue; 100 101 // don't let these objects pass, even though toString is called below. 102 if( value instanceof Class ) 103 continue; 104 105 results.setProperty( key.toString(), value.toString() ); 106 } 107 108 return results; 109 } 110 111 public static Map<Object, Object> asFlatMap( Map<Object, Object> properties ) 112 { 113 if( !( properties instanceof Properties ) ) 114 return properties; 115 116 Map<Object, Object> map = new HashMap<Object, Object>(); 117 Properties props = (Properties) properties; 118 119 for( String property : props.stringPropertyNames() ) 120 map.put( property, props.getProperty( property ) ); 121 122 return map; 123 } 124 }