View Javadoc
1 /* 2 * Copyright (c) 2003 Scott Howlett & Paul Libbrecht. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. The end-user documentation included with the redistribution, 18 * if any, must include the following acknowledgment: 19 * "This product includes software developed by the Xwing 20 * Project ( http://xwing.sourceforge.net/ )." 21 * Alternately, this acknowledgment may appear in the software itself, 22 * if and wherever such third-party acknowledgments normally appear. 23 * 24 * 4. The name "Xwing" must not be used to endorse or promote products 25 * derived from this software without prior written permission. For 26 * written permission, please contact the project authors via 27 * the Xwing project site, http://xwing.sourceforge.net/ . 28 * 29 * 5. Products derived from this software may not be called "Xwing", 30 * nor may "Xwing" appear in their name, without prior written 31 * permission of the Xwing project. 32 * 33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 36 * DISCLAIMED. IN NO EVENT SHALL THE XWING AUTHORS OR THE PROJECT 37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 * 46 * For more information on Xwing, please see 47 * < http://xwing.sourceforge.net/ >. 48 */ 49 50 package net.sourceforge.xwing.prefs; 51 52 import java.util.HashMap; 53 import java.util.Map; 54 import java.util.prefs.Preferences; 55 56 public class Prefs { 57 58 private Map prefTypes; 59 private Preferences node; 60 61 public Prefs(Class clazz, boolean system) { 62 this( 63 system 64 ? Preferences.systemNodeForPackage(clazz) 65 : Preferences.userNodeForPackage(clazz)); 66 } 67 68 public Prefs(String path, boolean system) { 69 this( 70 (system ? Preferences.systemRoot() : Preferences.userRoot()).node( 71 path)); 72 } 73 74 public Prefs(Preferences node) { 75 this.node = node; 76 prefTypes = new HashMap(); 77 } 78 79 public void add(String key, Object def) { 80 if (key == null) { 81 throw new NullPointerException("key must not be null"); 82 } 83 if (def == null) { 84 throw new NullPointerException("def must not be null"); 85 } 86 87 if (!(def instanceof String 88 || def instanceof Boolean 89 || def instanceof Integer 90 || def instanceof Long 91 || def instanceof Float 92 || def instanceof Double 93 || def.getClass() == byte[].class)) { 94 throw new IllegalArgumentException( 95 "def must be of type String, " 96 + "Boolean, Integer, Long, Float, " 97 + "Double, or byte[]."); 98 } 99 prefTypes.put(key, def); 100 } 101 102 public void add(String key, Object def, Class defClass) { 103 add(key, convert(def, defClass)); 104 } 105 106 public Preferences getPreferences() { 107 return node; 108 } 109 110 public Object get(String key) { 111 Object def = getDefault(key); 112 return convert(node.get(key, def.toString()), def.getClass()); 113 } 114 115 public void set(String key, Object value) { 116 value = convert(value, getDefault(key).getClass()); 117 node.put(key, value.toString()); 118 } 119 120 public Object getDefault(String key) { 121 Object def = prefTypes.get(key); 122 if (def == null) { 123 throw new IllegalStateException( 124 "Key " + key + " not declared in this Prefs object."); 125 } 126 127 return def; 128 } 129 130 public static Object convert(Object src, Class targetType) { 131 132 if (targetType.isInstance(src)) { 133 return src; 134 } 135 136 String str = src.toString(); 137 138 if (targetType == String.class) { 139 return str; 140 } else if (targetType == Boolean.class) { 141 return Boolean.valueOf(str); 142 } else if (targetType == Integer.class) { 143 return Integer.valueOf(str); 144 } else if (targetType == Long.class) { 145 return Long.valueOf(str); 146 } else if (targetType == Float.class) { 147 return Float.valueOf(str); 148 } else if (targetType == Double.class) { 149 return Double.valueOf(str); 150 } else if (targetType == byte[].class) { 151 return str.getBytes(); 152 } 153 154 return str; 155 } 156 157 }

This page was automatically generated by Maven