Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.configuration;

import org.litesoft.commonfoundation.base.*;
import org.litesoft.commonfoundation.exceptions.*;

import java.util.*;

public abstract class AbstractConfigDataAccessor implements ConfigDataAccessor {
    private final String mLoadedFromDirectory;
    private final String mLoadedFrom;
    private final Level mOtherSettableLevel;
    private final Level[] mSetableSupportedLevels;
    protected final Map<String, String> mRunLevelKeyValues = new HashMap<String, String>();

    protected AbstractConfigDataAccessor( String pLoadedFromDirectory, String pLoadedFrom, Level pOtherSettableLevel ) {
        mLoadedFromDirectory = pLoadedFromDirectory;
        mLoadedFrom = pLoadedFrom;
        mSetableSupportedLevels = (null == (mOtherSettableLevel = pOtherSettableLevel)) ? //
                                  new Level[]{Level.RUN} : //
                                  new Level[]{Level.RUN, pOtherSettableLevel};
    }

    @Override
    public final ConfigDataAccessor createConfigDataAccessor() {
        return this;
    }

    @Override
    public final String loadedFrom() {
        return mLoadedFrom;
    }

    @Override
    public final String loadedFromDirectory() {
        return mLoadedFromDirectory;
    }

    /**
     * All implementations MUST support RUN level, but may not support other levels
     *
     * @return an array with at least 1 (RUN) entry
     */
    @Override
    public final Level[] getSetableSupportedLevels() {
        return mSetableSupportedLevels;
    }

    /**
     * All implementations MUST support RUN level, but do not have to support other levels
     *
     * @param pLevel null interpreted as RUN
     * @param pKey   not null or empty
     * @param pValue null means remove
     *
     * @throws UnsupportedOperationException if requested level not supported
     */
    @Override
    public final synchronized void setKeyValue( Level pLevel, String pKey, String pValue )
            throws UnsupportedOperationException {
        pKey = Confirm.significant( "Key", pKey );
        Level zLevel = ConstrainTo.notNull( pLevel, Level.RUN );
        if ( Level.RUN.equals( zLevel ) ) {
            if ( pValue == null ) {
                mRunLevelKeyValues.remove( pKey );
            } else {
                mRunLevelKeyValues.put( pKey, pValue );
            }
            return;
        }
        if ( !zLevel.equals( mOtherSettableLevel ) ) {
            throw new UnsupportedOperationException( "setKeyValue for Level '" + pLevel + "' not supported" );
        }
        try {
            setOtherLevelKeyValue( pKey, pValue );
        }
        catch ( RuntimeException e ) {
            throw new PersistenceException( "Unable to setKeyValue( " + pLevel + ", \"" + pKey + "\", " + pValue + " )" + getExceptionPlus(), e );
        }
    }

    abstract protected String getExceptionPlus();

    abstract protected void setOtherLevelKeyValue( String pKey, String pValue );

    @Override
    public final synchronized String[] getAllKeys() {
        Set<String> zKeys;
        try {
            zKeys = new HashSet<String>( Arrays.asList( getBaseKeys() ) );
        }
        catch ( RuntimeException e ) {
            throw new PersistenceException( "Unable to getAllKeys()" + getExceptionPlus(), e );
        }
        zKeys.addAll( mRunLevelKeyValues.keySet() );
        return zKeys.toArray( new String[zKeys.size()] );
    }

    protected abstract String[] getBaseKeys();

    /**
     * @param pKey not null or empty
     *
     * @return null if not found or value associated with key
     */
    @Override
    public final synchronized String getString( String pKey ) {
        pKey = Confirm.significant( "Key", pKey );
        String rv = mRunLevelKeyValues.get( pKey );
        try {
            return (rv != null) ? rv : getBaseValue( pKey );
        }
        catch ( RuntimeException e ) {
            throw new PersistenceException( "Unable to getString( \"" + pKey + "\" )" + getExceptionPlus(), e );
        }
    }

    protected abstract String getBaseValue( String pKey );

    @Override
    public final synchronized void overrideAllStartingWith( String pKeyPrefix, String pValue ) {
        if ( null != (pKeyPrefix = ConstrainTo.significantOrNull( pKeyPrefix )) ) {
            pValue = ConstrainTo.notNull( pValue );
            for ( String zKey : getAllKeys() ) {
                if ( zKey.startsWith( pKeyPrefix ) ) {
                    setKeyValue( Level.RUN, zKey, pValue );
                }
            }
        }
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/configuration/AbstractConfigDataAccessor.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

821 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
475 GeorgeS picture GeorgeS Sat 03 Sep, 2011 13:54:51 +0000