avoid repetative calling set method for java class with similar fields - java

I was given a class (which I could not change) with a lot of fields of String, each of them have a set method.
public class C {
private String fieldA,
private String fieldB,
...
public void setFieldA(String s) {
this.fieldA = s;
}
public void setFieldB(String s) {
this.fieldB = s;
}
...
}
I need to write code to create an instance of the class and initialize it with a json object (which will have value for some or all of the fields).
The json is very simple and looks like:
{"fieldA":"valueA", "fieldB":"valueB"..... }
I know I could write something like below for every field:
if (myJsonObj.containsKey("fieldA"){
myInstance.setFieldA(myJsonObj.get("fieldA").toString());
}
I'm wondering if there is a smarter way I could do this, since the number of fields could be up to about thirty. Instead of repeating the above code thirty times (and the only thing different between each is "fieldA" in the above code).
Thanks!

Sounds like a job for reflection to me. Everything except the bit marked with "Reflection" is here solely to make this an SSCCE. You'll likely have your own way to track which methods are available or whatever. Take a look at the following site for an (IMHO) excellent guide (I am not affiliated with the site in any way): Guide to Java Reflection
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
private String fieldA;
private String fieldB;
private String fieldC;
private String fieldD;
private String fieldE;
public String getFieldA() {
return fieldA;
}
public void setFieldA(final String fieldA) {
this.fieldA = fieldA;
}
public String getFieldB() {
return fieldB;
}
public void setFieldB(final String fieldB) {
this.fieldB = fieldB;
}
public String getFieldC() {
return fieldC;
}
public void setFieldC(final String fieldC) {
this.fieldC = fieldC;
}
public String getFieldD() {
return fieldD;
}
public void setFieldD(final String fieldD) {
this.fieldD = fieldD;
}
public String getFieldE() {
return fieldE;
}
public void setFieldE(final String fieldE) {
this.fieldE = fieldE;
}
private static final char[] chars = {'A', 'B', 'C', 'D', 'E'};
private static final String[] values =
{"valueA", "valueB", "valueC", "valueD", "valueE"};
private static final Class<?>[] types = {String.class};
public static void main(final String[] args)
throws SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
// Reflection setting values - everything else is here solely to make this an SSCCE
final Test test = new Test();
final Class<Test> clazz = Test.class;
for(int i = 0; i < chars.length; i++) {
final String name = "setField" + chars[i];
final Method method = clazz.getDeclaredMethod(name, types);
final Object[] params = {values[i]};
method.invoke(test, params);
}
// End of reflection
System.out.println(test.getFieldA());
System.out.println(test.getFieldB());
System.out.println(test.getFieldC());
System.out.println(test.getFieldD());
System.out.println(test.getFieldE());
}
}

