/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.engine;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* This class contains an enumeration of constants that define different types
* of information needed for a certain engine to run, for example the location
* of the executable, the temporary directory, etc. This class needs to be
* subclassed to define the necessary constants for a certain engine.
*
* It implements the typesave enum pattern (Joshua Bloch, "Effective
* Java", Item 21) . If you compare ConfigKeys, always
* use the == operator.
*
* @author Markus Kraetzig
*/
public abstract class ConfigKeys {
private static final Logger log = Logger.getLogger(ConfigKeys.class);
/**
* The name of this ConfigKey.
*/
private final String name;
/**
* Creates a new config key.
*
* @param name
* the identifier of the new key
* @throws IllegalArgumentException
* if (name == null)
*/
public ConfigKeys(String name) {
if (name == null)
throw new IllegalArgumentException("Argument was null");
this.name = name;
}
/**
* Gets the default value for this config key. If no reasonable defaul
* exists for that setting, this method should return an empty string.
*
* @return string with default setting or "" if no reasonable
* default exists
*/
public abstract String defaultVal();
/**
* Override prevention method to enforce a.equals(b) if and
* only if a==b.
*
* @param o
* object to compare with
* @return true if equal, false otherwise
*/
public final boolean equals(Object o) {
return super.equals(o);
}
/**
* Gets a map of all public keys defined in class c. All key
* objects are stored with its name as the respective key.
*
* @return the config keys map
* @throws IllegalArgumentException
* if (c.getSuperclass() != ConfigKeys.class) or
* if (c == null)
*/
public static Map getAllKeys(Class c) {
if (c == null)
throw new IllegalArgumentException("Argument was null.");
if (c.getSuperclass() != ConfigKeys.class)
throw new IllegalArgumentException("Class argument: " + c
+ " not inherited from ConfigKeys.");
Field[] publicFields = c.getFields();
Map keysMap = new HashMap();
try {
for (int i = 0; i < publicFields.length; i++) {
Object obj = null;
if (publicFields[i].getType().equals(c))
obj = publicFields[i].get(publicFields[i].getType());
if (obj instanceof ConfigKeys) {
ConfigKeys key = (ConfigKeys) obj;
keysMap.put(key.name, (ConfigKeys) obj);
}
}
} catch (SecurityException e) {
log.error("Security error while reflective inspection", e);
} catch (IllegalAccessException e) {
log.error("Access error while reflective inspection", e);
}
return keysMap;
}
/**
* Override prevention method invokes super method.
*
* @return hash code
*/
public final int hashCode() {
return super.hashCode();
}
/**
* Gets whether value is valid for this.
*
* @param value
* a string value for this key
* @return null if valid, an error string if not valid
*/
public abstract String isValid(String value);
/**
* Gets the name of this.
*
* @return the name of the key
*/
public final String name() {
return name;
}
/**
* Gets a string representation.
*
* @return a string representation of this
*/
public final String toString() {
return name;
}
}
Ads help maintain this website.