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

it.espr.injector.Configuration Maven / Gradle / Ivy

There is a newer version: 0.2.6
Show newest version
package it.espr.injector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.inject.Named;

public abstract class Configuration {

	public class Binding {

		private final Bindings bindings;

		public final Class type;

		public List> implementations;

		public final Type instance;

		@SuppressWarnings("unchecked")
		public Binding(Bindings bindings, Type instance) {
			this.bindings = bindings;
			this.type = (Class) instance.getClass();
			this.instance = instance;
			this.bindings.bind(type, this.instance, null);
		}

		public Binding(Bindings bindings, Class type) {
			this.bindings = bindings;
			this.type = type;
			this.instance = null;
		}

		@SuppressWarnings("unchecked")
		public Binding to(Class... implementations) {
			this.implementations = new ArrayList<>();
			for (Class implementation : implementations) {
				if (!type.isAssignableFrom(implementation)) {
					throw new RuntimeException("Can't bind '" + implementation + "' to '" + type + "' - different types.");
				}
				String name = Utils.getAnnotationValue(Named.class, implementation.getAnnotations());
				this.bindings.bind(type, (Class) implementation, name);
				this.implementations.add((Class) implementation);
			}
			return this;
		}

		public void named(String name) {
			if (this.instance != null) {
				this.bindings.bind(type, this.instance, name);
			} else if (this.implementations != null) {
				for (Class impl : this.implementations) {
					this.bindings.bind(type, impl, name);
				}
			}
		}
	}

	protected class Bindings {

		private Map bindings = new HashMap<>();

		public  void bind(Class type, Type instance, String name) {
			String key = Utils.key(name, type);
			this.bindings.put(key, instance);
			if (instance instanceof Map) {
				this.bindings.put(Utils.key(name, Map.class), instance);
			} else if (instance instanceof List) {
				this.bindings.put(Utils.key(name, List.class), instance);
			} else if (instance instanceof Set) {
				this.bindings.put(Utils.key(name, Set.class), instance);
			}
		}

		public  void bind(Class type, Class implementation, String name) {
			String key = Utils.key(name, type);
			if (this.bindings.containsKey(key)) {
				throw new RuntimeException("Trying to bind multiple instances under the same name");
			}
			this.bindings.put(key, implementation);
		}

		public Object get(Class type) {
			return this.get(null, type);
		}

		public Object get(String name, Class type) {
			return this.bindings.get(Utils.key(name, type));
		}

		public boolean has(Class type) {
			return this.has(null, type);
		}

		public boolean has(String name, Class type) {
			return this.bindings.containsKey(Utils.key(name, type));
		}
	}

	final Bindings bindings;

	final Properties properties;

	protected Configuration() {
		this.bindings = new Bindings();
		this.properties = new Properties();
	}

	protected final  Binding bind(Class type) {
		return new Binding(this.bindings, type);
	}

	protected final  Binding bind(Type type) {
		return new Binding(this.bindings, type);

	}

	protected final boolean isBound(Class type) {
		return this.isBound(type, null);
	}

	protected final boolean isBound(Object type, String name) {
		return this.bindings.bindings.containsKey(Utils.key(name, type));
	}

	protected void configure() {
		// override for custom config
	};

	final void initialise() {
		Map properties = this.properties.load();
		for (Entry entry : properties.entrySet()) {
			this.bind(entry.getValue()).named(entry.getKey());
		}
		this.configure();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy