litesoft
@ 821
litesoft / trunk / Java / core / Anywhere / src / org / litesoft / configuration / AbstractConfigDataAccessor.java
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.configuration; import java.util.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.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 = Strings.assertNotNullNotEmpty( "Key", pKey ); Level zLevel = Objects.deNull( 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 = Strings.assertNotNullNotEmpty( "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 = Strings.noEmpty( pKeyPrefix )) ) { pValue = Strings.deNull( 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
Revision | Author | Commited | Message |
---|---|---|---|
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
801 Diff | GeorgeS | Wed 15 Aug, 2012 03:59:02 +0000 | |
475 Diff | GeorgeS | Sat 03 Sep, 2011 13:54:51 +0000 | |
474 | GeorgeS | Fri 02 Sep, 2011 14:29:50 +0000 | Switch to Properties and eliminate some of the Per App shit |