Subversion Repository Public Repository

litesoft

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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package org.litesoft.aokeyhole.swing.mains.mementobeans.support;

import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;

import org.litesoft.aokeyhole.objects.*;
import org.litesoft.aokeyhole.objects.factories.*;
import org.litesoft.aokeyhole.objects.factories.mementobean.*;
import org.litesoft.commonfoundation.typeutils.Objects;

public class AttributeData implements BeanMagicStrings
{
    private static final String[] VALID_TYPES = extractValidTypes( MementoBeanPropertyMetaDataDefinitionAccessor.INSTANCE );

    private int mID;
    private String mName;
    private String mType;
    private boolean mMementoableType;
    protected boolean mNullEmpties;
    protected boolean mNullZeros;
    protected boolean mNullFalses;
    protected boolean mRepeating;
    private String[] mNotes = Strings.EMPTY_ARRAY;

    private AttributeData()
    {
    }

    public AttributeData( int pID, BeanAttributeBuilder pAttribute )
    {
        mID = pID;
        mName = pAttribute.getName();
        mRepeating = pAttribute.isRepeating();
        mNotes = pAttribute.getNotes();
        mNullEmpties = pAttribute.isNullEmpties();
        mNullZeros = pAttribute.isNullZeros();
        mNullFalses = pAttribute.isNullFalses();
        mType = pAttribute.getType();
        mMementoableType = !Objects.isOneOf( mType, VALID_TYPES );
    }

    private AttributeData( AttributeData pCloneFrom )
    {
        this.mID = pCloneFrom.mID;
        this.mName = pCloneFrom.mName;
        this.mType = pCloneFrom.mType;
        this.mMementoableType = pCloneFrom.mMementoableType;
        this.mNullEmpties = pCloneFrom.mNullEmpties;
        this.mNullZeros = pCloneFrom.mNullZeros;
        this.mNullFalses = pCloneFrom.mNullFalses;
        this.mRepeating = pCloneFrom.mRepeating;
        this.mNotes = pCloneFrom.mNotes;
    }

    public int getID()
    {
        return mID;
    }

    public String getName()
    {
        return mName;
    }

    public String getType()
    {
        return mType;
    }

    public boolean isMementoableType()
    {
        return mMementoableType;
    }

    public boolean isNullEmpties()
    {
        return mNullEmpties;
    }

    public boolean isNullZeros()
    {
        return mNullZeros;
    }

    public boolean isNullFalses()
    {
        return mNullFalses;
    }

    public boolean isRepeating()
    {
        return mRepeating;
    }

    public String[] getNotes()
    {
        return mNotes;
    }

    public void setNotes( String[] pNotes )
    {
        mNotes = Strings.deNull( pNotes );
    }

    /**
     * An ID is a 2 digit value (with leading zeros) between 0 & 99
     */
    private void appendID( StringBuilder pSB ) // PART1
    {
        pSB.append( Integers.zeroPadIt( 2, mID ) );
    }

    /**
     * An ID is a 2 digit value (with leading zeros) between 0 & 99
     */
    private void parseID( String pID ) // PART1
    {
        mID = Integer.parseInt( pID );
    }

    private void appendType( StringBuilder pSB )
    {
        pSB.append( isMementoableType() ? MEMENTOABLE_ATTRIBUTE_PROXY_TYPE : getType() );
    }

    private void appendMementoTypeExtension1( StringBuilder pSB ) // PART5
    {
        if ( isMementoableType() )
        {
            pSB.append( '<' ).append( getType() ).append( '>' );
        }
    }

    /**
     * Parse the Pre & Post type indicators.
     *
     * @param pPreType           an AttributeProxy type: Mementoable, String, Integer, ...
     * @param pBracketedPostType Either an empty String of the class name of the Mementoable surrounded by generic type indicators ('<' & '>').
     */
    private void parseType( String pPreType, String pBracketedPostType ) // PART3 & PART5
    {
        // "String" & "" || "Mementoable" & "<DetailBean>"
        if ( !MEMENTOABLE_ATTRIBUTE_PROXY_TYPE.equals( pPreType ) )
        {
            Strings.assertNullOrEmpty( "BracketedPostType", pBracketedPostType );
            mType = Objects.assertOneOf( pPreType, VALID_TYPES );
            return;
        }
        mMementoableType = true;
        if ( pBracketedPostType.startsWith( "<" ) && pBracketedPostType.endsWith( ">" ) )
        {
            mType = pBracketedPostType.substring( 1, pBracketedPostType.length() - 1 );
            if ( Strings.isValidAttributeIdentifier( mType ) )
            {
                return;
            }
        }
        throw new IllegalArgumentException( "Unacceptable BracketedPostType: " + pBracketedPostType );
    }

    private void appendMementoTypeExtension2( StringBuilder pSB ) // PART7b
    {
        if ( isMementoableType() )
        {
            pSB.append( ATTR_PART7b_PRE_TYPE ).append( mType ).append( ATTR_PART7b_POST_TYPE );
        }
    }

