0

In our current code, we have a class called MasterCodeView:

public class MasterCodeView implements Serializable {

    private String code;
    private String description;
    //getters and setters
}

Then, in each of our business objects, we have one or more String[][] attributes:

public static final String[][] CONNECTIONMODEDESCRIPTIONS = {
        { CONNECTIONMODE_PASSIVE + "", MasterCodeDescriptionKeys.MC_FTPCONNECTIONMODE_PASSIVE }, 
        { CONNECTIONMODE_ACTIVE + "", MasterCodeDescriptionKeys.MC_FTPCONNECTIONMODE_ACTIVE }
};

These MasterCodeViews are used for i18n of certain object-specific data, for use in radio button labels, grid table cells,... We have around 60 of these.

The translation from the String[][] to List<MasterCodeView> is done using a MasterCodeServiceImpl singleton:

private List<MasterCodeView> connectionModeDescriptions = new ArrayList<MasterCodeView>();
// We have a bunch of these that are just plain List, but those are being
// replaced with Generic lists like above whenever possible to remove warnings.

private MasterCodeServiceImpl() {
    Object[][] tmp = {
        ...
        { connectionModeDescriptions, FTP.CONNECTIONMODEDESCRIPTIONS },
        ... // over 60 lines in total
    } ; 
    for (int i = 0; i < tmp.length; i++) {
        List<MasterCodeView> list = (List<MasterCodeView>)tmp[i][0];
        String[][] descriptions = (String[][])tmp[i][1];
        for (int j = 0; j < descriptions.length; j++) {
            MasterCodeView masterCodeView = new MasterCodeView();
            masterCodeView.setCode(descriptions[j][0]);
            masterCodeView.setDescription(descriptions[j][1]);
            list.add(masterCodeView);
        }
    }
}

As you can see, this takes a 2D array of Objects, which means that there's a message about Unchecked Conversion from Object to List<MasterCodeView> on the first line within the For loop. I would like to get rid of this error message. However, I want to do this without having to edit the 60 line mapping array to a new formatting, and without having to change anything about the business objects or the MasterCodeView class.

I preferably only want to change the Object[][] to something else and if needed the for loop. Is this possible?

Nzall
  • 3,439
  • 5
  • 29
  • 59
  • How about a static initialization block? See here: http://stackoverflow.com/a/2420466/794638 – sinned Jun 29 '16 at 10:00
  • @sinned That won't help here. This is already a singleton, so this only gets executed once anyway. It's not the constructor I want to get rid of, it's the awkward `Object[][]` temp variable. I preferably want to turn it into something like `Array,String[][]>`, but that would require rewriting the entire initialization array, I think – Nzall Jun 29 '16 at 10:01
  • You could make a class MasterCodeViewBuilder that has a constructor that takes (connectionModeDescriptions, FTP.CONNECTIONMODEDESCRIPTIONS). And has a build() function that builds a MasterCodeView. And have an array of those instead of your Object 2d array. But a better solution would be to do a more extensive refactoring of this, it's seems spagethi-cody right now. – Bob Brinks Jun 29 '16 at 10:03

1 Answers1

0

You could maybe do something like this (saves you the 2d array and looping).

private List<MasterCodeView> connectionModeDescriptions = new 

private MasterCodeServiceImpl() {
    connectionModeDescriptions = Arrays.asList( {
        ...
        new MasterCodeView(connectionModeDescriptions, FTP.CONNECTIONMODEDESCRIPTION),
        ... // over 60 lines in total
    } );        
}
Bob Brinks
  • 1,372
  • 1
  • 10
  • 19
  • Oops forgot the "I want to do this without having to edit the 60 line mapping array to a new formatting" part. Sorry – Bob Brinks Jun 29 '16 at 10:08
  • I think you misunderstood. We have 62 of those `List` private elements (with another class using them for i18n translation using Getters). the constructor also has 62 lines that each map a `List` to `String[][]`, with the for loop turning them from the latter into the former. I want to turn the `Object[][]` into something that supports Typechecking. – Nzall Jun 29 '16 at 10:12
  • Ok i can't find a good way to do that in your current setup. You would need a common super class for all objects in your 2d array. And currently the only common super class is Object. – Bob Brinks Jun 29 '16 at 10:23