1 21 22 package org.armedbear.j; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 public final class PropertyList 29 { 30 private HashMap map; 31 32 public PropertyList() 33 { 34 } 35 36 public Object getProperty(Property property) 37 { 38 if (map != null) 39 return map.get(property); 40 return null; 41 } 42 43 public void setProperty(Property property, Object value) 44 { 45 if (map == null) 46 map = new HashMap (); 47 map.put(property, value); 48 } 49 50 public void setProperty(Property property, boolean value) 51 { 52 setProperty(property, value ? Boolean.TRUE : Boolean.FALSE); 53 } 54 55 public void setProperty(Property property, int value) 56 { 57 setProperty(property, new Integer (value)); 58 } 59 60 public boolean setPropertyFromString(Property property, String value) 61 { 62 if (property.isBooleanProperty()) { 63 if (value.equals("true") || value.equals("1")) { 64 setProperty(property, true); 65 return true; 66 } 67 if (value.equals("false") || value.equals("0")) { 68 setProperty(property, false); 69 return true; 70 } 71 return false; } 73 if (property.isIntegerProperty()) { 74 try { 75 setProperty(property, Integer.parseInt(value)); 76 return true; 77 } 78 catch (NumberFormatException e) { 79 return false; } 81 } 82 setProperty(property, value); 83 return true; 84 } 85 86 public boolean removeProperty(Property property) 87 { 88 return map.remove(property) != null; 89 } 90 91 public boolean getBooleanProperty(Property property) 92 { 93 Object value = getProperty(property); 94 if (!(value instanceof Boolean )) 95 value = property.getDefaultValue(); 96 return ((Boolean )value).booleanValue(); 97 } 98 99 public int getIntegerProperty(Property property) 100 { 101 Object value = getProperty(property); 102 if (!(value instanceof Integer )) 103 value = property.getDefaultValue(); 104 return ((Integer )value).intValue(); 105 } 106 107 public String getStringProperty(Property property) 108 { 109 Object value = getProperty(property); 110 if (!(value instanceof String )) 111 value = property.getDefaultValue(); 112 return (String ) value; 113 } 114 115 public Iterator keyIterator() 116 { 117 if (map != null) 118 return map.keySet().iterator(); 119 return null; 120 } 121 122 public Set keySet() 123 { 124 if (map != null) 125 return map.keySet(); 126 return null; 127 } 128 129 public int size() 130 { 131 return map != null ? map.size() : 0; 132 } 133 134 public void putAll(PropertyList other) 135 { 136 if (other.map != null && other.map.size() > 0) { 137 if (map == null) 138 map = new HashMap (); 139 map.putAll(other.map); 140 } 141 } 142 } 143 | Popular Tags |