All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.refcodes.decoupling.InitializerClaim Maven / Gradle / Ivy

Go to download

Artifact for breaking up dependencies between components or modules of a software system (dependency injection and inversion of control).

There is a newer version: 3.3.8
Show newest version
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// /////////////////////////////////////////////////////////////////////////////
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// -----------------------------------------------------------------------------
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// -----------------------------------------------------------------------------
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// -----------------------------------------------------------------------------
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////

package org.refcodes.decoupling;

import java.util.function.BiFunction;

/**
 * A {@link InitializerClaim} extends a {@link Claim} and uses a
 * {@link BiFunction} upon a {@link Dependency} to initialize a given instance.
 *
 * @param  The type of the {@link Dependency} claimed for setting up an
 *        instance of the given type.
 * @param  The type of the instance to be initialize.
 */
public class InitializerClaim extends Claim {

	// /////////////////////////////////////////////////////////////////////////
	// VARIABLES:
	// /////////////////////////////////////////////////////////////////////////

	private final BiFunction _initializer;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTRUCTORS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Creates a new {@link InitializerClaim}.
	 * 
	 * @param aType The type of the {@link Dependency} required by the
	 *        {@link InitializerClaim}.
	 * @param aInitializer The processor to be used by the
	 *        {@link InitializerClaim} instance.
	 */
	public InitializerClaim( Class aType, BiFunction aInitializer ) {
		super( aType );
		_initializer = aInitializer;
	}

	/**
	 * Creates a new {@link InitializerClaim}.
	 *
	 * @param aType The type of the {@link Dependency} required by the
	 *        {@link InitializerClaim}.
	 * @param aInitializer The processor to be used by the
	 *        {@link InitializerClaim} instance.
	 * @param aAlias The alias for the {@link InitializerClaim}.
	 */
	public InitializerClaim( Class aType, BiFunction aInitializer, String aAlias ) {
		super( aType, aAlias );
		_initializer = aInitializer;
	}

	// /////////////////////////////////////////////////////////////////////////
	// METHODS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Initializes the provided instance.
	 * 
	 * @param aClaim The {@link Dependency} declaration required for
	 *        initialization.
	 * @param aInstance The instance to be initialized.
	 * 
	 * @return The initialized instance.
	 */
	public T initialize( C aClaim, T aInstance ) {
		return _initializer.apply( aInstance, aClaim );
	}

	/**
	 * Returns the type of the {@link Dependency} required by this
	 * {@link InitializerClaim} instance when invoking
	 * {@link #initialize(Object, Object)}.
	 * 
	 * @return The according type.
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Class getType() {
		return (Class) _type;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public DependencySchema toSchema() {
		return new DependencySchema( this );
	}

	// /////////////////////////////////////////////////////////////////////////
	// HOOKS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Gets the setup.
	 *
	 * @return the setup
	 */
	BiFunction getSetup() {
		return _initializer;
	}

	// /////////////////////////////////////////////////////////////////////////
	// HELPER:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Forces calling {@link #initialize(Object, Object)} by doing an unsafe
	 * cast.
	 *
	 * @param aClaim The {@link Dependency} declaration required for
	 *        initialization.
	 * @param aInstance The instance to be initialized.
	 * 
	 * @return The initialized instance.
	 */
	@SuppressWarnings("unchecked")
	T toInstance( Object aClaim, T aInstance ) {
		return initialize( (C) aClaim, aInstance );
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy