1 9 package org.jboss.portal.server.util; 10 11 import org.jboss.portal.common.util.Tools; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Collections ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 21 27 public class Properties 28 { 29 30 private Map content; 31 32 public Properties() 33 { 34 } 35 36 41 public void addProperty(String name, String value) 42 { 43 if (name == null) 44 { 45 throw new IllegalArgumentException ("Name cannot be null"); 46 } 47 if (value == null) 48 { 49 throw new IllegalArgumentException ("Value cannot be null"); 50 } 51 if (content == null) 52 { 53 content = new HashMap (); 54 } 55 Object o = content.get(name); 56 if (o == null) 57 { 58 content.put(name, value); 59 } 60 else if (o instanceof String ) 61 { 62 ArrayList values = new ArrayList (); 63 values.add(o); 64 values.add(value); 65 content.put(name, values); 66 } 67 else 68 { 69 ArrayList values = (ArrayList )o; 70 values.add(value); 71 } 72 } 73 74 79 public void setProperty(String name, String value) 80 { 81 if (name == null) 82 { 83 throw new IllegalArgumentException ("Name cannot be null"); 84 } 85 if (value == null) 86 { 87 throw new IllegalArgumentException ("Value cannot be null"); 88 } 89 if (content == null) 90 { 91 content = new HashMap (); 92 } 93 content.put(name, value); 94 } 95 96 99 public void clear() 100 { 101 if (content != null) 102 { 103 content.clear(); 104 } 105 } 106 107 public String getProperty(String name) 108 { 109 if (name == null) 110 { 111 throw new IllegalArgumentException ("Name cannot be null"); 112 } 113 if (content == null) 114 { 115 return null; 116 } 117 Object o = content.get(name); 118 if (o == null) 119 { 120 return null; 121 } 122 else if (o instanceof String ) 123 { 124 return (String )o; 125 } 126 else 127 { 128 return (String )((ArrayList )o).get(0); 129 } 130 } 131 132 137 public List getProperties(String name) 138 { 139 if (name == null) 140 { 141 throw new IllegalArgumentException ("Name cannot be null"); 142 } 143 if (content == null) 144 { 145 return Collections.EMPTY_LIST; 146 } 147 Object o = content.get(name); 148 if (o == null) 149 { 150 return Collections.EMPTY_LIST; 151 } 152 if (o instanceof String ) 153 { 154 return Collections.singletonList(o); 155 } 156 else 157 { 158 return (List )o; 159 } 160 } 161 162 public Set getPropertyNames() 163 { 164 if (content == null) 165 { 166 return Collections.EMPTY_SET; 167 } 168 return content.keySet(); 169 } 170 } 171 | Popular Tags |