001    package org.nakedobjects.applib.query;
002    
003    
004    /**
005     * Convenience adapter class for {@link Query}.
006     * 
007     * <p>
008     * Handles implementation of {@link #getResultType()}
009     */
010    public abstract class QueryAbstract<T> implements Query<T> {
011    
012            private static final long serialVersionUID = 1L;
013            
014            private final String resultTypeName;
015            /**
016             * Derived from {@link #getResultTypeName()}, with respect to the
017             * {@link Thread#getContextClassLoader() current thread's class loader}.
018             */
019            private transient Class<T> resultType;
020    
021            public QueryAbstract(final Class<T> type) {
022                    this.resultTypeName = type.getName();
023            }
024    
025            public QueryAbstract(final String typeName) {
026                    this.resultTypeName = typeName;
027            }
028    
029            /**
030             * @throws IllegalStateException
031             *             (wrapping a {@link ClassNotFoundException}) if the class
032             *             could not be determined.
033             */
034            @SuppressWarnings("unchecked")
035            public Class<T> getResultType() {
036                    if (resultType == null) {
037                            try {
038                                    resultType = (Class<T>) Thread.currentThread().getContextClassLoader()
039                                                    .loadClass(resultTypeName);
040                            } catch (ClassNotFoundException e) {
041                                    throw new IllegalStateException(e);
042                            }
043                    }
044                    return resultType;
045            }
046    
047            public String getResultTypeName() {
048                    return resultTypeName;
049            }
050            
051    }