Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Hibernate OGM, Domain model persistence for NoSQL datastores
   *
   * License: GNU Lesser General Public License (LGPL), version 2.1 or later
   * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
   */
  package org.hibernate.ogm.util.configurationreader.impl;
  
  import java.util.List;
 
A org.hibernate.ogm.util.configurationreader.spi.PropertyReaderContext which allows to retrieve properties by instantiating a given implementation type, e.g. specified as fully-qualified class name or class object.

Parameters:
<T> the type of the value to instantiate
Author(s):
Gunnar Morling
 
 
 	private static final Log log = LoggerFactory.make();
 
 	private Class<? extends T> defaultImplementation;
 	private Instantiator<T> instantiator;
 
 	DefaultClassPropertyReaderContext(ClassLoaderService classLoaderServiceObject valueString propertyNameClass<T> clazz, T defaultValueboolean isRequiredList<PropertyValidator<T>> validators) {
 		superclassLoaderServicevaluepropertyNameclazzdefaultValueisRequiredvalidators );
 		this. = classLoaderService;
 	}
 
 	public DefaultClassPropertyReaderContext<T> withDefaultImplementation(Class<? extends T> defaultImplementation) {
 		this. = defaultImplementation;
 		this. = null;
 		return this;
 	}
 
 	public DefaultClassPropertyReaderContext<T> withDefaultImplementation(String defaultImplementationName) {
 		this. = defaultImplementationName;
 		this. = null;
 		return this;
 	}

Sets an instantiator to be used to create an instance of the property. Currently not exposed on the SPI as it is only needed within this module. May be promoted to an SPI later on, if required.

Parameters:
instantiator the Instantiator to use to create the instance of the property
Returns:
an instance of the property
 
 		this. = instantiator;
 		return this;
 	}
 
 		this. = shortNameResolver;
 		return this;
 	}
 
 	public T getTypedValue() {
 		Instantiator<T> instantiator = this. != null ? this. : DefaultInstantiator.<T>getInstance();
 
 		Object configuredValue = getConfiguredValue();
 		Class<T> targetType = getTargetType();
 
 		T typedValue = null;
 
 		if ( configuredValue == null ) {
 			typedValue = getDefaultValueresolverinstantiator );
 		}
 		else if ( targetType.isAssignableFromconfiguredValue.getClass() ) ) {
 			@SuppressWarnings("unchecked")
 			T v = (T) configuredValue;
 			typedValue = v;
 		}
 		else if ( configuredValue instanceof Class ) {
 			Class<? extends T> configuredClazz = narrowDownClass( (Class<?>) configuredValuetargetType );
 			typedValue = instantiator.newInstanceconfiguredClazz );
 		}
 		else if ( configuredValue instanceof String ) {
 			Class<? extends T> configuredClazz = getClassFromString( (StringconfiguredValuetargetTyperesolver );
 			typedValue = instantiator.newInstanceconfiguredClazz );
		}
		else {
			throw .unexpectedInstanceTypegetPropertyName(), configuredValue.toString(), configuredValue.getClass(), targetType );
		}
		return typedValue;
	}
	private T getDefaultValue(ShortNameResolver resolverInstantiator<T> instantiator) {
		if ( getDefaultValue() != null ) {
			return getDefaultValue();
		}
		else if (  != null ) {
		}
		return  != null ? instantiator.newInstance ) : null;
	}
	private Class<? extends T> getClassFromString(String classNameClass<T> targetTypeShortNameResolver shortNameResolver) {
		if ( shortNameResolver.isShortNameclassName ) ) {
			className = shortNameResolver.resolveclassName );
		}
		Class<?> clazz = null;
		try {
			clazz = .classForNameclassName );
		}
		catch (ClassLoadingException e) {
			throw .unableToLoadClassgetPropertyName(), classNamee );
		}
		return narrowDownClassclazztargetType );
	}
	private Class<? extends T> narrowDownClass(Class<?> clazzClass<T> targetType) {
		if ( !targetType.isAssignableFromclazz ) ) {
			throw .unexpectedClassTypegetPropertyName(), clazztargetType );
		}
		@SuppressWarnings("unchecked")
		Class<T> typed = (Class<T>) clazz;
		return typed;
	}
	private static class NoOpNameResolver implements ShortNameResolver {
		static final NoOpNameResolver INSTANCE = new NoOpNameResolver();
		public boolean isShortName(String name) {
			return false;
		}
		public String resolve(String shortName) {
		}
	}
	private static class DefaultInstantiator<T> implements Instantiator<T> {
		@SuppressWarnings("rawtypes")
		private static final DefaultInstantiator<?> INSTANCE = new DefaultInstantiator();
		@SuppressWarnings("unchecked")
		public static <T> DefaultInstantiator<T> getInstance() {
		}
		public T newInstance(Class<? extends T> clazz) {
			try {
				return clazz.newInstance();
			}
			catch (Exception e) {
				throw .unableToInstantiateTypeclazze );
			}
		}
	}
New to GrepCode? Check out our FAQ X