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
package org.litesoft.rest.adapters;

import java.io.*;
import java.lang.reflect.*;
import org.litesoft.core.typeutils.Objects;
import java.util.*;

import org.litesoft.rest.utils.*;

/**
 * This class is the common super class of all JAXB generated classes, and is specifically for the purposes
 * of supporting a reflection based implementation of equals, hashCode, and toString.
 * <p/>
 * While the implementations are based on the Apache Commons reflection based implementations, we diverge
 * from those implementations by relying on the "public get???()" methods.
 */
@javax.xml.bind.annotation.XmlTransient
public class JAXBgeneratedClassSuperClass implements Serializable,
                                                     ToStringBuildable
{
    private static class RuntimeReflectionException extends RuntimeException
    {
        private RuntimeReflectionException( Throwable cause )
        {
            super( cause );
        }
    }

    private transient AccessorManager mAccessorManager;

    private AccessorManager getAssessorManager()
    {
        if ( mAccessorManager == null )
        {
            mAccessorManager = new AccessorManager( this.getClass() );
        }
        return mAccessorManager;
    }

    @Override
    public void toStringBuild( int pIndentLevel, StringBuilder pSB )
    {
        pSB.append( getClass().getSimpleName() ).append( ':' );
        pIndentLevel++;
        for ( Accessor zAccessor : getAssessorManager().getAccessors() )
        {
            Object zValue = zAccessor.getValue( this );
            if ( zValue != null )
            {
                pSB.append( '\n' );
                for ( int i = 0; i < pIndentLevel; i++ )
                {
                    pSB.append( "  " );
                }
                pSB.append( zAccessor.getGetterName() ).append( '=' );
                if ( zValue instanceof ToStringBuildable )
                {
                    ((ToStringBuildable) zValue).toStringBuild( pIndentLevel, pSB );
                }
                else
                {
                    pSB.append( zValue );
                }
            }
        }
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        toStringBuild( 0, sb );
        return sb.toString();
    }

    @Override
    public int hashCode()
    {
        int zHash = 0;
        for ( Accessor zAccessor : getAssessorManager().getAccessors() )
        {
            zHash *= 37;
            Object zValue = zAccessor.getValue( this );
            if ( zValue != null )
            {
                zHash += zValue.hashCode();
            }
        }
        return zHash;
    }

    /**
     * Reflection based equals (like the Apache Commons reflection based version- see class note) where one must be an
     * instance of the other and they must have the same getter methods!
     */
    @Override
    public boolean equals( Object obj )
    {
        return (this == obj) || ((obj instanceof JAXBgeneratedClassSuperClass) && equals( (JAXBgeneratedClassSuperClass) obj ));
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public boolean hasValues()
    {
        for ( Accessor zAccessor : getAssessorManager().getAccessors() )
        {
            Object zValue = zAccessor.getValue( this );
            if ( zValue != null )
            {
                return true;
            }
        }
        return false;
    }

    @SuppressWarnings({"unchecked"})
    private static boolean acceptableEqualTypes( Class pThis, Class pThem )
    {
        return pThis.isAssignableFrom( pThem ) || pThem.isAssignableFrom( pThis );
    }

    private static boolean areEqual( Object pThis, Object pThem )
    {
        return (pThis == pThem) || ((pThis != null) && pThis.equals( pThem ));
    }

    private boolean equals( JAXBgeneratedClassSuperClass them )
    {
        if ( acceptableEqualTypes( this.getClass(), them.getClass() ) )
        {
            AccessorManager zAMthis = this.getAssessorManager();
            AccessorManager zAMthem = them.getAssessorManager();

            Accessor[] zThisAccessors = zAMthis.getAccessors();
            if ( zThisAccessors.length == zAMthem.getAccessors().length )
            {
                for ( int i = 0; i < zThisAccessors.length; i++ )
                {
                    Accessor zThisAccessor = zThisAccessors[i];
                    Accessor zThemAccessor = zAMthem.getAccessor( i, zThisAccessor ); // Null == Not Found
                    if ( (zThemAccessor == null) || !areEqual( zThisAccessor.getValue( this ), zThemAccessor.getValue( them ) ) )
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    private static class AccessorManager
    {
        private Accessor[] mAccessors;

        public AccessorManager( Class pClass )
        {
            Method[] zMethods = pClass.getMethods();
            List<Accessor> zAccessors = new ArrayList<Accessor>( zMethods.length );
            for ( Method zMethod : zMethods )
            {
                if ( isGetter( zMethod ) )
                {
                    String zName = zMethod.getName();
                    zAccessors.add( new Accessor( zName.substring( 3 ), // drop the "get"
                                                  zMethod ) );
                }
            }
            Arrays.sort( mAccessors = zAccessors.toArray( new Accessor[zAccessors.size()] ) );
        }

        private static boolean isGetter( Method pMethod )
        {
            String zName = pMethod.getName();
            return !void.class.equals( pMethod.getReturnType() ) && //
                   !zName.equals( "getClass" ) && //
                   (zName.indexOf( '_' ) == -1) && //
                   zName.startsWith( "get" ) && //
                   (pMethod.getParameterTypes().length == 0);
        }

        public Accessor[] getAccessors()
        {
            return mAccessors;
        }

        public Accessor getAccessor( int pStartingSearchIndex, Accessor pThemAccessor )
        {
            return find( pThemAccessor, 0, pStartingSearchIndex, mAccessors.length - 1 );
        }

        private Accessor find( Accessor pThemAccessor, int pFrom, int pAt, int pThru )
        {
            Accessor zAccessor;
            while ( !pThemAccessor.equals( zAccessor = mAccessors[pAt] ) )
            {
                int zCmp = zAccessor.compareTo( pThemAccessor );
                if ( zCmp == 0 ) // Same Name but not Equal == Different ReturnType
                {
                    return null;
                }
                if ( zCmp > 0 ) // zAccessor > pThemAccessor
                {
                    pThru = pAt - 1;
                }
                else // zAccessor < pThemAccessor
                {
                    pFrom = pAt + 1;
                }
                if ( pFrom > pThru )
                {
                    return null;
                }
                pAt = (pFrom + pThru) / 2; // hopefully optimized to a simple shift!
            }
            return zAccessor;
        }
    }

    private static final class Accessor implements Comparable<Accessor>
    {
        private String mGetterName;
        private Method mMethod;
        private Class mReturnType;

        public Accessor( String pGetterName, Method pMethod )
        {
            mGetterName = pGetterName;
            mReturnType = (mMethod = pMethod).getReturnType();
        }

        public String getGetterName()
        {
            return mGetterName;
        }

        public Object getValue( Object o )
                throws RuntimeReflectionException
        {
            try
            {
                return mMethod.invoke( o );
            }
            catch ( IllegalAccessException e )
            {
                throw new RuntimeReflectionException( e );
            }
            catch ( InvocationTargetException e )
            {
                throw new RuntimeReflectionException( e );
            }
            catch ( RuntimeException e )
            {
                throw new RuntimeReflectionException( e );
            }
        }

        @Override
        public int compareTo( Accessor them )
        {
            return this.mGetterName.compareTo( them.mGetterName );
        }

        public boolean equals( Accessor them )
        {
            return (this == them) || ((them != null) //
                                      && this.mGetterName.equals( them.mGetterName ) //
                                      && this.mReturnType.equals( them.mReturnType ));
        }

        @Override
        public boolean equals( Object obj )
        {
            return (this == obj) || ((obj instanceof Accessor) && equals( (Accessor) obj ));
        }

        @Override
        public int hashCode()
        {
            return mGetterName.hashCode();
        }
    }
}

Commits for litesoft/trunk/Java/Rest/RestfulDTOs/DTOsupport/src/org/litesoft/rest/adapters/JAXBgeneratedClassSuperClass.java

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

Extracting commonfoundation

793 GeorgeS picture GeorgeS Sun 12 Aug, 2012 22:56:28 +0000