litesoft
@ 942
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.codec; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; import org.litesoft.commonfoundation.charstreams.*; /** * Convert Float value to/from a self-terminating string representation */ public abstract class AbstractNumericPartsTypedTerminatingCodec<T> extends AbstractTypedTerminatingCodec<T> { private final String mType; private final List<NumericPart> mNumericParts = Lists.newArrayList(); private final List<SeparatorPart> mSeparatorParts = Lists.newArrayList(); protected AbstractNumericPartsTypedTerminatingCodec( String pType, Part... pParts ) { mType = pType; for ( int i = 0; i < pParts.length; i++ ) { Part zPart = pParts[i]; if ( Integers.isEven( i ) ) { mNumericParts.add( (NumericPart) zPart ); } else { mSeparatorParts.add( (SeparatorPart) zPart ); } } } protected void encodeParts( CharSink pCharSink, String pValue ) { List<Integer> zNumericPartParsedValues = Lists.newArrayList(); CharSourceFromSequence zCharSource = new CharSourceFromSequence( pValue ); int zPart = 0; while ( zPart < mSeparatorParts.size() ) { zNumericPartParsedValues.add( mNumericParts.get( zPart ).parse( mType, zCharSource ) ); mSeparatorParts.get( zPart++ ).parse( mType, zCharSource ); } zNumericPartParsedValues.add( mNumericParts.get( zPart ).parse( mType, zCharSource ) ); for ( int i = zNumericPartParsedValues.size(); --i > 0; ) { if ( zNumericPartParsedValues.get( i ) == 0 ) { zNumericPartParsedValues.remove( i ); } } IntegerTypedTerminatingCodec.NON_NEGATIVE.encode( pCharSink, zNumericPartParsedValues.size() - 1 ); for ( int i = 0; i < zNumericPartParsedValues.size(); i++ ) { mNumericParts.get( i ).encode( pCharSink, zNumericPartParsedValues.get( i ) ); } } protected String decodeParts( CharSource pCharSource ) { int zPartCount = IntegerTypedTerminatingCodec.NON_NEGATIVE.decode( pCharSource ) + 1; List<Integer> zNumericPartParsedValues = Lists.newArrayList(); for ( int i = 0; i < zPartCount; i++ ) { zNumericPartParsedValues.add( mNumericParts.get( i ).decode( pCharSource ) ); } StringBuilder sb = new StringBuilder(); int zPart = 0; while ( zPart < mSeparatorParts.size() ) { mNumericParts.get( zPart ).format( sb, zNumericPartParsedValues ); mSeparatorParts.get( zPart++ ).format( sb ); } mNumericParts.get( zPart ).format( sb, zNumericPartParsedValues ); return sb.toString(); } protected interface Part { } protected static class SeparatorPart implements Part { private final char mSeparator; public SeparatorPart( char pSeparator ) { mSeparator = pSeparator; } public void parse( String pType, CharSource pCharSource ) { char zChar = pCharSource.getRequired(); if ( zChar != mSeparator ) { throw new IllegalArgumentException( "Parsing " + pType + ", Expected '" + mSeparator + "' but got a '" + zChar + "'" ); } } public void format( StringBuilder pSB ) { pSB.append( mSeparator ); } } protected static SeparatorPart DASH = new SeparatorPart( '-' ); protected static SeparatorPart SPACE = new SeparatorPart( ' ' ); protected static SeparatorPart COLON = new SeparatorPart( ':' ); protected static SeparatorPart PERIOD = new SeparatorPart( '.' ); protected static class NumericPart implements Part { private final int mMinimumWidth; private final PrimitiveIntegerCodec mCodec; protected NumericPart( int pMinimumWidth, PrimitiveIntegerCodec pCodec ) { mMinimumWidth = pMinimumWidth; mCodec = pCodec; } protected NumericPart( int pMinimumWidth ) { this( pMinimumWidth, PrimitiveIntegerCodec.NON_NEGATIVE ); } protected NumericPart() { this( 2 ); } public void encode( CharSink pCharSink, int pInt ) { mCodec.encode( pCharSink, pInt ); } public int decode( CharSource pCharSource ) { return mCodec.decode( pCharSource ); } public int parse( String pType, CharSource pCharSource ) { int zChar = pCharSource.getRequired(); if ( !Characters.isNumeric( (char) zChar ) ) { throw new IllegalArgumentException( "Parsing " + pType + ", Expected digit but got a '" + zChar + "'" ); } int rv = zChar - '0'; while ( -1 != (zChar = pCharSource.peek()) ) { if ( !Characters.isNumeric( (char) zChar ) ) { break; } rv = (rv * 10) + (pCharSource.get() - '0'); } return rv; } public void format( StringBuilder pSB, List<Integer> pNumericPartParsedValues ) { pSB.append( Integers.zeroPadIt( mMinimumWidth, pNumericPartParsedValues.isEmpty() ? 0 : pNumericPartParsedValues.remove( 0 ) ) ); } } } |