javajavafxenum-map

Initialize Map of EnumMap


I need to initialize the following private member in a JavaFX application where I am trying to organize gui widgets, however I do not know the correct syntax, could someone please let me know the correct syntax:

Here is the enum I am using for my EnumMap

enum Connection {
    Connection1,
    Connection2,
    Connection3,
    Connection4;
}

Here are a selection of the widgets that I am trying to organize with this map or EnumMaps based on a service name key, so the following list of 4 checkboxes and labels belong together, (I have similar JavaFX widgets for service 2 and so forth.

@FXML
private CheckBox mService1CheckBox1;
@FXML
private CheckBox mService1CheckBox2;
@FXML
private CheckBox mService1CheckBox3;
@FXML
private CheckBox mService1CheckBox4;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;
@FXML
private Label mService1Label1;

This is the private Member I am trying to initialize with JavaFX widgets

private Map<String, EnumMap<Connection, AbstractMap.SimpleEntry<Label,
CheckBox>>> mWidgetInfo;

I can initialize the top level empty mServiceWidgetMap with:

mWidgetInfo= new HashMap<>();

And I know that I need to initialize the EnumMaps as new EnumMap<>(Connection.class); but I also need to put pairs of widgets in the value size of these EnumMaps and I am confused as to how to do this.

However I don't know how to initialize the enumMap value pairs. Syntax help much appreciated.

EDIT After struggling for a while I came up with the following, but surely there has got to be a simpler approach like double brace initialization or some other less verbose approach.

private void initializeServiceHeartbeatTab() {
    // @JC Todo - dynamically create base on CSV rows
    // Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service1Info =
        new EnumMap<>(Connection.class);
    SimpleEntry<Label, CheckBox> pair1 = new SimpleEntry<>(
        mService1Label1, mService1CheckBox1);
    SimpleEntry<Label, CheckBox> pair2 = new SimpleEntry<>(
        mService1Label2, mService1CheckBox2);
    SimpleEntry<Label, CheckBox> pair3 = new SimpleEntry<>(
        mService1Label3, mService1CheckBox3);
    SimpleEntry<Label, CheckBox> pair4 = new SimpleEntry<>(
        mService1Label4, mService1CheckBox4);
    service1Info.put(Connection.Connection1, pair1);
    service1Info.put(Connection.Connection1, pair2);
    service1Info.put(Connection.Connection1, pair3);
    service1Info.put(Connection.Connection1, pair4);

    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info = 
        new EnumMap<>(Connection.class);
    pair1 = new SimpleEntry<>(mService2Label1, mService2CheckBox1);
    pair2 = new SimpleEntry<>(mService2Label2, mService2CheckBox2);
    pair3 = new SimpleEntry<>(mService2Label3, mService2CheckBox3);
    pair4 = new SimpleEntry<>(mService2Label4, mService2CheckBox4);
    service2Info.put(Connection.Connection1, pair1);
    service2Info.put(Connection.Connection1, pair2);
    service2Info.put(Connection.Connection1, pair3);
    service2Info.put(Connection.Connection1, pair4);

    EnumMap<Connection, SimpleEntry<Label, CheckBox>> service2Info = 
        new EnumMap<>(Connection.class);
    pair1 = new SimpleEntry<>(mService3Label1, mService3CheckBox1);
    pair2 = new SimpleEntry<>(mService3Label2, mService3CheckBox2);
    pair3 = new SimpleEntry<>(mService3Label3, mService3CheckBox3);
    pair4 = new SimpleEntry<>(mService3Label4, mService3CheckBox4);
    service3Info.put(Connection.Connection1, pair1);
    service3Info.put(Connection.Connection1, pair2);
    service3Info.put(Connection.Connection1, pair3);
    service3Info.put(Connection.Connection1, pair4);

    mWidgetInfo = new HashMap<>();
    mWidgetInfo.put("albf", service1Info);
    mWidgetInfo.put("fms1", service2Info);
    mWidgetInfo.put("fms2", service3Info);
}

Solution

  • I finally figured out how to initialize the above Map. The trick to initializing it without having all those un-necessary temporaries was to use nested double brace initialization as follows:

    // Map<String, EnumMap<Connection, SimpleEntry<Label, CheckBox>>>
    mWidgetInfo = new HashMap() {{
        put("service1", new EnumMap(Connection.class) {{
            put(Connection.Connection1, new SimpleEntry<>(mService1Label1, mService1CheckBox1));
            put(Connection.Connection2, new SimpleEntry<>(mService1Label2, mService1CheckBox2));
            put(Connection.Connection3, new SimpleEntry<>(mService1Label3, mService1CheckBox3));
            put(Connection.Connection4, new SimpleEntry<>(mService1Label4, mService1CheckBox4));
        }});
        put("service2", new EnumMap(Connection.class) {{
            put(Connection.Connection1, new SimpleEntry<>(mService2Label1, mService2CheckBox1));
            put(Connection.Connection2, new SimpleEntry<>(mService2Label2, mService2CheckBox2));
            put(Connection.Connection3, new SimpleEntry<>(mService2Label3, mService2CheckBox3));
            put(Connection.Connection4, new SimpleEntry<>(mService2Label4, mService2CheckBox4));
        }});
        put("service3", new EnumMap(Connection.class) {{
            put(Connection.Connection1, new SimpleEntry<>(mService3Label1, mService3CheckBox1));
            put(Connection.Connection2, new SimpleEntry<>(mService3Label2, mService3CheckBox2));
            put(Connection.Connection3, new SimpleEntry<>(mService3Label3, mService3CheckBox3));
            put(Connection.Connection4, new SimpleEntry<>(mService3Label4, mService3CheckBox4));
        }});
    }};
    

    Then later I can iterate over all the widgets in the collection - for example initializing the tooltip help and setting a default label as follows:

    // initialize the tooltips & default labels
    mWidgetInfo.forEach((k, v)-> {
        // no need to switch on the service
        // disable click events on the checkboxes
        // as they should be read only
        v.forEach((k1, widgetPair)-> {
            widgetPair.getKey().setTooltip(
                new Tooltip("Connect ID"));
            CheckBox checkBox = widgetPair.getValue();
            checkBox.setTooltip(new Tooltip("Servicing Connection"));
            checkBox.setOpacity(1);
        });
    });