1

I'm having code with if and else if to find some type and create a respective value from it. I wonder how to make it more efficient, I have found the following post in a forum but I don't have a type like boolean, my types are bollean.edm, char.edm etc.

Is there a way to use the following code with adjustment to support my case?

public static void main(String[] args) throws InterruptedException {
    String typeName = "Boolean";
    String memberValue = "memberValue";
    SwitchInputType type = Type.valueOf(typeName).makeType(memberValue);
}

enum Type {
    Boolean {
        SwitchInputType makeType(String memberValue) {
            return new SwitchInputType<Boolean>(new Boolean(memberValue));
        }
    },
    Double {
        SwitchInputType makeType(String memberValue) {
            return new SwitchInputType<Double>(new Double(memberValue));
        }
    }, 
    Int32 {
        SwitchInputType makeType(String memberValue) {
            return new SwitchInputType<Integer>(new Integer(memberValue));
        }
    };

    // All must do this.
    abstract SwitchInputType makeType(String memberValue);
}

static class SwitchInputType<T> {
    public SwitchInputType(Object o) {
    }
}
2
  • Can you please try to explain more exactly what your aim is? What is there to know about your type bollean.edm etc?
    – user905686
    Mar 23, 2013 at 10:12
  • I dont think that this is the issue the type should be provided as string ,btw this is Odata type Mar 23, 2013 at 10:16

1 Answer 1

1

According to this, that looks like a documentation to yours misterious Odata type. The more or less working sollution should look like this (just change the String typeName values from standard java.lang.classes to those Odata type whatever ;)):

public class Test {
    public static void main(String[] args) throws InterruptedException {
            String typeName = "Edm.Double";
            String namePreparedForEncoding = typeName.replace('.', '_');
            Type type = Type.valueOf(namePreparedForEncoding);
            System.out.println(type);

            String memberValue = "42.99";
            SwitchInputType<?> value = type.makeType(memberValue);
            System.out.println(value);

            String typeName1 = "Edm.Int32";
            String namePreparedForEncoding1 = typeName1.replace('.', '_');
            Type type1 = Type.valueOf(namePreparedForEncoding1);
            System.out.println(type1);

            String memberValue1 = "42";
            SwitchInputType<?> value1 = type1.makeType(memberValue1);
            System.out.println(value1);
    }

    enum Type {
        Edm_Boolean {
            SwitchInputType makeType(String memberValue) {
                return new SwitchInputType<Boolean>(new Boolean(memberValue));
            }
        },
        Edm_Double {
            SwitchInputType makeType(String memberValue) {
                return new SwitchInputType<Double>(new Double(memberValue));
            }
        },
        Edm_Int32 {
            SwitchInputType makeType(String memberValue) {
                return new SwitchInputType<Integer>(new Integer(memberValue));
            }
        };

        // All must do this.
        abstract SwitchInputType makeType(String memberValue);
    }

    static class SwitchInputType<T> {
        private Object o;

        public SwitchInputType(Object o) {
            this.o = o;
        }

        @Override
        public String toString() {
            return "SwitchInputType: " + o.toString();
        }
    }
}

output:

Edm_Double
SwitchInputType: 42.99

Edm_Int32
SwitchInputType: 42

As you may notice, I have replaced Edm. with Edm_ in enums - because enum cannot be names with the dot in the midlle.

PS:

If you change a bit toString() method you will be sure that the conversion is really working:

    public String toString() {
        return String.format("SwitchInputType: (%s) %s", o.getClass().getSimpleName(), o);
    }

results in: SwitchInputType: (Double) 42.99

Hope this helps you

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.