Thanks to Chris Parker.
Here is my final code based on his original code. Because there are many String fields in the class and it might change in the future, so using reflect makes the update/change very easy: I only need to change the String Array.
The given class is like below (only String fields and their set methods are shown):
public class MyClass {
private String fieldA,
private String fieldB,
...
public void setFieldA(String s) {
this.fieldA = s;
}
public void setFieldB(String s) {
this.fieldB = s;
}
...
}
The method to set the value of those string fields using a JSONObject input to an instance of C:
public void fillClassStringFields(MyClass c, JSONObject input) {
String[] names = {"FieldA","FieldB",...};
Class<MyClass> clazz = MyClass.class;
Class<?>[] types = {String.class};
for(int i = 0; i< names.length;i++) {
String name = names[i].toString();
String methodName = "set" + name;
if(input.containsKey(name)) {
try {
Method method = clazz.getDeclaredMethod(methodName, types);
method.invoke(c, input.get(name).toString());
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}

Related

Method not available in Generic Return Object

I am trying to access the method GetDatbaseName(), from the returned object obj, but it is returning error that the method is not available.
However, when I Typecast the obj, it is working.
String name = ((Oracle)obj).GetDatabaseName();
How to handle this generic? Like I can't typecast for each return type like Oracle and MongoDB. Also any better implementation for this?
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
Data dt = new Data("Oracle");
Object obj = dt.GetObject();
String name = obj.GetDatabaseName();
System.out.println(name);
}
}
public class Data
{
public String _type;
public Data(String type)
{
_type = type;
}
public Object GetObject()
{
Object obj = null;
switch(_type)
{
case("Oracle"):
obj = new Oracle("Test");
break;
case("MongoDB"):
obj = new MongoDB("TestCollection");
break;
}
return obj;
}
}
public class Oracle
{
public String _databaseName;
public Oracle(String databaseName)
{
_databaseName = databaseName;
}
public String GetDatabaseName() { return _databaseName; }
}
public class MongoDB
{
public String _collectionName;
public MongoDB(String collectionName)
{
_collectionName = collectionName;
}
public String GetCollectionName() { return _collectionName; }
}
There are two ways to solve this, the first is using a generic class, while the second is using interface, the second approach is better if you know that the classes will have the same methods, while the generic approach is if the classes have different methods
Generic approach
public class DBtest{
public static void main(String[] args){
DataBase<Oracle> database = new DataBase<>(Oracle.class);
Oracle oracle = database.getDataBase();
System.out.println(oracle.getDatabaseName());
}
}
class DataBase<T>{
private T database;
public DataBase(Class<T> classOfT){
try {
database = classOfT.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public T getDataBase(){
return database;
}
}
class Oracle{
private String _databaseName;
public Oracle(){
_databaseName = "test";
}
public String getDatabaseName() { return _databaseName; }
}
As you can see, it is not possible to define the name of the database, this would be possible of you write <T extends Name> which is an interface which has getName() and setName() method
Interface approach
public class DBtest{
// arguments are passed using the text field below this editor
public static void main(String[] args){
DataBase database = new DataBase(new Oracle("test"));
DatabaseName databaseName = database.getDataBase();
System.out.println(databaseName.getName());
}
}
interface DatabaseName {
String getName();
}
class DataBase{
private DatabaseName databaseName;
public DataBase(DatabaseName databaseName){
this.databaseName = databaseName;
}
public DatabaseName getDataBase(){
return databaseName;
}
}
class Oracle implements DatabaseName {
private String _databaseName;
public Oracle(String name){
_databaseName = name;
}
public String getName() {
return _databaseName;
}
}
class MongoDB implements DatabaseName {
private String _databaseName;
public MongoDB(String name){
_databaseName = name;
}
public String getName() {
return _databaseName;
}
}
Obviously DatabaseName is a bad name for an interface, but it is the only method which is the same for both classes, so it makes sense to call it that. The great thing about interfaces is that you don't have to give a shit about what class is used as long as you know the method names.
You problem is on the following lines:
Object obj = dt.GetObject();
String name = obj.GetDatabaseName();
As far as those lines are concerned, obj is of type Object, which does not have the invoked method; thus, the issue. This is due to Java being strongly typed.
To go around that, you need a type that has this method, or use reflection. To use a type that has this method, they need to inherit it from a common parent of implement it from a common interface. You can also wrap you objects or a bunch of other alternatives.
In your case, it seems that a common interface is the easiest way to go. In this case, each class should implement this interface and instead of using Object your reference would be of the type of that interface.
public Object GetObject()
Would become
public MyInterface GetObject()
and
public class Oracle
would be
public class Oracle implements MyInterface
Where MyInterface would declare the method
public interface MyInterface {
String GetDatabaseName();
}
Being mindful of Java conventions, methods should start with lowercase
public interface MyInterface {
String getDatabaseName();
}
In the case where you cannot change the code in order to implements those methods, you can use "instanceof" to test against the class type.
name = (obj instanceof Oracle)?((Oracle)obj).GetDatabaseName():((MongoDB )obj).getCollectionName();
You must have to create an Interface and then with getDatabaseName() method. Then your objects Oracle and MongoDB must implement that interface.
What you are trying to do is something similar to AbstractFactory Pattern. You should google it.
public interface MyDbInterface {
String getDatabaseName();
}
public class HelloWorld {
// arguments are passed using the text field below this editor
public static void main(String[] ){
MyDbInterface dt = DataFactory.create("Oracle");
String name = dt.getDatabaseName();
System.out.println(name);
}
}
public final class DataFactory{
private DataFactory(){
super();
}
public static MyDbInterface create(String type){
MyDbInterface obj = null;
switch(type) {
case("Oracle"):
obj = new Oracle("Test");
break;
case("MongoDB"):
obj = new MongoDB("TestCollection");
break;
}
return obj;
}
}
public class Oracle implement MyDbInterface{
public String databaseName;
public Oracle(String databaseName){
databaseName = databaseName;
}
#Override
public String getDatabaseName() {
return databaseName;
}
}
public class MongoDB implement MyDbInterface{
public String collectionName;
public MongoDB(String collectionName){
collectionName = collectionName;
}
public String getCollectionName() {
return collectionName;
}
#Override
public String getDatabaseName() {
return getCollectionName();
}
}
I suposed you come from C#, check java style guide. ;)
You should think about the design of your code. You need to use basic OOP principal to solve the problem. There are several ways to solve your problem like using interface/generics etc. Here I am giving one such example.
public class HelloWorld {
public static void main(String[] args) {
Data dt = new Data("Oracle");
DataBase obj = dt.GetObject();
String name = obj.getDatabaseName();
System.out.println("Name : "+name);
}
}
class Data {
public String _type;
public Data(String type) {
_type = type;
}
public DataBase GetObject() {
DataBase dataBase=null;
switch (_type) {
case "Oracle":
dataBase = new Oracle();
break;
case "Mongo":
dataBase = new MongoDb();
break;
}
return dataBase;
}
}
interface DataBase {
String getDatabaseName();
}
class Oracle implements DataBase {
public String getDatabaseName() {
return "Oracle";
}
}
class MongoDb implements DataBase {
public String getDatabaseName() {
return "Mongo";
}
}
Edited:
Here is another way to solve your problem. I believe this approach might solve your problem.
public class HelloWorld {
public static void main(String[] args) {
Data<Oracle> dt = new Data<Oracle>("Oracle");
Oracle obj = dt.getObject();
String name = obj.getDatabaseName();
System.out.println("Name : "+name);
}
}
class Data<T> {
public String _type;
public Data(String type) {
_type = type;
}
public T getObject() {
Object dataBase=null;
switch (_type) {
case "Oracle":
dataBase = new Oracle();
break;
case "Mongo":
dataBase = new MongoDb();
break;
}
return (T)dataBase;
}
}
class Oracle {
public String getDatabaseName() {
return "Oracle";
}
}
class MongoDb {
}

Finding all private fields and their corresponding getters / setters for nested classes

Situation: few apps communicate using Java DTOs.
I have classes which holds as its fields another classes and they hold another another classes (up to three levels down from top DTO).
Fields could be single DTO or as (exclusively) ArrayList of other classes (DTOs).
All classes are DTO. Just private fields and public setters and getters.
Now, when I get top DTO is there any way to inspect it and get all getters, including nested ones, read fields through getters and then do what I have to do (change some data, specifically remove/change some characters (I have method which does that, all final fields are eventually Strings or Integers), and then write data back using appropriate setter. I guess the best would be to find getter/setter pair per final field and do operation then move to next. Upon finding final (lowest level field) I should check if it is String (do the operation) and if Integer skip operation.
I know there is similar question but it doesn't deal with nested DTOs.
Java reflection get all private fields
If possible I would avoid any 3rd party library.
Any advice on this?
UPDATE: Almost there. Here is kind of demo code, I wish it is so simple, but conceptually it is more less like that:
class SymposiaDTO
import java.util.ArrayList;
public class SymposiaDTO {
private ProgramDTO programDTO;
private ArrayList<PaperDTO> papersDTO;
public ProgramDTO getProgramDTO() {
return programDTO;
}
public void setProgramDTO(ProgramDTO programDTO) {
this.programDTO = programDTO;
}
public ArrayList<PaperDTO> getPapersDTO() {
return papersDTO;
}
public void setPapersDTO(ArrayList<PaperDTO> papersDTO) {
this.papersDTO = papersDTO;
}
}
class ProgramDTO
public class ProgramDTO {
String programTitle;
Integer programID;
public String getProgramTitle() {
return programTitle;
}
public void setProgramTitle(String programTitle) {
this.programTitle = programTitle;
}
public Integer getProgramID() {
return programID;
}
public void setProgramID(Integer programID) {
this.programID = programID;
}
}
class PaperDTO
import java.util.ArrayList;
public class PaperDTO {
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ArrayList<AuthorDTO> getAuthrosDTO() {
return authrosDTO;
}
public void setAuthrosDTO(ArrayList<AuthorDTO> authrosDTO) {
this.authrosDTO = authrosDTO;
}
private String title;
private ArrayList<AuthorDTO> authrosDTO;
}
class AuthorDTO
public class AuthorDTO {
private String address;
private String name;
private String title;
private String age;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
class Controller <--- by Carlos if I got his instructions right, this version gives no output at all, never even get's single iteration in for loop.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Controller {
#SuppressWarnings({ "unused", "rawtypes" })
public static void main(String[] args) {
SymposiaDTO symposiaDTO = new SymposiaDTO();
ProgramDTO programDTO = new ProgramDTO();
PaperDTO paperDTO = new PaperDTO();
AuthorDTO authorDTO = new AuthorDTO();
Class<?> topClass = symposiaDTO.getClass();
for (Class<?> innerClass : topClass.getDeclaredClasses()) {
for (Field field : innerClass.getDeclaredFields()) {
if (Modifier.isPrivate(field.getModifiers())) {
String name = Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
Method getter;
try {
getter = innerClass.getDeclaredMethod("get" + name);
} catch (Exception ex) {
getter = null;
}
Method setter;
try {
setter = innerClass.getDeclaredMethod("set" + name, field.getType());
} catch (Exception ex) {
setter = null;
}
// TODO real work...
System.out.printf("%s: getter=%s, setter=%s%n", innerClass.getSimpleName(), getter, setter);
}
}
}
}
}
class Controller2 <--- slightly modified previous version, this gets into the loop, but runs twice, and it never gets deeper into nested DTOs.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Controller2 {
#SuppressWarnings({ "unused", "rawtypes" })
public static void main(String[] args) {
SymposiaDTO symposiaDTO = new SymposiaDTO();
ProgramDTO programDTO = new ProgramDTO();
PaperDTO paperDTO = new PaperDTO();
AuthorDTO authorDTO = new AuthorDTO();
Class<?> topClass = symposiaDTO.getClass();
List<Class> classesToWalk = new ArrayList<Class>();
for (Field field : topClass.getDeclaredFields()) {
Class symposiaDTO2 = field.getDeclaringClass();
classesToWalk.add(symposiaDTO2);
}
for (Class<?> innerClass : classesToWalk) {
Field[] fields = Arrays.stream(innerClass.getDeclaredFields())
.filter(field -> Modifier.isPrivate(field.getModifiers())).toArray(Field[]::new);
for (Field field : fields) {
String name = Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
Method getter;
try {
getter = innerClass.getDeclaredMethod("get" + name);
} catch (Exception ex) {
getter = null;
}
Method setter;
try {
setter = innerClass.getDeclaredMethod("set" + name, field.getType());
} catch (Exception ex) {
setter = null;
}
// TODO real work...
System.out.printf("%s: getter=%s, setter=%s%n", innerClass.getSimpleName(), getter, setter);
}
}
}
}
This is output from Controller2:
SymposiaDTO: getter=public ProgramDTO SymposiaDTO.getProgramDTO(),
setter=public void SymposiaDTO.setProgramDTO(ProgramDTO)
SymposiaDTO: getter=public java.util.ArrayList
SymposiaDTO.getPapersDTO(), setter=public void
SymposiaDTO.setPapersDTO(java.util.ArrayList)
SymposiaDTO: getter=public ProgramDTO SymposiaDTO.getProgramDTO(),
setter=public void SymposiaDTO.setProgramDTO(ProgramDTO)
SymposiaDTO: getter=public java.util.ArrayList
SymposiaDTO.getPapersDTO(), setter=public void
SymposiaDTO.setPapersDTO(java.util.ArrayList)
You could use getDeclaredClasses to find nested classes, then find the private fields and finally the getters and setters:
Class<?> topClass = ...
for (Class<?> innerClass : topClass.getDeclaredClasses()) {
for (Field field : innerClass.getDeclaredFields()) {
if (Modifier.isPrivate(field.getModifiers())) {
String name = Character.toUpperCase(field.getName().charAt(0))
+ field.getName().substring(1);
Method getter;
try {
getter = innerClass.getDeclaredMethod("get" + name);
} catch (Exception ex) {
getter = null;
}
Method setter;
try {
setter = innerClass.getDeclaredMethod("set" + name, field.getType());
} catch (Exception ex) {
setter = null;
}
// TODO real work...
System.out.printf("%s: getter=%s, setter=%s%n",
innerClass.getSimpleName(), getter, setter);
}
}
}
Edit: above code is valid for "nested classes" as mentioned in the questions title. After the sample code was added to the question it seems like the question is about getters and setters of the fields of the class:
Use getDeclaredFields to get all fields of the class and find the corresponding getter and setter as above; use getType to get the type (class) of each field and (recursively) start over with that class.

Retrieve a static variable using its name dynamically using reflection

How to retrieve a static variable using its name dynamically using Java reflection?
If I have class containing some variables:
public class myClass {
final public static string [][] cfg1= {{"01"},{"02"},{"81"},{"82"}};
final public static string [][]cfg2= {{"c01"},{"c02"},{"c81"},{"c82"}};
final public static string [][] cfg3= {{"d01"},{"d02"},{"d81"}{"d82"}};
final public static int cfg11 = 5;
final public static int cfg22 = 10;
final public static int cfg33 = 15;
}
And in another class I want variable name is input from user:
class test {
Scanner in = new Scanner(System.in);
String userInput = in.nextLine();
// get variable from class myClass that has the same name as userInput
System.out.println("variable name " + // correct variable from class)
}
Using reflection. Any help please?
You need to make use of java reflect. Here is a sample code. For example, I accessed 'cfg1' variable using java reflection, and then printed it into the console. Look into the main method carefully. I have handled no exceptions for simplification. The key line here is:
(String[][]) MyClass.class.getField("cfg1").get(MyClass.class);
__ ^typecast__ ^accessingFeild______________ ^accessFromClassDefinition
public class MyClass {
final public static String[][] cfg1 = { { "01" }, { "02" }, { "81" },
{ "82" } };
final public static String[][] cfg2 = { { "c01" }, { "c02" }, { "c81" },
{ "c82" } };
final public static String[][] cfg3 = { { "d01" }, { "d02" }, { "d81" },
{ "d82" } };
final public static int cfg11 = 5;
final public static int cfg22 = 10;
final public static int cfg33 = 15;
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, NoSuchFieldException, SecurityException {
String[][] str = (String[][]) MyClass.class.getField("cfg1").get(
MyClass.class);
for (String[] strings : str) {
for (String string : strings) {
System.out.println(string);
}
}
}
}
If I well understood your needs, this could suit them:
// user input, hardcoded for the example
String fieldName = "cfg22";
MyClass blank = new MyClass();
Object value = null;
try {
value = MyClass.class.getDeclaredField(fieldName).get(blank);
} catch (IllegalArgumentException e) {
// if the specified object is not an instance of the class or
// interface declaring the underlying field (or a subclass or
// implementor thereof)
e.printStackTrace();
} catch (SecurityException e) {
// if a security manager, s, is present [and restricts the access to
// the field]
e.printStackTrace();
} catch (IllegalAccessException e) {
// if the underlying field is inaccessible
e.printStackTrace();
} catch (NoSuchFieldException e) {
// if a field with the specified name is not found
e.printStackTrace();
}
System.out.println(value);
Prints 10.
I merge the two above solutions and get:
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
public class MyClass
{
final public static String[][] cfg1 = {{"01"}, {"02"}, {"81"},
{"82"}};
final public static String[][] cfg2 = {{"c01"}, {"c02"}, {"c81"},
{"c82"}};
final public static String[][] cfg3 = {{"d01"}, {"d02"}, {"d81"},
{"d82"}};
final public static int cfg11 = 5;
final public static int cfg22 = 10;
final public static int cfg33 = 15;
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, NoSuchFieldException, SecurityException
{
for (Field field : MyClass.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
System.out.println("Non-static field: " + field.getName());
}
else {
System.out.println("Static field: " + field.getName());
Object obj = MyClass.class.getField(field.getName()).get(MyClass.class);
if (obj instanceof String[][]) {
String[][] cad = (String[][]) obj;
for (String[] strings : cad) {
System.out.println("Values:: " + Arrays.toString(strings));
}
}
else {
System.out.println(" " + obj.toString());
}
}
}
}
}
You can try something like this :
for (Field field : myClass.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
System.out.println("Non-static field: " + field.getName());
}
else {
System.out.println("Static field: " + field.getName());
}
}
Use Field#get(Object obj) to get the value .
Note: Please follow Java naming conventions.
Just call Class.getField() or Class.getDeclaredField(), then call Field.getValue() on the result, providing null (or the class itself) as the parameter in the case of a static variable, or an instance of the class in the case of an instance variable.

string cannot be resolved to a type

I am getting a "string cannot be resolved to a type" error in my code.
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
FormLetter template;
template = new FormLetter();
template.print();
template.composeFormLetter(Sean, Colbert, God);
template.print();
}
}
class FormLetter
{
public void print(){
System.out.println(to + candidate + from);
}
public void composeFormLetter(string t, string c, string f){
to = t;
candidate = c;
from = f;
}
private string to;
private string candidate;
private string from;
}
String is capitalized in Java. You cannot use lowercase string. (That's C#)
Side question:
How is the following compiling at all?
template.composeFormLetter(Sean, Colbert, God);

Configurable Values in Enum

I often use this design in my code to maintain configurable values. Consider this code:
public enum Options {
REGEX_STRING("Some Regex"),
REGEX_PATTERN(Pattern.compile(REGEX_STRING.getString()), false),
THREAD_COUNT(2),
OPTIONS_PATH("options.config", false),
DEBUG(true),
ALWAYS_SAVE_OPTIONS(true),
THREAD_WAIT_MILLIS(1000);
Object value;
boolean saveValue = true;
private Options(Object value) {
this.value = value;
}
private Options(Object value, boolean saveValue) {
this.value = value;
this.saveValue = saveValue;
}
public void setValue(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public String getString() {
return value.toString();
}
public boolean getBoolean() {
Boolean booleanValue = (value instanceof Boolean) ? (Boolean) value : null;
if (value == null) {
try {
booleanValue = Boolean.valueOf(value.toString());
}
catch (Throwable t) {
}
}
// We want a NullPointerException here
return booleanValue.booleanValue();
}
public int getInteger() {
Integer integerValue = (value instanceof Number) ? ((Number) value).intValue() : null;
if (integerValue == null) {
try {
integerValue = Integer.valueOf(value.toString());
}
catch (Throwable t) {
}
}
return integerValue.intValue();
}
public float getFloat() {
Float floatValue = (value instanceof Number) ? ((Number) value).floatValue() : null;
if (floatValue == null) {
try {
floatValue = Float.valueOf(value.toString());
}
catch (Throwable t) {
}
}
return floatValue.floatValue();
}
public static void saveToFile(String path) throws IOException {
FileWriter fw = new FileWriter(path);
Properties properties = new Properties();
for (Options option : Options.values()) {
if (option.saveValue) {
properties.setProperty(option.name(), option.getString());
}
}
if (DEBUG.getBoolean()) {
properties.list(System.out);
}
properties.store(fw, null);
}
public static void loadFromFile(String path) throws IOException {
FileReader fr = new FileReader(path);
Properties properties = new Properties();
properties.load(fr);
if (DEBUG.getBoolean()) {
properties.list(System.out);
}
Object value = null;
for (Options option : Options.values()) {
if (option.saveValue) {
Class<?> clazz = option.value.getClass();
try {
if (String.class.equals(clazz)) {
value = properties.getProperty(option.name());
}
else {
value = clazz.getConstructor(String.class).newInstance(properties.getProperty(option.name()));
}
}
catch (NoSuchMethodException ex) {
Debug.log(ex);
}
catch (InstantiationException ex) {
Debug.log(ex);
}
catch (IllegalAccessException ex) {
Debug.log(ex);
}
catch (IllegalArgumentException ex) {
Debug.log(ex);
}
catch (InvocationTargetException ex) {
Debug.log(ex);
}
if (value != null) {
option.setValue(value);
}
}
}
}
}
This way, I can save and retrieve values from files easily. The problem is that I don't want to repeat this code everywhere. Like as we know, enums can't be extended; so wherever I use this, I have to put all these methods there. I want only to declare the values and that if they should be persisted. No method definitions each time; any ideas?
Using an enum to hold configurable values like this looks like an entirely wrong design. Enums are singletons, so effectively you can only have one configuration active at any given time.
An EnumMap sounds more like what you need. It's external to the enum, so you can instantiate as many configurations as you need.
import java.util.*;
public class EnumMapExample {
static enum Options {
DEBUG, ALWAYS_SAVE, THREAD_COUNT;
}
public static void main(String[] args) {
Map<Options,Object> normalConfig = new EnumMap<Options,Object>(Options.class);
normalConfig.put(Options.DEBUG, false);
normalConfig.put(Options.THREAD_COUNT, 3);
System.out.println(normalConfig);
// prints "{DEBUG=false, THREAD_COUNT=3}"
Map<Options,Object> debugConfig = new EnumMap<Options,Object>(Options.class);
debugConfig.put(Options.DEBUG, true);
debugConfig.put(Options.THREAD_COUNT, 666);
System.out.println(debugConfig);
// prints "{DEBUG=true, THREAD_COUNT=666}"
}
}
API links
java.util.EnumMap
A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
i tried doing something similar with enum maps and properties files (please see code below). but my enums were simple and only had one value except for an embedded case. i may have something that is more type safe. i will look around for it.
package p;
import java.util.*;
import java.io.*;
public class GenericAttributes<T extends Enum<T>> {
public GenericAttributes(final Class<T> keyType) {
map = new EnumMap<T, Object>(this.keyType = keyType);
}
public GenericAttributes(final Class<T> keyType, final Properties properties) {
this(keyType);
addStringProperties(properties);
}
public Object get(final T key) {
// what does a null value mean?
// depends on P's semantics
return map.containsKey(key) ? map.get(key) : null;
}
public boolean contains(final T key) {
return map.containsKey(key);
}
public void change(final T key, final Object value) {
remove(key);
put(key, value);
}
public Object put(final T key, final Object value) {
if (map.containsKey(key))
throw new RuntimeException("map already contains: " + key);
else
return map.put(key, value);
}
public Object remove(final T key) {
if (!map.containsKey(key))
throw new RuntimeException("map does not contain: " + key);
return map.remove(key);
}
public String toString() {
return toString(defaultEquals, defaultEndOfLine);
}
// maybe we don;t need this stuff
// we have tests for it though
// it might be useful
public String toString(final String equals, final String endOfLine) {
final StringBuffer sb = new StringBuffer();
for (Map.Entry<T, Object> entry : map.entrySet())
sb.append(entry.getKey()).append(equals).append(entry.getValue()).append(endOfLine);
return sb.toString();
}
public Properties toProperties() {
final Properties p = new Properties();
for (Map.Entry<T, Object> entry : map.entrySet())
p.put(entry.getKey().toString(), entry.getValue().toString());
return p;
}
public void addStringProperties(final Properties properties) {
// keep this for strings, but mostly do work in the enum class
// i.e. static GenericAttributes<PA> fromProperties();
// which would use a fromString()
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
final String key = (String) entry.getKey();
final String value = (String) entry.getValue();
addProperty(key, value);
}
}
public void addProperty(final String key, final Object value) {
try {
final T e = Enum.valueOf(keyType, key);
map.put(e, value);
} catch (IllegalArgumentException e) {
System.err.println(key + " is not an enum from: " + keyType);
}
}
public int size() {
return map.size();
}
public static Properties load(final InputStream inputStream,final Properties defaultProperties) {
final Properties p=defaultProperties!=null?new Properties(defaultProperties):new Properties();
try {
p.load(inputStream);
} catch(IOException e) {
throw new RuntimeException(e);
}
return p;
}
public static Properties load(final File file,final Properties defaultProperties) {
Properties p=null;
try {
final InputStream is=new FileInputStream(file);
p=load(is,defaultProperties);
is.close();
} catch(IOException e) {
throw new RuntimeException(e);
}
return p;
}
public static void store(final OutputStream outputStream, final Properties properties) {
try {
properties.store(outputStream, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void store(final File file, final Properties properties) {
try {
final OutputStream os = new FileOutputStream(file);
store(os, properties);
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
final Class<T> keyType;
static final String defaultEquals = "=", defaultEndOfLine = "\n";
private final EnumMap<T, Object> map;
public static void main(String[] args) {
}
}
package p;
import static org.junit.Assert.*;
import org.junit.*;
import java.io.*;
import java.util.*;
enum A1 {
foo,bar,baz;
}
enum A2 {
x,y,z;
}
public class GenericAttributesTestCase {
#Test public void testGenericAttributes() {
new GenericAttributes<A1>(A1.class);
}
#Test public void testGenericAttributesKeyTypeProperties() {
final Properties expected=gA1.toProperties();
final GenericAttributes<A1> gA=new GenericAttributes<A1>(A1.class,expected);
final Properties actual=gA.toProperties();
assertEquals(expected,actual);
}
#Test public void testGet() {
final A1 key=A1.foo;
emptyGA1.put(key,null);
final Object actual=emptyGA1.get(key);
assertEquals(null,actual);
}
#Test public void testGetInteger() {
// attributes.add(key,integer);
// assertEquals(integer,attributes.get("key"));
}
#Test public void testContains() {
for(A1 a:A1.values())
assertFalse(emptyGA1.contains(a));
}
#Test public void testChange() {
final A1 key=A1.foo;
final Integer value=42;
emptyGA1.put(key,value);
final Integer expected=43;
emptyGA1.change(key,expected);
final Object actual=emptyGA1.get(key);
assertEquals(expected,actual);
}
#Test public void testAdd() {
final A1 key=A1.foo;
final Integer expected=42;
emptyGA1.put(key,expected);
final Object actual=emptyGA1.get(key);
assertEquals(expected,actual);
}
#Test public void testRemove() {
final A1 key=A1.foo;
final Integer value=42;
emptyGA1.put(key,value);
emptyGA1.remove(key);
assertFalse(emptyGA1.contains(key));
}
#Test public void testToString() {
final String actual=gA1.toString();
final String expected="foo=a foo value\nbar=a bar value\n";
assertEquals(expected,actual);
}
#Test public void testToStringEqualsEndOfLine() {
final String equals=",";
final String endOFLine=";";
final String actual=gA1.toString(equals,endOFLine);
final String expected="foo,a foo value;bar,a bar value;";
assertEquals(expected,actual);
}
#Test public void testEmbedded() {
final String equals=",";
final String endOfLine=";";
//System.out.println("toString(\""+equals+"\",\""+endOFLine+"\"):");
final String embedded=gA1.toString(equals,endOfLine);
GenericAttributes<A2> gA2=new GenericAttributes<A2>(A2.class);
gA2.put(A2.x,embedded);
//System.out.println("embedded:\n"+gA2);
// maybe do file={name=a.jpg;dx=1;zoom=.5}??
// no good, key must be used more than once
// so file:a.jpg={} and hack
// maybe file={name=...} will work
// since we have to treat it specially anyway?
// maybe this is better done in ss first
// to see how it grows?
}
#Test public void testFromString() {
// final Attributes a=Attributes.fromString("");
// final String expected="";
// assertEquals(expected,a.toString());
}
#Test public void testToProperties() {
final Properties expected=new Properties();
expected.setProperty("foo","a foo value");
expected.setProperty("bar","a bar value");
final Properties actual=gA1.toProperties();
assertEquals(expected,actual);
}
#Test public void testAddProperties() {
final Properties p=gA1.toProperties();
final GenericAttributes<A1> ga=new GenericAttributes<A1>(A1.class);
ga.addStringProperties(p);
// assertEquals(ga1,ga); // fails since we need to define equals!
// hack, go backwards
final Properties p2=ga.toProperties();
assertEquals(p,p2); // hack until we define equals
}
#Test public void testStore() throws Exception {
final Properties expected=gA1.toProperties();
final ByteArrayOutputStream baos=new ByteArrayOutputStream();
GenericAttributes.store(baos,expected);
baos.close();
final byte[] bytes=baos.toByteArray();
final ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
final Properties actual=GenericAttributes.load(bais,null);
bais.close();
assertEquals(expected,actual);
}
#Test public void testLoad() throws Exception {
final Properties expected=gA1.toProperties();
final ByteArrayOutputStream baos=new ByteArrayOutputStream();
GenericAttributes.store(baos,expected);
baos.close();
final ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
final Properties actual=GenericAttributes.load(bais,null);
bais.close();
assertEquals(expected,actual);
}
#Test public void testMain() {
// fail("Not yet implemented");
}
GenericAttributes<A1> gA1=new GenericAttributes<A1>(A1.class);
{
gA1.put(A1.foo,"a foo value");
gA1.put(A1.bar,"a bar value");
}
GenericAttributes<A1> emptyGA1=new GenericAttributes<A1>(A1.class);
}
answering your comment:
seems like i am getting values by using the enum as the key. i am probably confused.
an enum can implement an interface and each set of enums could have an instance of that base class and delegate calls to it (see item 34 of http://java.sun.com/docs/books/effective/toc.html)
i found the other code that went with my generic attributes (please see below), but i can't find any tests for it and am not quite sure what i was doing other than perhaps to add some stronger typing.
my motivation for all of this was to store some attributes for a photo viewer like picasa, i wanted to store a bunch of attributes for a picture in a single line of a property file
package p;
import java.util.*;
public enum GA {
// like properties, seems like this wants to be constructed with a set of default values
i(Integer.class) {
Integer fromString(final String s) {
return new Integer(s);
}
Integer fromNull() {
return zero; // return empty string?
}
},
b(Boolean.class) {
Boolean fromString(final String s) {
return s.startsWith("t")?true:false;
}
Boolean fromNull() {
return false;
}
},
d(Double.class) {
Double fromString(final String s) {
return new Double(s);
}
Double fromNull() {
return new Double(zero);
}
};
GA() {
this(String.class);
}
GA(final Class clazz) {
this.clazz=clazz;
}
abstract Object fromString(String string);
abstract Object fromNull();
static GenericAttributes<GA> fromProperties(final Properties properties) {
final GenericAttributes<GA> pas=new GenericAttributes<GA>(GA.class);
for(Map.Entry<Object,Object> entry:properties.entrySet()) {
final String key=(String)entry.getKey();
final GA pa=valueOf(key);
if(pa!=null) {
final String stringValue=(String)entry.getValue();
Object value=pa.fromString(stringValue);
pas.addProperty(key,value);
} else throw new RuntimeException(key+"is not a member of "+"GA");
}
return pas;
}
// private final Object defaultValue; // lose type?; require cast?
/* private */final Class clazz;
static final Integer zero=new Integer(0);
}
If you are still looking for answers, you could give a try to Properties library which is open-source with MIT license. Using this, you won't have to specify string constants and everything will be determined by an enum defined by you. And, it has some other features too. Highlights of this library are:
All property keys are defined in a single place, i.e. a user defined enum
Property values can contain variables (starting with $ sign, e.g. $PATH) where PATH is a property key in same file
Property value can be obtained as specified data type, so no need to convert string value to required data type
Property value can be obtained as list of specified data types
Property value can be a multi-line text
Can make property keys mandatory or optional
Can specify default value for the property key if value is not available
Is thread safe
You can find sample programs here

Categories

Resources