org.mentalog.config.ListConfigParam Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of menta-log Show documentation
Show all versions of menta-log Show documentation
A log library that embraces the kiss principle.
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();
}
}