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

se.jbee.inject.config.Options Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
/*
 *  Copyright (c) 2012, Jan Bernitt 
 *			
 *  Licensed under the Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
 */
package se.jbee.inject.config;

import java.util.EnumSet;
import java.util.IdentityHashMap;

/**
 * {@link Options} are used to model configurations of the bootstrapping process through one enum
 * for each configurable property (each property is identified by the enum's {@link Class} object).
 * 
 * Each property can be used as a set or single associate value. So a option property can describe
 * either alternatives where one should be chosen or options with multiple choice. It is up to the
 * author of the module to decide and use correctly.
 * 
 * {@linkplain Options} are immutable! Use {@link #chosen(Enum)} to build up sets.
 * 
 * @author Jan Bernitt ([email protected])
 */
public final class Options {

	public static final Options STANDARD = new Options(
			new IdentityHashMap>, EnumSet>() );

	private final IdentityHashMap>, EnumSet> properties;

	private Options( IdentityHashMap>, EnumSet> properties ) {
		this.properties = properties;
	}

	public > boolean isChosen( Class property, C option ) {
		EnumSet options = properties.get( property );
		return options == null || options.isEmpty()
			? ( option == null )
			: options.contains( option );
	}

	public > Options chosen( C option ) {
		if ( option == null ) {
			return this;
		}
		return with( option.getDeclaringClass(), EnumSet.of( option ) );
	}

	private > Options with( Class property, EnumSet options ) {
		IdentityHashMap>, EnumSet> clone = copy();
		clone.put( property, options );
		return new Options( clone );
	}

	public final > Options chosen( C... options ) {
		if ( options.length == 0 ) {
			return this;
		}
		return with( options[0].getDeclaringClass(), EnumSet.of( options[0], options ) );
	}

	@SuppressWarnings ( "unchecked" )
	private IdentityHashMap>, EnumSet> copy() {
		return (IdentityHashMap>, EnumSet>) properties.clone();
	}

	@Override
	public boolean equals( Object obj ) {
		return obj instanceof Options && properties.equals( ( (Options) obj ).properties );
	}

	@Override
	public int hashCode() {
		return properties.hashCode();
	}

	@Override
	public String toString() {
		return properties.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy