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

org.mentalog.config.ListConfigParam Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.mentalog.config;

import java.util.ArrayList;
import java.util.List;

public class ListConfigParam extends ConfigParam> {

	public ListConfigParam(String name, String addParam, String setParam, List defList) {
		super(name, copy(defList));
		
		String add = getString(addParam, null);
		String set = getString(setParam, null);
		
		if (add != null && set != null) {
			throw new IllegalStateException("Cannot pass both config params: " + addParam + " and " + setParam);
		}
		
		if (add != null) {
			String[] classes = add.split(",");
			for(String klass : classes) {
				values().add(loadClass(klass));
			}
			force();
		}
		if (set != null) {
			value().clear();
			String[] classes = set.split(",");
			for(String klass : classes) {
				values().add(loadClass(klass));
			}
			force();
		}
	}
	
	public void add(E e, boolean force) {
		value().add(e);
		if (force) force();
	}
	
	public void add(E e) {
		add(e, isForceMode());
	}
	
	public final List values() {
		return value();
	}
	
	@Override
	public void set(List values, boolean force) {
		if (!(isForced() && !force)) {
			values().clear();
			values().addAll(values);
		}
		super.set(values(), force);
	}

	@Override
	protected void init(String name) { }
	
	private static final  List copy(List list) {
		List newList = new ArrayList(32);
		if (list == null || list.isEmpty()) {
			return newList;
		}
		for(E e : list) {
			newList.add(e);
		}
		return newList;
	}
	
	@SuppressWarnings("unchecked")
	private E loadClass(String s) {
		try {
			Class c = Class.forName(s);
			
			return (E) c.newInstance();
			
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected List parseString(String s) {
		throw new UnsupportedOperationException();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy