001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.util;
021
022
023 import java.util.Dictionary;
024 import java.util.Enumeration;
025 import java.util.prefs.Preferences;
026 import java.util.prefs.BackingStoreException;
027
028 import org.apache.directory.shared.i18n.I18n;
029
030
031 /**
032 * A wrapper around Preferences to access it as a Dictionary.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 919765 $
036 */
037 public class PreferencesDictionary extends Dictionary<String,String>
038 {
039 /** the underlying wrapped preferences object */
040 private final Preferences prefs;
041
042
043 // ------------------------------------------------------------------------
044 // C O N S T R U C T O R
045 // ------------------------------------------------------------------------
046
047 public PreferencesDictionary(Preferences prefs)
048 {
049 this.prefs = prefs;
050 }
051
052
053 // ------------------------------------------------------------------------
054 // E X T R A M E T H O D S
055 // ------------------------------------------------------------------------
056
057 /**
058 * Gets the Preferences used as the backing store for this Dictionary.
059 *
060 * @return the underlying Preferences object
061 */
062 public Preferences getPreferences()
063 {
064 return prefs;
065 }
066
067
068 // ------------------------------------------------------------------------
069 // D I C T I O N A R Y M E T H O D S
070 // ------------------------------------------------------------------------
071
072 public int size()
073 {
074 try
075 {
076 return prefs.keys().length;
077 }
078 catch ( BackingStoreException e )
079 {
080 throw new NestableRuntimeException( I18n.err( I18n.ERR_04423 ), e );
081 }
082 }
083
084
085 public boolean isEmpty()
086 {
087 try
088 {
089 return prefs.keys().length == 0;
090 }
091 catch ( BackingStoreException e )
092 {
093 throw new NestableRuntimeException( I18n.err( I18n.ERR_04423 ), e );
094 }
095 }
096
097
098 @SuppressWarnings("unchecked")
099 public Enumeration<String> elements()
100 {
101 try
102 {
103 return new ArrayEnumeration( prefs.keys() )
104 {
105 public String nextElement()
106 {
107 String key = ( String ) super.nextElement();
108
109 return prefs.get( key, null );
110 }
111 };
112 }
113 catch ( BackingStoreException e )
114 {
115 throw new NestableRuntimeException( I18n.err( I18n.ERR_04423 ), e );
116 }
117 }
118
119
120 @SuppressWarnings("unchecked")
121 public Enumeration<String> keys()
122 {
123 try
124 {
125 return new ArrayEnumeration( prefs.keys() );
126 }
127 catch ( BackingStoreException e )
128 {
129 throw new NestableRuntimeException( I18n.err( I18n.ERR_04423 ), e );
130 }
131 }
132
133
134 public String get( Object key )
135 {
136 if ( key instanceof String )
137 {
138 return prefs.get( ( String ) key, null );
139 }
140
141 return prefs.get( key.toString(), null );
142 }
143
144
145 public String remove( Object key )
146 {
147 String retval = get( key );
148
149 if ( key instanceof String )
150 {
151 prefs.remove( ( String ) key );
152 }
153 else
154 {
155 prefs.remove( key.toString() );
156 }
157
158 return retval;
159 }
160
161
162 public String put( String key, String value )
163 {
164 String retval = get( key );
165
166 prefs.put( key, value );
167
168 return retval;
169 }
170 }