View Javadoc
1   /*
2    *  Copyright 2010, 2011, 2012 Christopher Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jadira.usertype.spi.shared;
17  
18  import java.io.Serializable;
19  import java.util.Properties;
20  
21  import org.hibernate.usertype.EnhancedUserType;
22  import org.jadira.usertype.spi.utils.reflection.TypeHelper;
23  
24  public abstract class AbstractKnownClassHeuristicUserType<T> extends AbstractHeuristicUserType implements EnhancedUserType, Serializable {
25  
26  	private static final long serialVersionUID = 2233911693851349367L;
27  
28  	private Class<T> mappedClass;
29  
30  	protected void setMappedClass(Class<T> mappedClass) {
31      	this.mappedClass = mappedClass;
32      }
33      
34      protected Class<T> getMappedClass() {
35      	return mappedClass;
36      }
37          
38  	public void setParameterValues(Properties parameters) {
39  
40  		if (mappedClass == null) {
41  			
42  			throw new IllegalStateException("No mapped class was defined for " + this.getClass().getName());
43  		}		
44  		
45  		super.setParameterValues(parameters);
46  	}
47  
48      @SuppressWarnings("unchecked")
49      @Override
50      public Class<T> returnedClass() {
51          return (Class<T>) TypeHelper.getTypeArguments(AbstractKnownClassHeuristicUserType.class, getClass()).get(0);
52      }   
53  }