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
package org.litesoft.aokeyhole.swing.mains.mementobeans.support;

import org.litesoft.commonfoundation.base.*;
import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.textfiledirectory.*;

import java.io.*;
import java.util.*;

public class BeanWriter implements BeanMagicStrings {
    private final TextFileDirectory mTextFileDirectory;
    private final String mPackage;

    public BeanWriter( TextFileDirectory pTextFileDirectory, String pPackage ) {
        mTextFileDirectory = pTextFileDirectory;
        mPackage = pPackage;
    }

    /**
     * Persist the changes from the BeanReader to the BeanObjectBuilder (while either can be null, at least one will be not null).
     * <p/>
     * If <code>pBeanReader</code> is null, then this is a "create" Bean request,
     * Else If <code>pBuilder</code> is null, then this is a Bean "delete" request,
     * else this is an "update" Bean "request"
     *
     * @param pBeanReader Original Bean Data
     * @param pBuilder    New Bean Data
     */
    public BeanReader persist( BeanReader pBeanReader, BeanObjectBuilder pBuilder )
            throws IOException {
        if ( pBuilder == null ) {
            return delete( pBeanReader.getName() );
        }
        List<AttributeData> mergedAttributes = Lists.newArrayList();
        if ( pBeanReader == null ) {
            convertAndMergeInto( null, pBuilder.getAttributes(), mergedAttributes );
            return create( pBuilder.getName(), pBuilder.getNotes(), mergedAttributes );
        }
        boolean attributesChanged = convertAndMergeInto( pBeanReader.getAttributeDatas(), pBuilder.getAttributes(), mergedAttributes );
        if ( attributesChanged || !Currently.areEqual( pBeanReader.getNotes(), pBuilder.getNotes() ) ) {
            return update( pBuilder.getName(), pBuilder.getNotes(), mergedAttributes );
        }
        return pBeanReader;
    }

    private BeanReader delete( String pName )
            throws IOException {
        String zFileName = pName + "." + SOURCE_FILE_EXTENSION;
        mTextFileDirectory.delete( ABSTRACT_NAME_PREFIX + zFileName );
        try {
            mTextFileDirectory.delete( zFileName );
        }
        catch ( TextFileDirectory.FileDoesNotExists pFileDoesNotExists ) {
            // Ignore as some kinds of refactoring may have removed it!
        }
        return null;
    }

    private BeanReader create( String pName, String[] pNotes, List<AttributeData> pNewAttributes )
            throws IOException {
        String zFileName = pName + "." + SOURCE_FILE_EXTENSION;
        try {
            mTextFileDirectory.create( new TextFile( zFileName, getBeanLines( pName ) ) );
        }
        catch ( TextFileDirectory.FileAlreadyExists pFileAlreadyExists ) {
            // Ignore as some kinds of refactoring may have already created it!
        }
        mTextFileDirectory.create( new TextFile( ABSTRACT_NAME_PREFIX + zFileName, getAbstractBeanLines( pName, pNotes, pNewAttributes ) ) );
        return new BeanReader( pName, pNotes, pNewAttributes );
    }

    private BeanReader update( String pName, String[] pNotes, List<AttributeData> pNewAttributes )
            throws IOException {
        String zFileName = pName + "." + SOURCE_FILE_EXTENSION;
        mTextFileDirectory.update( new TextFile( ABSTRACT_NAME_PREFIX + zFileName, getAbstractBeanLines( pName, pNotes, pNewAttributes ) ) );
        return new BeanReader( pName, pNotes, pNewAttributes );
    }

    private String[] getBeanLines( String pName ) {
        return new String[]
                {
                        "package " + mPackage + ";", //
                        "", //
                        "public class " + pName + " extends Abstract" + pName, //
                        "{", //
                        "    public " + pName + "()", //
                        "    {", //
                        "    }", //
                        "", //
                        "    public " + pName + "( " + pName + " pOrig )", //
                        "    {", //
                        "        super( pOrig );", //
                        "    }", //
                        "}", //
                };
    }

