Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/Rest/RestfulDTOs/DTOsupport/src/org/litesoft/rest/adapters/JAXBgeneratedClassSuperClass.java

Diff revisions: vs.
  @@ -1,283 +1,282 @@
1 - package org.litesoft.rest.adapters;
2 -
3 - import java.io.*;
4 - import java.lang.reflect.*;
5 - import org.litesoft.core.typeutils.Objects;
6 - import java.util.*;
7 -
8 - import org.litesoft.rest.utils.*;
9 -
10 - /**
11 - * This class is the common super class of all JAXB generated classes, and is specifically for the purposes
12 - * of supporting a reflection based implementation of equals, hashCode, and toString.
13 - * <p/>
14 - * While the implementations are based on the Apache Commons reflection based implementations, we diverge
15 - * from those implementations by relying on the "public get???()" methods.
16 - */
17 - @javax.xml.bind.annotation.XmlTransient
18 - public class JAXBgeneratedClassSuperClass implements Serializable,
19 - ToStringBuildable
20 - {
21 - private static class RuntimeReflectionException extends RuntimeException
22 - {
23 - private RuntimeReflectionException( Throwable cause )
24 - {
25 - super( cause );
26 - }
27 - }
28 -
29 - private transient AccessorManager mAccessorManager;
30 -
31 - private AccessorManager getAssessorManager()
32 - {
33 - if ( mAccessorManager == null )
34 - {
35 - mAccessorManager = new AccessorManager( this.getClass() );
36 - }
37 - return mAccessorManager;
38 - }
39 -
40 - @Override
41 - public void toStringBuild( int pIndentLevel, StringBuilder pSB )
42 - {
43 - pSB.append( getClass().getSimpleName() ).append( ':' );
44 - pIndentLevel++;
45 - for ( Accessor zAccessor : getAssessorManager().getAccessors() )
46 - {
47 - Object zValue = zAccessor.getValue( this );
48 - if ( zValue != null )
49 - {
50 - pSB.append( '\n' );
51 - for ( int i = 0; i < pIndentLevel; i++ )
52 - {
53 - pSB.append( " " );
54 - }
55 - pSB.append( zAccessor.getGetterName() ).append( '=' );
56 - if ( zValue instanceof ToStringBuildable )
57 - {
58 - ((ToStringBuildable) zValue).toStringBuild( pIndentLevel, pSB );
59 - }
60 - else
61 - {
62 - pSB.append( zValue );
63 - }
64 - }
65 - }
66 - }
67 -
68 - @Override
69 - public String toString()
70 - {
71 - StringBuilder sb = new StringBuilder();
72 - toStringBuild( 0, sb );
73 - return sb.toString();
74 - }
75 -
76 - @Override
77 - public int hashCode()
78 - {
79 - int zHash = 0;
80 - for ( Accessor zAccessor : getAssessorManager().getAccessors() )
81 - {
82 - zHash *= 37;
83 - Object zValue = zAccessor.getValue( this );
84 - if ( zValue != null )
85 - {
86 - zHash += zValue.hashCode();
87 - }
88 - }
89 - return zHash;
90 - }
91 -
92 - /**
93 - * Reflection based equals (like the Apache Commons reflection based version- see class note) where one must be an
94 - * instance of the other and they must have the same getter methods!
95 - */
96 - @Override
97 - public boolean equals( Object obj )
98 - {
99 - return (this == obj) || ((obj instanceof JAXBgeneratedClassSuperClass) && equals( (JAXBgeneratedClassSuperClass) obj ));
100 - }
101 -
102 - @SuppressWarnings({"UnusedDeclaration"})
103 - public boolean hasValues()
104 - {
105 - for ( Accessor zAccessor : getAssessorManager().getAccessors() )
106 - {
107 - Object zValue = zAccessor.getValue( this );
108 - if ( zValue != null )
109 - {
110 - return true;
111 - }
112 - }
113 - return false;
114 - }
115 -
116 - @SuppressWarnings({"unchecked"})
117 - private static boolean acceptableEqualTypes( Class pThis, Class pThem )
118 - {
119 - return pThis.isAssignableFrom( pThem ) || pThem.isAssignableFrom( pThis );
120 - }
121 -
122 - private static boolean areEqual( Object pThis, Object pThem )
123 - {
124 - return (pThis == pThem) || ((pThis != null) && pThis.equals( pThem ));
125 - }
126 -
127 - private boolean equals( JAXBgeneratedClassSuperClass them )
128 - {
129 - if ( acceptableEqualTypes( this.getClass(), them.getClass() ) )
130 - {
131 - AccessorManager zAMthis = this.getAssessorManager();
132 - AccessorManager zAMthem = them.getAssessorManager();
133 -
134 - Accessor[] zThisAccessors = zAMthis.getAccessors();
135 - if ( zThisAccessors.length == zAMthem.getAccessors().length )
136 - {
137 - for ( int i = 0; i < zThisAccessors.length; i++ )
138 - {
139 - Accessor zThisAccessor = zThisAccessors[i];
140 - Accessor zThemAccessor = zAMthem.getAccessor( i, zThisAccessor ); // Null == Not Found
141 - if ( (zThemAccessor == null) || !areEqual( zThisAccessor.getValue( this ), zThemAccessor.getValue( them ) ) )
142 - {
143 - return false;
144 - }
145 - }
146 - return true;
147 - }
148 - }
149 - return false;
150 - }
151 -
152 - private static class AccessorManager
153 - {
154 - private Accessor[] mAccessors;
155 -
156 - public AccessorManager( Class pClass )
157 - {
158 - Method[] zMethods = pClass.getMethods();
159 - List<Accessor> zAccessors = new ArrayList<Accessor>( zMethods.length );
160 - for ( Method zMethod : zMethods )
161 - {
162 - if ( isGetter( zMethod ) )
163 - {
164 - String zName = zMethod.getName();
165 - zAccessors.add( new Accessor( zName.substring( 3 ), // drop the "get"
166 - zMethod ) );
167 - }
168 - }
169 - Arrays.sort( mAccessors = zAccessors.toArray( new Accessor[zAccessors.size()] ) );
170 - }
171 -
172 - private static boolean isGetter( Method pMethod )
173 - {
174 - String zName = pMethod.getName();
175 - return !void.class.equals( pMethod.getReturnType() ) && //
176 - !zName.equals( "getClass" ) && //
177 - (zName.indexOf( '_' ) == -1) && //
178 - zName.startsWith( "get" ) && //
179 - (pMethod.getParameterTypes().length == 0);
180 - }
181 -
182 - public Accessor[] getAccessors()
183 - {
184 - return mAccessors;
185 - }
186 -
187 - public Accessor getAccessor( int pStartingSearchIndex, Accessor pThemAccessor )
188 - {
189 - return find( pThemAccessor, 0, pStartingSearchIndex, mAccessors.length - 1 );
190 - }
191 -
192 - private Accessor find( Accessor pThemAccessor, int pFrom, int pAt, int pThru )
193 - {
194 - Accessor zAccessor;
195 - while ( !pThemAccessor.equals( zAccessor = mAccessors[pAt] ) )
196 - {
197 - int zCmp = zAccessor.compareTo( pThemAccessor );
198 - if ( zCmp == 0 ) // Same Name but not Equal == Different ReturnType
199 - {
200 - return null;
201 - }
202 - if ( zCmp > 0 ) // zAccessor > pThemAccessor
203 - {
204 - pThru = pAt - 1;
205 - }
206 - else // zAccessor < pThemAccessor
207 - {
208 - pFrom = pAt + 1;
209 - }
210 - if ( pFrom > pThru )
211 - {
212 - return null;
213 - }
214 - pAt = (pFrom + pThru) / 2; // hopefully optimized to a simple shift!
215 - }
216 - return zAccessor;
217 - }
218 - }
219 -
220 - private static final class Accessor implements Comparable<Accessor>
221 - {
222 - private String mGetterName;
223 - private Method mMethod;
224 - private Class mReturnType;
225 -
226 - public Accessor( String pGetterName, Method pMethod )
227 - {
228 - mGetterName = pGetterName;
229 - mReturnType = (mMethod = pMethod).getReturnType();
230 - }
231 -
232 - public String getGetterName()
233 - {
234 - return mGetterName;
235 - }
236 -
237 - public Object getValue( Object o )
238 - throws RuntimeReflectionException
239 - {
240 - try
241 - {
242 - return mMethod.invoke( o );
243 - }
244 - catch ( IllegalAccessException e )
245 - {
246 - throw new RuntimeReflectionException( e );
247 - }
248 - catch ( InvocationTargetException e )
249 - {
250 - throw new RuntimeReflectionException( e );
251 - }
252 - catch ( RuntimeException e )
253 - {
254 - throw new RuntimeReflectionException( e );
255 - }
256 - }
257 -
258 - @Override
259 - public int compareTo( Accessor them )
260 - {
261 - return this.mGetterName.compareTo( them.mGetterName );
262 - }
263 -
264 - public boolean equals( Accessor them )
265 - {
266 - return (this == them) || ((them != null) //
267 - && this.mGetterName.equals( them.mGetterName ) //
268 - && this.mReturnType.equals( them.mReturnType ));
269 - }
270 -
271 - @Override
272 - public boolean equals( Object obj )
273 - {
274 - return (this == obj) || ((obj instanceof Accessor) && equals( (Accessor) obj ));
275 - }
276 -
277 - @Override
278 - public int hashCode()
279 - {
280 - return mGetterName.hashCode();
281 - }
282 - }
283 - }
1 + package org.litesoft.rest.adapters;
2 +
3 + import java.io.*;
4 + import java.lang.reflect.*;
5 + import java.util.*;
6 +
7 + import org.litesoft.rest.utils.*;
8 +
9 + /**
10 + * This class is the common super class of all JAXB generated classes, and is specifically for the purposes
11 + * of supporting a reflection based implementation of equals, hashCode, and toString.
12 + * <p/>
13 + * While the implementations are based on the Apache Commons reflection based implementations, we diverge
14 + * from those implementations by relying on the "public get???()" methods.
15 + */
16 + @javax.xml.bind.annotation.XmlTransient
17 + public class JAXBgeneratedClassSuperClass implements Serializable,
18 + ToStringBuildable
19 + {
20 + private static class RuntimeReflectionException extends RuntimeException
21 + {
22 + private RuntimeReflectionException( Throwable cause )
23 + {
24 + super( cause );
25 + }
26 + }
27 +
28 + private transient AccessorManager mAccessorManager;
29 +
30 + private AccessorManager getAssessorManager()
31 + {
32 + if ( mAccessorManager == null )
33 + {
34 + mAccessorManager = new AccessorManager( this.getClass() );
35 + }
36 + return mAccessorManager;
37 + }
38 +
39 + @Override
40 + public void toStringBuild( int pIndentLevel, StringBuilder pSB )
41 + {
42 + pSB.append( getClass().getSimpleName() ).append( ':' );
43 + pIndentLevel++;
44 + for ( Accessor zAccessor : getAssessorManager().getAccessors() )
45 + {
46 + Object zValue = zAccessor.getValue( this );
47 + if ( zValue != null )
48 + {
49 + pSB.append( '\n' );
50 + for ( int i = 0; i < pIndentLevel; i++ )
51 + {
52 + pSB.append( " " );
53 + }
54 + pSB.append( zAccessor.getGetterName() ).append( '=' );
55 + if ( zValue instanceof ToStringBuildable )
56 + {
57 + ((ToStringBuildable) zValue).toStringBuild( pIndentLevel, pSB );
58 + }
59 + else
60 + {
61 + pSB.append( zValue );
62 + }
63 + }
64 + }
65 + }
66 +
67 + @Override
68 + public String toString()
69 + {
70 + StringBuilder sb = new StringBuilder();
71 + toStringBuild( 0, sb );
72 + return sb.toString();
73 + }
74 +
75 + @Override
76 + public int hashCode()
77 + {
78 + int zHash = 0;
79 + for ( Accessor zAccessor : getAssessorManager().getAccessors() )
80 + {
81 + zHash *= 37;
82 + Object zValue = zAccessor.getValue( this );
83 + if ( zValue != null )
84 + {
85 + zHash += zValue.hashCode();
86 + }
87 + }
88 + return zHash;
89 + }
90 +
91 + /**
92 + * Reflection based equals (like the Apache Commons reflection based version- see class note) where one must be an
93 + * instance of the other and they must have the same getter methods!
94 + */
95 + @Override
96 + public boolean equals( Object obj )
97 + {
98 + return (this == obj) || ((obj instanceof JAXBgeneratedClassSuperClass) && equals( (JAXBgeneratedClassSuperClass) obj ));
99 + }
100 +
101 + @SuppressWarnings({"UnusedDeclaration"})
102 + public boolean hasValues()
103 + {
104 + for ( Accessor zAccessor : getAssessorManager().getAccessors() )
105 + {
106 + Object zValue = zAccessor.getValue( this );
107 + if ( zValue != null )
108 + {
109 + return true;
110 + }
111 + }
112 + return false;
113 + }
114 +
115 + @SuppressWarnings({"unchecked"})
116 + private static boolean acceptableEqualTypes( Class pThis, Class pThem )
117 + {
118 + return pThis.isAssignableFrom( pThem ) || pThem.isAssignableFrom( pThis );
119 + }
120 +
121 + private static boolean areEqual( Object pThis, Object pThem )
122 + {
123 + return (pThis == pThem) || ((pThis != null) && pThis.equals( pThem ));
124 + }
125 +
126 + private boolean equals( JAXBgeneratedClassSuperClass them )
127 + {
128 + if ( acceptableEqualTypes( this.getClass(), them.getClass() ) )
129 + {
130 + AccessorManager zAMthis = this.getAssessorManager();
131 + AccessorManager zAMthem = them.getAssessorManager();
132 +
133 + Accessor[] zThisAccessors = zAMthis.getAccessors();
134 + if ( zThisAccessors.length == zAMthem.getAccessors().length )
135 + {
136 + for ( int i = 0; i < zThisAccessors.length; i++ )
137 + {
138 + Accessor zThisAccessor = zThisAccessors[i];
139 + Accessor zThemAccessor = zAMthem.getAccessor( i, zThisAccessor ); // Null == Not Found
140 + if ( (zThemAccessor == null) || !areEqual( zThisAccessor.getValue( this ), zThemAccessor.getValue( them ) ) )
141 + {
142 + return false;
143 + }
144 + }
145 + return true;
146 + }
147 + }
148 + return false;
149 + }
150 +
151 + private static class AccessorManager
152 + {
153 + private Accessor[] mAccessors;
154 +
155 + public AccessorManager( Class pClass )
156 + {
157 + Method[] zMethods = pClass.getMethods();
158 + List<Accessor> zAccessors = new ArrayList<Accessor>( zMethods.length );
159 + for ( Method zMethod : zMethods )
160 + {
161 + if ( isGetter( zMethod ) )
162 + {
163 + String zName = zMethod.getName();
164 + zAccessors.add( new Accessor( zName.substring( 3 ), // drop the "get"
165 + zMethod ) );
166 + }
167 + }
168 + Arrays.sort( mAccessors = zAccessors.toArray( new Accessor[zAccessors.size()] ) );
169 + }
170 +
171 + private static boolean isGetter( Method pMethod )
172 + {
173 + String zName = pMethod.getName();
174 + return !void.class.equals( pMethod.getReturnType() ) && //
175 + !zName.equals( "getClass" ) && //
176 + (zName.indexOf( '_' ) == -1) && //
177 + zName.startsWith( "get" ) && //
178 + (pMethod.getParameterTypes().length == 0);
179 + }
180 +
181 + public Accessor[] getAccessors()
182 + {
183 + return mAccessors;
184 + }
185 +
186 + public Accessor getAccessor( int pStartingSearchIndex, Accessor pThemAccessor )
187 + {
188 + return find( pThemAccessor, 0, pStartingSearchIndex, mAccessors.length - 1 );
189 + }
190 +
191 + private Accessor find( Accessor pThemAccessor, int pFrom, int pAt, int pThru )
192 + {
193 + Accessor zAccessor;
194 + while ( !pThemAccessor.equals( zAccessor = mAccessors[pAt] ) )
195 + {
196 + int zCmp = zAccessor.compareTo( pThemAccessor );
197 + if ( zCmp == 0 ) // Same Name but not Equal == Different ReturnType
198 + {
199 + return null;
200 + }
201 + if ( zCmp > 0 ) // zAccessor > pThemAccessor
202 + {
203 + pThru = pAt - 1;
204 + }
205 + else // zAccessor < pThemAccessor
206 + {
207 + pFrom = pAt + 1;
208 + }
209 + if ( pFrom > pThru )
210 + {
211 + return null;
212 + }
213 + pAt = (pFrom + pThru) / 2; // hopefully optimized to a simple shift!
214 + }
215 + return zAccessor;
216 + }
217 + }
218 +
219 + private static final class Accessor implements Comparable<Accessor>
220 + {
221 + private String mGetterName;
222 + private Method mMethod;
223 + private Class mReturnType;
224 +
225 + public Accessor( String pGetterName, Method pMethod )
226 + {
227 + mGetterName = pGetterName;
228 + mReturnType = (mMethod = pMethod).getReturnType();
229 + }
230 +
231 + public String getGetterName()
232 + {
233 + return mGetterName;
234 + }
235 +
236 + public Object getValue( Object o )
237 + throws RuntimeReflectionException
238 + {
239 + try
240 + {
241 + return mMethod.invoke( o );
242 + }
243 + catch ( IllegalAccessException e )
244 + {
245 + throw new RuntimeReflectionException( e );
246 + }
247 + catch ( InvocationTargetException e )
248 + {
249 + throw new RuntimeReflectionException( e );
250 + }
251 + catch ( RuntimeException e )
252 + {
253 + throw new RuntimeReflectionException( e );
254 + }
255 + }
256 +
257 + @Override
258 + public int compareTo( Accessor them )
259 + {
260 + return this.mGetterName.compareTo( them.mGetterName );
261 + }
262 +
263 + public boolean equals( Accessor them )
264 + {
265 + return (this == them) || ((them != null) //
266 + && this.mGetterName.equals( them.mGetterName ) //
267 + && this.mReturnType.equals( them.mReturnType ));
268 + }
269 +
270 + @Override
271 + public boolean equals( Object obj )
272 + {
273 + return (this == obj) || ((obj instanceof Accessor) && equals( (Accessor) obj ));
274 + }
275 +
276 + @Override
277 + public int hashCode()
278 + {
279 + return mGetterName.hashCode();
280 + }
281 + }
282 + }