1 package org.coolreader.crengine; 2 3 import android.util.Log; 4 5 import java.util.HashSet; 6 import java.util.Map; 7 import java.util.Set; 8 9 @SuppressWarnings("serial") 10 public class Properties extends java.util.Properties { 11 public static final Logger elog = L.create("props", Log.ERROR); 12 Properties()13 public Properties() { 14 super(); 15 } 16 setAll(java.util.Properties props)17 synchronized public void setAll(java.util.Properties props) { 18 for (Map.Entry<Object, Object> entry : props.entrySet()) { 19 setProperty((String) entry.getKey(), (String) entry.getValue()); 20 } 21 } 22 Properties(java.util.Properties props)23 public Properties(java.util.Properties props) { 24 synchronized(props) { 25 setAll(props); 26 } 27 } 28 revBytes(int color)29 private static int revBytes(int color) { 30 return color & 0xFFFFFF; 31 // return ((color & 0xFF)<<16)|((color & 0xFF00)<<0)|((color & 32 // 0xFF0000)>>16); 33 } 34 setColor(String key, int color)35 public void setColor(String key, int color) { 36 color &= 0xFFFFFF; 37 color = revBytes(color); 38 String value = Integer.toHexString(color); 39 while (value.length() < 6) 40 value = "0" + value; 41 value = "0x" + value; 42 setProperty(key, value); 43 } 44 getColor(String key, int defColor)45 public int getColor(String key, int defColor) { 46 defColor = revBytes(defColor); 47 String value = getProperty(key); 48 try { 49 if (value != null && value.length() > 2 && value.startsWith("0x")) { 50 int cl = Integer.parseInt(value.substring(2), 16); 51 cl = revBytes(cl); 52 return cl | 0xFF000000; 53 } 54 if (value != null && value.length() > 1 && value.startsWith("#")) { 55 int cl = Integer.parseInt(value.substring(1), 16); 56 cl = revBytes(cl); 57 return cl | 0xFF000000; 58 } 59 } catch (Exception e) { 60 } 61 return revBytes(defColor) | 0xFF000000; 62 } 63 setInt(String key, int v)64 public void setInt(String key, int v) { 65 String value = String.valueOf(v); 66 setProperty(key, value); 67 } 68 getInt(String key, int def)69 public int getInt(String key, int def) { 70 String value = getProperty(key); 71 int res = def; 72 try { 73 if (value != null) 74 res = Integer.valueOf(value); 75 } catch (Exception e) { 76 } 77 return res; 78 } 79 setLong(String key, Long v)80 public void setLong(String key, Long v) { 81 String value = String.valueOf(v); 82 setProperty(key, value); 83 } 84 getLong(String key, long def)85 public long getLong(String key, long def) { 86 String value = getProperty(key); 87 long res = def; 88 try { 89 if (value != null) 90 res = Long.parseLong(value); 91 } catch (Exception e) { 92 } 93 return res; 94 } 95 setBool(String key, boolean value)96 public void setBool(String key, boolean value) { 97 setProperty(key, value ? "1" : "0"); 98 } 99 getBool(String key, boolean defaultValue)100 public boolean getBool(String key, boolean defaultValue) { 101 String value = getProperty(key); 102 if (value == null) 103 return defaultValue; 104 if (value.equals("1") || value.equals("true") || value.equals("yes")) 105 return true; 106 if (value.equals("0") || value.equals("false") || value.equals("no")) 107 return false; 108 return defaultValue; 109 } 110 applyDefault(String prop, String defValue)111 public void applyDefault(String prop, String defValue) { 112 if (getProperty(prop) == null) 113 setProperty(prop, defValue); 114 } 115 applyDefault(String prop, int defValue)116 public void applyDefault(String prop, int defValue) { 117 if (getProperty(prop) == null) 118 setInt(prop, defValue); 119 } 120 eq(Object obj1, Object obj2)121 public static boolean eq(Object obj1, Object obj2) { 122 if (obj1 == null && obj2 == null) 123 return true; 124 if (obj1 == null || obj2 == null) 125 return false; 126 return obj1.equals(obj2); 127 } 128 diff(Properties oldValue)129 synchronized public Properties diff(Properties oldValue) { 130 Properties res = new Properties(); 131 for (Map.Entry<Object, Object> entry : entrySet()) { 132 if (!oldValue.containsKey(entry.getKey()) 133 || !eq(entry.getValue(), oldValue.get(entry.getKey()))) { 134 res.setProperty((String) entry.getKey(), 135 (String) entry.getValue()); 136 } 137 } 138 return res; 139 } 140 141 @Override getProperty(String name, String defaultValue)142 synchronized public String getProperty(String name, String defaultValue) { 143 return super.getProperty(name, defaultValue); 144 } 145 146 @Override getProperty(String name)147 synchronized public String getProperty(String name) { 148 return super.getProperty(name); 149 } 150 151 @Override setProperty(String name, String value)152 synchronized public Object setProperty(String name, String value) { 153 try { 154 return super.setProperty(name, value); 155 } catch (NullPointerException e) { 156 elog.e("Properties.setProperty(): value can't be null, name=" + name); 157 return null; 158 } 159 } 160 161 @Override remove(Object key)162 public synchronized Object remove(Object key) { 163 return super.remove(key); 164 } 165 166 @Override entrySet()167 public synchronized Set<java.util.Map.Entry<Object, Object>> entrySet() { 168 Set<java.util.Map.Entry<Object, Object>> res = new HashSet<java.util.Map.Entry<Object, Object>>(); 169 res.addAll(super.entrySet()); 170 return res; 171 } 172 } 173