1

I want to parse a YAML-file via Jackson but encounter the problem that one of the properties (let's call it 'Event') has a string called 'type' and a 'properties' object that differs for different Events. My issue is that I need to define the POJOs for this YAML. Therefore, I want to define a Hashmap with VariableObject that can be any of some predefined classes (for brevity, let's say Shipping and Inventory).

How can I implement a Hashmap like that?

public class Event {
    private static String type;
    private static Map<String, VariableObject> properties;

    public static void main(String[] args) {
        Inventory inventory = new Inventory("inventoryName", 13);
        properties.put("Inventory", inventory);
        Shipping shipping = new Shipping("shippingName", true);
        properties.put("Shipping", shipping);
    }
}


public class Inventory {

    private static String name;
    private static int someNumber;

    public Inventory(String name, int someNumber) {
        this.name = name;
        this.someNumber = someNumber;
    }
}


public class Shipping {

    private static String name;
    private static boolean someBoolean;

    public Shipping(String name, boolean someBoolean) {
        this.name = name;
        this.someBoolean = someBoolean;
    }
}
1
  • You should use either a common interface or super class, i.e. Inventory implements VariableObject or Inventory extends VariableObject (since both have a name property that could be part of the common super class VariableObject and thus it would be Inventory extends VariableObject). Commented Apr 18, 2019 at 14:22

2 Answers 2

3

What you're talking ablut is simple Object. It's the most specific common superclass:

private static Map<String, Object> properties;

Other solution would be to make Inventory and Shipping implement some common interface, for example Named and use it as type parameter in HashMap.

Sign up to request clarification or add additional context in comments.

Comments

1

One way to do this is to make Shipping and Inventory implements the same interface (like VariableObject in your cas)

public class Event {
    private static String type;
    private static Map<String, VariableObject> properties;

    public static void main(String[] args) {
        Inventory inventory = new Inventory("inventoryName", 13);
        properties.put("Inventory", inventory);
        Shipping shipping = new Shipping("shippingName", true);
        properties.put("Shipping", shipping);
    }
}

public interface VariableObject{
//you can define common methods here if you want
}


public class Inventory implements VariableObject{

    private static String name;
    private static int someNumber;

    public Inventory(String name, int someNumber) {
        this.name = name;
        this.someNumber = someNumber;
    }
}


public class Shipping implements VariableObject{

    private static String name;
    private static boolean someBoolean;

    public Shipping(String name, boolean someBoolean) {
        this.name = name;
        this.someBoolean = someBoolean;
    }
}

Comments

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.