001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * 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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.settings.j2seprefs;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    import java.util.prefs.AbstractPreferences;
021    import java.util.prefs.BackingStoreException;
022    
023    /**
024     * Transient J2SE Preferences implementation. Used for testing with J2SE prefs.
025     * 
026     * @author Peter De Bruycker
027     */
028    public class TransientPreferences extends AbstractPreferences {
029    
030            public TransientPreferences(AbstractPreferences parent, String name) {
031                    super(parent, name);
032            }
033    
034            public TransientPreferences() {
035                    this(null, "");
036            }
037    
038            private Map children = new HashMap();
039    
040            private Map values = new HashMap();
041    
042            protected void flushSpi() throws BackingStoreException {
043                    // not used
044            }
045    
046            protected void removeNodeSpi() throws BackingStoreException {
047                    values.clear();
048            }
049    
050            protected void syncSpi() throws BackingStoreException {
051                    // not used
052            }
053    
054            protected String[] childrenNamesSpi() throws BackingStoreException {
055                    return (String[]) children.keySet().toArray(new String[children.size()]);
056            }
057    
058            protected String[] keysSpi() throws BackingStoreException {
059                    return (String[]) values.keySet().toArray(new String[values.size()]);
060            }
061    
062            protected void removeSpi(String key) {
063                    values.remove(key);
064            }
065    
066            protected String getSpi(String key) {
067                    if (values.containsKey(key)) {
068                            return (String) values.get(key);
069                    }
070                    return "";
071            }
072    
073            protected void putSpi(String key, String value) {
074                    values.put(key, value);
075            }
076    
077            protected AbstractPreferences childSpi(String name) {
078                    if (!children.containsKey(name)) {
079                            children.put(name, new TransientPreferences(this, name));
080                    }
081                    return (AbstractPreferences) children.get(name);
082            }
083    }