0

I have a class

 public class ValueObject<T> {
        private T value;

        public void setValue(T value){
          this.value = value
        }
    }

In another class I have an Array of the Objects from the first Class

ArrayList<ValueObject<?>> valueObjects = new ArrayList<>();
ArrayList<String> valueNames = new ArrayList<>();

now I want to write a Method which looks in a second array for a name and assigns a new value to an instance of the first object in that arrayList

ValueObject<?> get(String name) {
        return valueObjects.get(valueNames.indexOf(name));
}

public <T> void set(String name, T value) {
     get(name).setValue(value);
}

But I don't get this to work. Do I need to write something with ? in the set() Method?

Thanks

5
  • avoid value = value should be this.value = value Commented Dec 28, 2017 at 20:58
  • Lets say your ValueObject<?> had a String value but you call set with Integer, how would compiler be able to prevent it? Commented Dec 28, 2017 at 20:58
  • What's ValueNames? And what's valueNames? And why are you passing one instance to another? And why do you think you can call setValue() with any T? Commented Dec 28, 2017 at 20:58
  • This might help? stackoverflow.com/questions/16171637/… Commented Dec 28, 2017 at 21:00
  • ok corrected my question Commented Dec 28, 2017 at 21:00

1 Answer 1

1

You don't provide a full example, so not sure which will help you.

Version 1 if you can use List<ValueObject<T>> because all ValueObjects hold the same type.

static class Lookup<T2> {

    List<ValueObject<T2>> valueObjects = new ArrayList<>();
    List<String> valueNames = new ArrayList<>();

    ValueObject<T2> get(String name) {
        return valueObjects.get(valueNames.indexOf(name));
    }

    public void set(String name, T2 value) {
        get(name).setValue(value);
    }
}

Version 2 if valueObjects really contains ValueObject with different contained classes:

@SuppressWarnings("unchecked")
static class Lookup2 {

    List<ValueObject<?>> valueObjects = new ArrayList<>();
    List<String> valueNames = new ArrayList<>();

    /* unsafe get */
    ValueObject<?> get(String name) {
        return valueObjects.get(valueNames.indexOf(name));
    }


    /* set using unsafe get */
    public <T> void setUnsafe(String name, T value) {
        /* might add handling of runtime exceptions */
        ((ValueObject<T>)get(name)).setValue(value);
    }

    /* safe get when client knows class */
    <T> ValueObject<T> get(String name, Class<T> clazz) {
        /* might do instanceOf check here to throw custom exception */
        return (ValueObject<T>) valueObjects.get(valueNames.indexOf(name));
    }

    /* set using safe get */
    public <T> void set(String name, T value) {
        /* might add handling of runtime exceptions */
        get(name, (Class<T>) value.getClass()).setValue(value);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! The second solution worked great =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.