litesoft
@ 917
litesoft / trunk / Java / core / Anywhere / src / org / litesoft / mementobeansupport / AbstractMementoBean.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 |
package org.litesoft.mementobeansupport; import java.util.*; import org.litesoft.charstreams.*; import org.litesoft.core.annotations.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.typeutils.Objects; import org.litesoft.core.util.*; public abstract class AbstractMementoBean<CT extends AbstractMementoBean> implements Cloneable, ToStringBuilder, Mementoable<CT> { private final AbstractAttributeProxy[] mAttributeProxies; protected AbstractMementoBean( AbstractAttributeProxy[] pAttributeProxies ) { mAttributeProxies = pAttributeProxies; } protected AbstractMementoBean( AbstractMementoBean<CT> pOrig ) { this.mAttributeProxies = new AbstractAttributeProxy[pOrig.mAttributeProxies.length]; System.arraycopy( pOrig.mAttributeProxies, 0, this.mAttributeProxies, 0, pOrig.mAttributeProxies.length ); } public final boolean isEmpty() { for ( AbstractAttributeProxy zProxy : mAttributeProxies ) { if ( !zProxy.isEmpty() ) { return false; } } return true; } protected <T> T getValueFor( int pIndex ) { return cast( getProxy( pIndex ).get() ); } protected <T> CT setValueFor( int pIndex, T pValue ) { if ( EqualSupport.equal( getValueFor( pIndex ), pValue ) ) { return cast( this ); } CT zClone = copy(); AbstractAttributeProxy<T, ?> zProxy = getProxy( pIndex ); castBase( zClone ).mAttributeProxies[pIndex] = zProxy.set( pValue ); return zClone; } protected <T> List<T> getRepeatingValueFor( int pIndex ) { return cast( getProxy( pIndex ).getRepeating() ); } protected CT clearRepeatingValueFor( int pIndex ) { return setRepeatingValueFor( pIndex, (List<?>) null ); } protected <T> CT addRepeatingValueFor( int pIndex, T pValue ) { List<T> zCurList = getRepeatingValueFor( pIndex ); List<T> zNewList = (zCurList == null) ? new ArrayList<T>() : new ArrayList<T>( zCurList ); zNewList.add( pValue ); return setRepeatingValueFor( pIndex, zNewList ); } protected <T> CT setRepeatingValueFor( int pIndex, T... pValues ) { List<T> zValues = (pValues == null) ? null : Arrays.asList( pValues ); return setRepeatingValueFor( pIndex, zValues ); } protected <T> CT setRepeatingValueFor( int pIndex, List<T> pValues ) { if ( EqualSupport.equal( getRepeatingValueFor( pIndex ), pValues ) ) { return cast( this ); } CT zClone = copy(); AbstractAttributeProxy<T, ?> zProxy = getProxy( pIndex ); castBase( zClone ).mAttributeProxies[pIndex] = zProxy.setRepeating( pValues ); return zClone; } private <T> AbstractAttributeProxy<T, ?> getProxy( int pIndex ) { return cast( mAttributeProxies[pIndex] ); } abstract protected CT copy(); abstract protected CT defaultInstance(); @SuppressWarnings({"unchecked"}) protected final <T> T cast( Object o ) { return (T) o; } @Override public final boolean equals( Object obj ) { return (this == obj) || ((obj instanceof AbstractMementoBean) && equals( (AbstractMementoBean) obj )); } public final boolean equals( AbstractMementoBean them ) { if ( this != them ) { if ( (them == null) || (this.getClass() != them.getClass()) ) { return false; } for ( int i = 0; i < mAttributeProxies.length; i++ ) { if ( !EqualSupport.equal( this.mAttributeProxies[i], them.mAttributeProxies[i] ) ) { return false; } } } return true; } @Override public int hashCode() { int hash = 0; for ( AbstractAttributeProxy zProxy : mAttributeProxies ) { hash = EqualSupport.hashCodeEm( hash, EqualSupport.calcHashCode( zProxy ) ); } return hash; } public String toMemento() { if ( isEmpty() ) { return null; } CharSinkToString zCharSink = new CharSinkToString(); toMemento( zCharSink ); return zCharSink.toString(); } public CT fromMemento( CharSequence pToMemoValue ) { return fromMemento( new CharSourceFromSequence( pToMemoValue ) ); } @Override public void toMemento( @NotNull CharSink pCharSink ) { for ( AbstractAttributeProxy zProxy : mAttributeProxies ) { zProxy.toMemento( pCharSink ); } } @Override public CT fromMemento( @NotNull CharSource pToMemoValue ) { return Cast.it( castBase( defaultInstance() ).defaultFromMemento( pToMemoValue ) ); } private CT defaultFromMemento( @NotNull CharSource pToMemoValue ) { CT zCopy = copy(); AbstractMementoBean zClone = castBase( zCopy ); for ( int i = 0; i < mAttributeProxies.length; i++ ) { zClone.mAttributeProxies[i] = getProxy( i ).fromMemento( pToMemoValue ); } return zCopy; } @Override public final String toString() { return toStringBuilder( new StringBuilder(), 0 ).toString(); } @Override public StringBuilder toStringBuilder( StringBuilder pSB, int pDepth ) { pSB.append( Objects.justClassNameOf( this ) ); String zIndex = Strings.spaces( ++pDepth * 4 ); String zPrefix = ":"; for ( AbstractAttributeProxy zProxy : mAttributeProxies ) { String s = zProxy.toString(); if ( s != null ) { zProxy.toStringBuilder( pSB.append( zPrefix ).append( '\n' ).append( zIndex ), pDepth ); zPrefix = ""; } } return pSB; } protected final AbstractMementoBean castBase( CT pCT ) { return (AbstractMementoBean) pCT; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/mementobeansupport/AbstractMementoBean.java
Revision | Author | Commited | Message |
---|---|---|---|
917 Diff | GeorgeS | Sun 08 Dec, 2013 20:49:56 +0000 | 1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model! |
833 Diff | GeorgeS | Sat 01 Sep, 2012 00:52:53 +0000 | |
825 Diff | GeorgeS | Sun 19 Aug, 2012 19:55:49 +0000 | |
814 Diff | GeorgeS | Sat 18 Aug, 2012 17:52:17 +0000 | |
813 Diff | GeorgeS | Sat 18 Aug, 2012 17:48:21 +0000 | |
811 Diff | GeorgeS | Sat 18 Aug, 2012 13:45:18 +0000 | |
799 Diff | GeorgeS | Wed 15 Aug, 2012 03:32:04 +0000 | |
788 Diff | GeorgeS | Sun 05 Aug, 2012 22:58:50 +0000 | ! |
779 Diff | GeorgeS | Mon 16 Jul, 2012 04:34:33 +0000 | |
777 | GeorgeS | Mon 16 Jul, 2012 00:48:01 +0000 |