    private String[] getAbstractBeanLines( String pName, String[] pNotes, List<AttributeData> zNewAttributes ) {
        List<String> zLines = Lists.newArrayList();
        zLines.add( "package " + mPackage + ";" );
        zLines.add( "" );
        if ( anyRepeating( zNewAttributes ) ) {
            zLines.add( "import org.litesoft.commonfoundation.typeutils.Objects;" );
            zLines.add( "import java.util.*;" );
            zLines.add( "" );
        }
        zLines.add( "import " + MEMENTO_BEANS_PACKAGE + ".*;" );
        zLines.add( "" );
        if ( pNotes.length != 0 ) {
            zLines.add( "/**" );
            for ( String zNote : pNotes ) {
                zLines.add( " * " + zNote );
            }
            zLines.add( " */" );
        }
        zLines.add( "@SuppressWarnings(\"UnusedDeclaration\")" );
        zLines.add( "public class Abstract" + pName + " extends AbstractMementoBean<" + pName + ">" );
        zLines.add( "{" );
        zLines.add( "    private static final AbstractAttributeProxy[] ATTRIBUTES = //" );
        zLines.add( "            { //" );
        for ( AttributeData zAttribute : zNewAttributes ) {
            zLines.add( "              " + zAttribute.toParseLine() );
        }
        zLines.add( "            };" );
        zLines.add( "" );
        zLines.add( "    protected Abstract" + pName + "()" );
        zLines.add( "    {" );
        zLines.add( "        super( ATTRIBUTES );" );
        zLines.add( "    }" );
        zLines.add( "" );
        zLines.add( "    protected Abstract" + pName + "( Abstract" + pName + " pOrig )" );
        zLines.add( "    {" );
        zLines.add( "        super( pOrig );" );
        zLines.add( "    }" );
        zLines.add( "" );
        zLines.add( "    @Override" );
        zLines.add( "    protected final " + pName + " copy()" );
        zLines.add( "    {" );
        zLines.add( "        return new " + pName + "( (" + pName + ") this );" );
        zLines.add( "    }" );
        zLines.add( "" );
        zLines.add( "    @Override" );
        zLines.add( "    protected final " + pName + " defaultInstance()" );
        zLines.add( "    {" );
        zLines.add( "        return DEFAULT_INSTANCE;" );
        zLines.add( "    }" );
        for ( AttributeData zAttribute : zNewAttributes ) {
            String zName = zAttribute.getName();
            if ( zName != null ) {
                int zID = zAttribute.getID();
                String zType = zAttribute.getType();
                zLines.add( "" );
                if ( !zAttribute.isRepeating() ) {
                    zLines.add( "    public " + zType + " get" + zName + "()" );
                    zLines.add( "    {" );
                    zLines.add( "        return getValueFor( " + zID + " );" );
                    zLines.add( "    }" );
                    zLines.add( "" );
                    zLines.add( "    public " + pName + " set" + zName + "( " + zType + " p" + zName + " )" );
                    zLines.add( "    {" );
                    zLines.add( "        return setValueFor( " + zID + ", p" + zName + " );" );
                    zLines.add( "    }" );
                } else {
                    zLines.add( "    public List<" + zType + "> get" + zName + "()" );
                    zLines.add( "    {" );
                    zLines.add( "        return getRepeatingValueFor( " + zID + " );" );
                    zLines.add( "    }" );
                    zLines.add( "" );
                    zLines.add( "    public " + pName + " clear" + zName + "()" );
                    zLines.add( "    {" );
                    zLines.add( "        return clearRepeatingValueFor( " + zID + " );" );
                    zLines.add( "    }" );
                    zLines.add( "" );
                    zLines.add( "    public " + pName + " add" + zName + "( " + zType + " p" + zName + " )" );
                    zLines.add( "    {" );
                    zLines.add( "        return addRepeatingValueFor( " + zID + ", p" + zName + " );" );
                    zLines.add( "    }" );
                    zLines.add( "" );
                    zLines.add( "    public " + pName + " set" + zName + "( List<" + zType + "> p" + zName + " )" );
                    zLines.add( "    {" );
                    zLines.add( "        return setRepeatingValueFor( " + zID + ", p" + zName + " );" );
                    zLines.add( "    }" );
                    zLines.add( "" );
                    zLines.add( "    public " + pName + " set" + zName + "( " + zType + "... p" + zName + " )" );
                    zLines.add( "    {" );
                    zLines.add( "        return setRepeatingValueFor( " + zID + ", p" + zName + " );" );
                    zLines.add( "    }" );
                }
            }
        }
        zLines.add( "" );
        zLines.add( "    public static final " + pName + " DEFAULT_INSTANCE = new " + pName + "();" );
        zLines.add( "}" );
        return zLines.toArray( new String[zLines.size()] );
    }

    private boolean anyRepeating( List<AttributeData> pAttributes ) {
        for ( AttributeData zAttribute : pAttributes ) {
            if ( zAttribute.isRepeating() ) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return if there were any attribute changes
     */
    private boolean convertAndMergeInto( List<AttributeData> pPrevAttributes, List<BeanAttributeBuilder> pNewAttributes, List<AttributeData> pMergedAttributes ) {
        pPrevAttributes = ConstrainTo.notNull( pPrevAttributes );
        pNewAttributes = Lists.from( pNewAttributes ); // Copy
        boolean zChanges = false;
        int zLastIndex = -1;
        for ( AttributeData zAttribute : pPrevAttributes ) {
            zLastIndex = zAttribute.getID();
            if ( zAttribute.getName() == null ) {
                pMergedAttributes.add( zAttribute );
                continue;
            }
            BeanAttributeBuilder zNewMatch = findMatch( zAttribute, pNewAttributes );
            if ( zNewMatch == null ) {
                zChanges = true;
                pMergedAttributes.add( zAttribute.makePlaceHolder() );
                continue;
            }
            AttributeData zNewAttribute = zAttribute.updateWith( zNewMatch );
            zChanges |= (zAttribute != zNewAttribute);
            pMergedAttributes.add( zNewAttribute );
        }
        for ( BeanAttributeBuilder zNewAttribute : pNewAttributes ) {
            zChanges = true;
            pMergedAttributes.add( new AttributeData( ++zLastIndex, zNewAttribute ) );
        }
        return zChanges;
    }

    private BeanAttributeBuilder findMatch( AttributeData pAttribute, List<BeanAttributeBuilder> pNewAttributes ) {
        for ( int i = 0; i < pNewAttributes.size(); i++ ) {
            BeanAttributeBuilder zNewAttribute = pNewAttributes.get( i );
            if ( pAttribute.isUpdatableFrom( zNewAttribute ) ) {
                pNewAttributes.remove( i );
                return zNewAttribute;
            }
        }
        return null; // Not Found
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

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
845 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 19:44:44 +0000
842 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 19:15:51 +0000
840 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 18:23:05 +0000
839 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 17:57:31 +0000
838 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 17:50:14 +0000
837 GeorgeS picture GeorgeS Wed 05 Sep, 2012 21:37:06 +0000