    private void appendName( StringBuilder pSB ) // PART7a
    {
        if ( mName == null )
        {
             pSB.append( ATTR_PART7a_NO_NAME );
        }
        else
        {
            pSB.append( DOUBLE_QUOTE ).append( mName ).append( DOUBLE_QUOTE );
        }
    }

    /**
     * Parse the name from the Attribute Proxy's Parameters.
     */
    private void parseName( String pAttributeProxyParameters ) // PART7
    {
        // A string of 'null' which indicates that this is a legacy place holder
        // Single (double quoted) Names, e.g.: "FirstName", "Name", "Description"
        // or the above followed by a default instance, like:
        // "SpecialDetail", DetailBean.DEFAULT_INSTANCE
        // "Details", DetailBean.DEFAULT_INSTANCE
        String zName = pAttributeProxyParameters;
        if ( mMementoableType )
        {
            String zDefaultInstanceTail = ATTR_PART7b_PRE_TYPE + mType + ATTR_PART7b_POST_TYPE;
            if ( !pAttributeProxyParameters.endsWith( zDefaultInstanceTail ) )
            {
                throw new IllegalArgumentException(
                        "Mementoable attribute proxy, but not default instance in the parameter section: " + pAttributeProxyParameters );
            }
            zName = pAttributeProxyParameters.substring( 0, pAttributeProxyParameters.length() - zDefaultInstanceTail.length() );
        }
        if ( !zName.equals( ATTR_PART7a_NO_NAME ) )
        {
            if ( (zName.length() < 3) || !zName.startsWith( DOUBLE_QUOTE ) || !zName.endsWith( DOUBLE_QUOTE ) )
            {
                throw new IllegalArgumentException( "Malformed name '" + zName + "' in: " + pAttributeProxyParameters );
            }
            zName = zName.substring( 1, zName.length() - 1 );
            if ( !Strings.isAsciiIdentifier( zName ) )
            {
                throw new IllegalArgumentException( "Name '" + zName + "' not an Ascii Identifier in: " + pAttributeProxyParameters );
            }
            mName = zName;
        }
    }

    private void appendFlags( StringBuilder pSB ) // PART9
    {
        for ( BeanAttributeProperties zValue : BeanAttributeProperties.values() )
        {
            zValue.appendFlag( this, pSB );
        }
    }

    // /* 00 */ new StringAttributeProxy( "FirstName" ), //
    // /* 00 */ new StringAttributeProxy( "Name" ).nullEmpties(), //
    // /* 01 */ new StringAttributeProxy( null ), //
    // /* 02 */ new StringAttributeProxy( "Description" ).nullEmpties().repeating(), //
    // /* 03 */ new MementoableAttributeProxy<DetailBean>( "SpecialDetail", DetailBean.DEFAULT_INSTANCE ).nullEmpties(), //
    // 01234567-10123
    // /* 04 */ new MementoableAttributeProxy<DetailBean>( "Details", DetailBean.DEFAULT_INSTANCE ).nullEmpties().repeating(), //
    // 123  12345678           12345678901234            12                                      12                          1234
    // 0  1 2       3          4             5           6 7                                     8 9                         10
    private void parseFlags( String pFlags ) // PART9
    {
        StringBuilder zFlags = new StringBuilder( pFlags );
        for ( BeanAttributeProperties zValue : BeanAttributeProperties.values() )
        {
            zValue.parseFlag( this, zFlags );
        }
        if ( zFlags.length() != 0 )
        {
            throw new IllegalArgumentException( "Don't understand '" + zFlags + "' in: " + pFlags );
        }
    }

    public String toParseLine()
    {
        StringBuilder sb = new StringBuilder();
        sb.append( ATTR_PART0 );
        appendID( sb ); // PART1
        sb.append( ATTR_PART2 );
        appendType( sb ); // PART3
        sb.append( ATTR_PART4 );
        appendMementoTypeExtension1( sb ); // PART5
        sb.append( ATTR_PART6 );
        appendName( sb ); // PART7a
        appendMementoTypeExtension2( sb ); // PART7b
        sb.append( ATTR_PART8 );
        appendFlags( sb ); // PART9
        return sb.append( ATTR_PART10 ).toString();
    }

    public static AttributeData parseLine( String pLine )
    {
        // /* 00 */ new StringAttributeProxy( "FirstName" ), //
        // /* 00 */ new StringAttributeProxy( "Name" ).nullEmpties(), //
        // /* 01 */ new StringAttributeProxy( null ), //
        // /* 02 */ new StringAttributeProxy( "Description" ).nullEmpties().repeating(), //
        // /* 03 */ new MementoableAttributeProxy<DetailBean>( "SpecialDetail", DetailBean.DEFAULT_INSTANCE ).nullEmpties(), //
        // 01234567-10123
        // /* 04 */ new MementoableAttributeProxy<DetailBean>( "Details", DetailBean.DEFAULT_INSTANCE ).nullEmpties().repeating(), //
        // 123  12345678           12345678901234            12                                      12                          1234
        // 0  1 2       3          4             5           6 7                                     8 9                         10
        int atPart2 = pLine.indexOf( ATTR_PART2 );
        int atPart4 = pLine.indexOf( ATTR_PART4, atPart2 + 1 );
        int atPart6 = pLine.indexOf( ATTR_PART6, atPart4 + 1 );
        int atPart8 = pLine.indexOf( ATTR_PART8, atPart6 + 1 );
        if ( pLine.startsWith( ATTR_PART0 ) && //
             (atPart2 != -1) && (atPart4 != -1) && (atPart6 != -1) && (atPart8 != -1) && //
             pLine.endsWith( ATTR_PART10 ) )
        {
            AttributeData zAttribute = new AttributeData();
            try
            {
                zAttribute.parseID( pLine.substring( ATTR_PART0.length(), atPart2 ) ); // PART1
                zAttribute.parseType( pLine.substring( atPart2 + ATTR_PART2.length(), atPart4 ), // PART3
                                      pLine.substring( atPart4 + ATTR_PART4.length(), atPart6 ) ); // PART5
                zAttribute.parseName( pLine.substring( atPart6 + ATTR_PART6.length(), atPart8 ) ); // PART7
                zAttribute.parseFlags( pLine.substring( atPart8 + ATTR_PART8.length(), pLine.length() - ATTR_PART10.length() ) ); // PART9
                return zAttribute;
            }
            catch ( IllegalArgumentException e )
            {
                System.out.println( e.getMessage() );
                // Fall thru...
            }
        }
        return null;
    }

    private static String[] extractValidTypes( PropertyMetaDataDefinitionAccessor pAccessor )
    {
        List<String> zTypes = Lists.newArrayList();
        for ( AttributeSet zAttribute : pAccessor.getAttributeSets() )
        {
            AttributeType zType = zAttribute.getType();
            if ( zType.getFAT().isSimple() )
            {
                zTypes.add( zType.toString() );
            }
        }
        return zTypes.toArray( new String[zTypes.size()] );
    }

    public AttributeData makePlaceHolder()
    {
        AttributeData zAttributeData = new AttributeData( this );
        zAttributeData.mName = null;
        zAttributeData.setNotes( null );
        if ( isMementoableType() )
        {
            zAttributeData.mNullEmpties = true;
            zAttributeData.mType = "String";
            zAttributeData.mMementoableType = false;
        }
        return zAttributeData;
    }

    public boolean isUpdatableFrom( BeanAttributeBuilder pAttribute )
    {
        return Objects.areNonArraysEqual( this.getName(), pAttribute.getName() ) && //
               Objects.areNonArraysEqual( this.getType(), pAttribute.getType() ) && //
               (this.isRepeating() == pAttribute.isRepeating());
    }

    /**
     * Either return <code>this</code> if nothing has changed or create a new instance from <code>this</code> with the changes
     * <p/>
     * Note: <code>pAttribute</code> must have already passed thru <code>isUpdatableFrom</code>
     */
    public AttributeData updateWith( BeanAttributeBuilder pAttribute )
    {
        if ( isUpdatableFromRestIsEquivalent( pAttribute ) )
        {
            return this;
        }
        AttributeData zAttributeData = new AttributeData( this );
        zAttributeData.setNotes( pAttribute.getNotes() );
        zAttributeData.mNullEmpties = pAttribute.isNullEmpties();
        zAttributeData.mNullZeros = pAttribute.isNullZeros();
        zAttributeData.mNullFalses = pAttribute.isNullFalses();
        return zAttributeData;
    }

    private boolean isUpdatableFromRestIsEquivalent( BeanAttributeBuilder pAttribute )
    {
        return Objects.areArraysEqual( this.getNotes(), pAttribute.getNotes() ) && //
               (this.isNullEmpties() == pAttribute.isNullEmpties()) && //
               (this.isNullZeros() == pAttribute.isNullZeros()) && //
               (this.isNullFalses() == pAttribute.isNullFalses());
    }
}

Commits for litesoft/trunk/Java/KeyHole/src/org/litesoft/aokeyhole/swing/mains/mementobeans/support/AttributeData.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

934 Diff Diff GeorgeS picture GeorgeS Mon 07 Apr, 2014 00:49:58 +0000
838 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 17:50:14 +0000
830 Diff Diff GeorgeS picture GeorgeS Fri 31 Aug, 2012 18:10:19 +0000
828 Diff Diff GeorgeS picture GeorgeS Wed 29 Aug, 2012 00:53:01 +0000
827 Diff Diff GeorgeS picture GeorgeS Sun 26 Aug, 2012 17:14:31 +0000

!

821 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000
820 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 22:41:35 +0000

!

815 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 17:54:14 +0000
788 GeorgeS picture GeorgeS Sun 05 Aug, 2012 22:58:50 +0000

!