001 /*
002 * Copyright 2005,2009 Ivan SZKIBA
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.ini4j;
017
018 import org.ini4j.spi.AbstractBeanInvocationHandler;
019
020 import java.lang.reflect.Proxy;
021
022 import java.util.prefs.Preferences;
023
024 public final class PreferencesBean
025 {
026 private PreferencesBean()
027 {
028 }
029
030 public static <T> T newInstance(Class<T> clazz, final Preferences prefs)
031 {
032 return clazz.cast(Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, new Handler(prefs)));
033 }
034
035 private static class Handler extends AbstractBeanInvocationHandler
036 {
037 private final Preferences _prefs;
038
039 public Handler(Preferences prefs)
040 {
041 _prefs = prefs;
042 }
043
044 @Override protected Object getPropertySpi(String property, Class<?> clazz)
045 {
046 return _prefs.get(property, null);
047 }
048
049 @Override protected void setPropertySpi(String property, Object value, Class<?> clazz)
050 {
051 _prefs.put(property, value.toString());
052 }
053
054 @Override protected boolean hasPropertySpi(String property)
055 {
056 return _prefs.get(property, null) != null;
057 }
058 }
059 }