litesoft
@ 811
litesoft / trunk / Java / core / Anywhere / src / org / litesoft / memobeansupport / AbstractMemoBean.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 |
package org.litesoft.memobeansupport; 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.util.*; public abstract class AbstractMemoBean<CT extends AbstractMemoBean> implements Cloneable, ToStringBuilder, Memoable<CT> { private AbstractAttributeProxy[] mAttributeProxies; protected AbstractMemoBean( AbstractAttributeProxy... pAttributeProxies ) { mAttributeProxies = pAttributeProxies; } protected AbstractMemoBean( AbstractMemoBean<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 ); 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 ); zClone.mAttributeProxies[pIndex] = zProxy.setRepeating( pValues ); return zClone; } private <T> AbstractAttributeProxy<T, ?> getProxy( int pIndex ) { return cast( mAttributeProxies[pIndex] ); } abstract protected CT copy(); @SuppressWarnings({"unchecked"}) protected final <T> T cast( Object o ) { return (T) o; } @Override public final boolean equals( Object obj ) { return (this == obj) || ((obj instanceof AbstractMemoBean) && equals( (AbstractMemoBean) obj )); } public final boolean equals( AbstractMemoBean 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 toMemo() { if ( isEmpty() ) { return null; } CharSinkToString zCharSink = new CharSinkToString(); toMemo( zCharSink ); return zCharSink.toString(); } public CT fromMemo( CharSequence pToMemoValue ) { return fromMemo( new CharSourceFromSequence( pToMemoValue ) ); } @Override public void toMemo( @NotNull CharSink pCharSink ) { for ( AbstractAttributeProxy zProxy : mAttributeProxies ) { zProxy.toMemo( pCharSink ); } } @Override public CT fromMemo( @NotNull CharSource pToMemoValue ) { CT zClone = copy(); for ( int i = 0; i < mAttributeProxies.length; i++ ) { zClone.mAttributeProxies[i] = getProxy( i ).fromMemo( pToMemoValue ); } return zClone; } @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; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/memobeansupport/AbstractMemoBean.java
Revision | Author | Commited | Message |
---|---|---|---|
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 Diff | GeorgeS | Mon 16 Jul, 2012 00:48:01 +0000 | |
773 Diff | GeorgeS | Sun 15 Jul, 2012 22:39:12 +0000 | |
772 Diff | GeorgeS | Sun 15 Jul, 2012 16:55:51 +0000 | ! |
770 Diff | GeorgeS | Sat 14 Jul, 2012 23:20:33 +0000 | |
769 Diff | GeorgeS | Sat 14 Jul, 2012 23:18:20 +0000 | |
768 | GeorgeS | Sat 14 Jul, 2012 22:48:34 +0000 | ! |