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
029 /**
030 * A wrapper around Preferences to access it as a Dictionary.
031 *
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 * @version $Rev: 679217 $
034 */
035 public class PreferencesDictionary extends Dictionary<String,String>
036 {
037 /** the underlying wrapped preferences object */
038 private final Preferences prefs;
039
040
041 // ------------------------------------------------------------------------
042 // C O N S T R U C T O R
043 // ------------------------------------------------------------------------
044
045 public PreferencesDictionary(Preferences prefs)
046 {
047 this.prefs = prefs;
048 }
049
050
051 // ------------------------------------------------------------------------
052 // E X T R A M E T H O D S
053 // ------------------------------------------------------------------------
054
055 /**
056 * Gets the Preferences used as the backing store for this Dictionary.
057 *
058 * @return the underlying Preferences object
059 */
060 public Preferences getPreferences()
061 {
062 return prefs;
063 }
064
065
066 // ------------------------------------------------------------------------
067 // D I C T I O N A R Y M E T H O D S
068 // ------------------------------------------------------------------------
069
070 public int size()
071 {
072 try
073 {
074 return prefs.keys().length;
075 }
076 catch ( BackingStoreException e )
077 {
078 throw new NestableRuntimeException( "can't get keys from prefs", e );
079 }
080 }
081
082
083 public boolean isEmpty()
084 {
085 try
086 {
087 return prefs.keys().length == 0;
088 }
089 catch ( BackingStoreException e )
090 {
091 throw new NestableRuntimeException( "can't get keys from prefs", e );
092 }
093 }
094
095
096 @SuppressWarnings("unchecked")
097 public Enumeration<String> elements()
098 {
099 try
100 {
101 return new ArrayEnumeration( prefs.keys() )
102 {
103 public String nextElement()
104 {
105 String key = ( String ) super.nextElement();
106
107 return prefs.get( key, null );
108 }
109 };
110 }
111 catch ( BackingStoreException e )
112 {
113 throw new NestableRuntimeException( "can't get keys from prefs", e );
114 }
115 }
116
117
118 @SuppressWarnings("unchecked")
119 public Enumeration<String> keys()
120 {
121 try
122 {
123 return new ArrayEnumeration( prefs.keys() );
124 }
125 catch ( BackingStoreException e )
126 {
127 throw new NestableRuntimeException( "can't get keys from prefs", e );
128 }
129 }
130
131
132 public String get( Object key )
133 {
134 if ( key instanceof String )
135 {
136 return prefs.get( ( String ) key, null );
137 }
138
139 return prefs.get( key.toString(), null );
140 }
141
142
143 public String remove( Object key )
144 {
145 String retval = get( key );
146
147 if ( key instanceof String )
148 {
149 prefs.remove( ( String ) key );
150 }
151 else
152 {
153 prefs.remove( key.toString() );
154 }
155
156 return retval;
157 }
158
159
160 public String put( String key, String value )
161 {
162 String retval = get( key );
163
164 prefs.put( key, value );
165
166 return retval;
167 }
168 }