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