litesoft
@ 939
litesoft / trunk / Java / core / Anywhere / src / org / litesoft / mementobeansupport / AbstractAttributeProxy.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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
package org.litesoft.mementobeansupport; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; import org.litesoft.charstreams.*; import org.litesoft.codec.*; import org.litesoft.commonfoundation.annotations.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.util.*; public abstract class AbstractAttributeProxy<T, V extends AbstractAttributeProxy<T, V>> implements Cloneable, ToStringBuilder, Mementoable<V> { private final boolean mLegacyMemoAttribute; private final String mName; private final TypedTerminatingCodec<T> mCodec; private boolean mRepeating = false; private Object mValue; // Note: If this is a LegacyMemoAttribute then this will always be null! protected AbstractAttributeProxy( String pName, TypedTerminatingCodec<T> pCodec ) { mLegacyMemoAttribute = (null == pName); if ( !mLegacyMemoAttribute && !Strings.isAsciiIdentifier( pName ) ) { throw new IllegalArgumentException( "Not an Identifier: '" + pName + "'" ); } mName = pName; mCodec = pCodec; } protected AbstractAttributeProxy( AbstractAttributeProxy<T, V> pOrig ) { this( pOrig.mName, pOrig.mCodec ); this.mRepeating = pOrig.mRepeating; this.mValue = pOrig.mValue; } public boolean isEmpty() { return (mValue == null); } public final V repeating() { if ( isRepeating() ) { return castV( this ); } V zClone = copy(); castBase( zClone ).mRepeating = true; return zClone; } public final boolean isRepeating() { return mRepeating; } public final T get() { if ( isRepeating() ) { throw new IllegalStateException( "Attempt to access repeating attribute in a non-repeating way" ); } return castT( mValue ); } public final V set( T pValue ) { if ( mLegacyMemoAttribute ) { pValue = null; } else { pValue = normalize( pValue ); } T zCurValue = get(); if ( EqualSupport.equal( zCurValue, pValue ) ) { return castV( this ); } V zClone = copy(); castBase( zClone ).mValue = pValue; return zClone; } public final List<T> getRepeating() { if ( !isRepeating() ) { throw new IllegalStateException( "Attempt to access non-repeating attribute in a repeating way" ); } return castListT( mValue ); } public final V setRepeating( List<T> pValue ) { if ( mLegacyMemoAttribute ) { pValue = null; } else { pValue = normalize( pValue ); } List<T> zCurValue = getRepeating(); if ( EqualSupport.equal( zCurValue, pValue ) ) { return castV( this ); } V zClone = copy(); castBase( zClone ).mValue = makeImmutable( pValue ); return zClone; } @Override public final String toString() { return mLegacyMemoAttribute ? null : toStringBuilder( new StringBuilder(), 0 ).toString(); } @Override public final StringBuilder toStringBuilder( StringBuilder pSB, int pDepth ) { if ( !mLegacyMemoAttribute ) { pSB.append( mName ); if ( isRepeating() ) { repeatingToStringBuilder( pSB, pDepth, getRepeating() ); } else { singleToStringBuilder( pSB.append( ':' ), pDepth, get() ); } } return pSB; } @Override public final int hashCode() { return EqualSupport.calcHashCode( mValue ); } @Override public final boolean equals( Object obj ) { return (this == obj) || // ((obj != null) && (this.getClass() == obj.getClass()) && EqualSupport.equal( this.mValue, ((AbstractAttributeProxy)obj).mValue )); } @Override public void toMemento( @NotNull CharSink pCharSink ) { if ( !isRepeating() ) { mCodec.encode( pCharSink, get() ); return; } List<T> zRepeating = getRepeating(); if ( zRepeating == null ) { IntegerTypedTerminatingCodec.NON_NEGATIVE.encode( pCharSink, null ); return; } IntegerTypedTerminatingCodec.NON_NEGATIVE.encode( pCharSink, zRepeating.size() ); for ( T zValue : zRepeating ) { mCodec.encode( pCharSink, zValue ); } } @Override public V fromMemento( @NotNull CharSource pToMemoValue ) { if ( !isRepeating() ) { return set( mCodec.decode( pToMemoValue ) ); } Integer zSize = IntegerTypedTerminatingCodec.NON_NEGATIVE.decode( pToMemoValue ); List<T> zRepeating = null; if ( zSize != null ) { int zCount = zSize; for ( zRepeating = new ArrayList<T>( zCount ); zCount > 0; zCount-- ) { zRepeating.add( mCodec.decode( pToMemoValue ) ); } } return setRepeating( zRepeating ); } private List<T> makeImmutable( List<T> pValue ) { if ( pValue == null ) { return null; } return Collections.unmodifiableList( pValue ); } private void repeatingToStringBuilder( StringBuilder pSB, int pDepth, List<T> pRepeating ) { if ( pRepeating == null ) { singleToStringBuilder( pSB.append( ':' ), pDepth, null ); return; } pSB.append( " (" ).append( pRepeating.size() ).append( ')' ); if ( !pRepeating.isEmpty() ) { String zIndex = Strings.spaces( ++pDepth * 4 ); pSB.append( ':' ); for ( int i = 0; i < pRepeating.size(); i++ ) { singleToStringBuilder( pSB.append( '\n' ).append( zIndex ).append( i ).append( " -" ), pDepth, pRepeating.get( i ) ); } } } private void singleToStringBuilder( StringBuilder pSB, int pDepth, T pValue ) { pSB.append( ' ' ); if ( pValue == null ) { pSB.append( pValue ); // null } else if ( pValue instanceof ToStringBuilder ) { ((ToStringBuilder) pValue).toStringBuilder( pSB, pDepth ); } else { appendNonNullValueToString( pSB, pDepth, pValue ); } } protected final List<T> normalizeEachEntry( List<T> pValues ) { if ( (pValues != null) && !pValues.isEmpty() ) { List<T> zValues = new ArrayList<T>( pValues.size() ); for ( T zValue : pValues ) { zValues.add( normalize( zValue ) ); } while ( zValues.get( zValues.size() - 1 ) == null ) { zValues.remove( zValues.size() - 1 ); } if ( !zValues.isEmpty() ) { return zValues; } } return null; } protected List<T> normalize( List<T> pValues ) { return pValues; } protected T normalize( T pValue ) { return pValue; } @SuppressWarnings("UnusedParameters") protected void appendNonNullValueToString( StringBuilder pSB, int pDepth, T pValue ) { pSB.append( pValue ); } abstract protected V copy(); protected final AbstractAttributeProxy castBase( V pV ) { return (AbstractAttributeProxy) pV; } @SuppressWarnings({"unchecked"}) protected final V castV( Object o ) { return (V) o; } @SuppressWarnings({"unchecked"}) protected final T castT( Object o ) { return (T) o; } @SuppressWarnings({"unchecked"}) protected final List<T> castListT( Object o ) { return (List<T>) o; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/mementobeansupport/AbstractAttributeProxy.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
917 Diff | GeorgeS | Sun 08 Dec, 2013 20:49:56 +0000 | 1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model! |
831 Diff | GeorgeS | Fri 31 Aug, 2012 22:39:56 +0000 | |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
814 Diff | GeorgeS | Sat 18 Aug, 2012 17:52:17 +0000 | |
813 Diff | GeorgeS | Sat 18 Aug, 2012 17:48:21 +0000 | |
799 Diff | GeorgeS | Wed 15 Aug, 2012 03:32:04 +0000 | |
779 Diff | GeorgeS | Mon 16 Jul, 2012 04:34:33 +0000 | |
777 Diff | GeorgeS | Mon 16 Jul, 2012 00:48:01 +0000 | |
773 | GeorgeS | Sun 15 Jul, 2012 22:39:12 +0000 |