根据字符串标识符检查对象字段

时间:2014-11-29 10:26:23

标签: java oop object

我有一个包含不同类型字段的对象,主要是String,Float,Integer。

我需要编写一个方法来检查此类对象的数组,是否由特定字符串标识符标识的特定字段不为空且不为空。

例如,我有以下字段:

String field1;
Integer field2;
String field3;

和标识符:

String IDENTIFIER_FIELD_1 = "IDENTIFIER_1";
String IDENTIFIER_FIELD_2 = "IDENTIFIER_2";

如果我将带有ArrayList<String>的{​​{1}}传递给我的方法,我只想检查IDENTIFIER_FIELD_1,如果我通过field1 ArrayList<String>传递IDENTIFIER_FIELD_1 {1}},我想同时检查IDENTIFIER_FIELD_2field2

Java中有没有有效的方法对数组对象进行此类检查?我提出的唯一解决方案是使用字符串比较检查每个字段,但这根本不会有效。

3 个答案:

答案 0 :(得分:1)

你可以使用一些反思,它可能是一个&#34;矫枉过正的&#34;寿。这个方法有一个缺陷,你需要知道变量的确切名称,但我相信你有它们。

public class TestObject {

//Some fields.
private int firstInt;
private int secondInt;
private String firstString;

public TestObject(int firstInt, int secondInt, String firstString) { 
    this.firstInt = firstInt;
    this.secondInt = secondInt;
    this.firstString = firstString;
}

public void checkForNulls(String... fieldsToSearch) throws IllegalArgumentException, IllegalAccessException {

    boolean isValid = true;

    //Here we retrieve all the fields we need from our class.
    for (String fieldName : fieldsToSearch) {
        //Here is the method to get a field by name.
        try {
            Field field = this.getClass().getDeclaredField(fieldName);

            //Change access modifier so we can get the value.
            field.setAccessible(true);

            //Get the field value of this instance.
            if (field.get(this) == null) {
                isValid = false;
                System.out.println("Fix him! Field " + field.getName() + " equals null!");
            }

        } catch (Exception e) {
            //There can be no field with such name, you need to do something here.
        }
    }

    if (isValid) {
        System.out.println("This objects fields are fine.");
    }
}
}

现在的主要方法是:

public class Test {
//Names of the fields, you actually need to know them.
public static final String FIRST_INT_INDENTIFIER = "firstInt";
public static final String SECOND_INT_INDENTIFIER = "secondInt";
public static final String FIRST_STRING_INDENTIFIER = "firstString";

public static void main(String[] args) {

    TestObject[] testArray = new TestObject[10];

    //Fill the array with some objects.
    for (int i = 0; i < testArray.length; i++) {
        testArray[i] = new TestObject(i, i * 2, "Number: " + i);

        if (i % 3 == 0) {
            testArray[i] = new TestObject(i, i * 2, null);
        }
    }

    //Check the array of our objects.
    for (int i = 0; i < testArray.length; i++) {
        try {
            testArray[i].checkForNulls(FIRST_INT_INDENTIFIER, FIRST_STRING_INDENTIFIER);
        } catch (Exception e) {
            //Some exception handling
        }
    }

}
}

答案 1 :(得分:0)

您可以使用包含方法,例如:

if (mylist.contains("field1") && myList.contains("field2")) {
    System.out.println("Found both identifier");
}

答案 2 :(得分:0)

我会在对象中放置一个方法,例如:

public boolean isPopulated(String key){ // or int key if you're more interested in efficiency than clarity

    switch(key){
        case "field1":
            if (field1 != null && !field1.equals("")) return true;
            return false;
            break;
        case "field2":
            etc

通过这种方式,您可以为每个字段定义“空”的含义。

然后陈述显而易见的,你的检查方法看起来像(假设你使用的是Integer字段而不是字符串)

public boolean areAllPopulated(ArrayList<MyObject> objects, ArrayList<Integer> fields) {
    for (Integer field : fields){
        for (MyObject object : objects){
            if (object.isPopulated(field) == false){
                return false;
            }
        }
    }
    return true;
}

如果你担心String比较的代价,可以在某处创建一个fieldNames的HashMap到fieldIntegers并转换ArrayList 在检查方法的开头字符串到整数的ArrayList,这将节省大部分字符串比较惩罚。