3
\$\begingroup\$

I'm creating a version of the classic sequential master file update in java. This is used when you sort two files by the same key for updating one file with the other. In this case, I'm pulling in all available note types and then comparing them against note types configured. The goal is to create two collections, the list of configured note types, and the list of available note types, which do not contain the configured ones.

private void setAvailableAndConfiguredNoteTypes(OptionGroupConfiguration optionConfig,
                                                List<Map<String, String>> noteConfig) {
    //retrieve the collections and sort by ID
    List<ConfigOption> dbConfigured = getConfiguredNoteTypes();
    List<ConfigOption> apiAvailable = getAvailableNoteTypes(noteConfig);

    ConfigOption.CompareById comparator = new ConfigOption.CompareById();
    Collections.sort(dbConfigured, comparator);
    Collections.sort(apiAvailable, comparator);

    //hold the results
    List<ConfigOption> configured = new ArrayList<>();
    List<ConfigOption> available = new ArrayList<>();

    //process both collections
    ListIterator<ConfigOption> configuredIt = dbConfigured.listIterator();
    String configuredId = "";
    ListIterator<ConfigOption> availableIt = apiAvailable.listIterator();
    String availableId = "";

    ConfigOption currentAvailable = null;
    ConfigOption currentConfigured = null;

    while (stillRunning(configuredId, availableId)) {
        int cmp = configuredId.compareTo(availableId);

        if (cmp == 0) {
            //on equal, it goes in configured
            if (currentConfigured != null) {
                configured.add(currentConfigured);
            }

////////////Code Group A
            if (configuredIt.hasNext()) {
                currentConfigured = configuredIt.next();
                configuredId = currentConfigured.getId();
            }
            else {
                currentConfigured = null;
                configuredId = EOF;
            }

////////////Code Group B
            if (availableIt.hasNext()) {
                currentAvailable = availableIt.next();
                availableId = currentAvailable.getId();
            }
            else {
                currentAvailable = null;
                availableId = EOF;
            }
        }
        else if (cmp < 0) {
            //someone disabled the configured option, remove it
            noteTypesConfiguredRepository.delete(configuredId);

////////////Code Group A
            if (configuredIt.hasNext()) {
                currentConfigured = configuredIt.next();
                configuredId = currentConfigured.getId();
            }
            else {
                currentConfigured = null;
                configuredId = EOF;
            }
        }

        else {
            //add to available
            available.add(currentAvailable);

////////////Code Group B
            if (availableIt.hasNext()) {
                currentAvailable = availableIt.next();
                availableId = currentAvailable.getId();
            }
            else {
                currentAvailable = null;
                availableId = EOF;
            }
        }
    }

    Collections.sort(available);
    optionConfig.setAvailableOptions(available);

    Collections.sort(configured);
    optionConfig.setConfiguredOptions(configured);
}

private static boolean stillRunning(String configuredId, String availableId) {
    boolean result = !(configuredId.equals(EOF) && availableId.equals(EOF));
    return result;
}

Some people on my project are looking at this and thinking it's complicated, but I can't for the life of me figure out how to make it any simpler.

Specifically, if you look at the two sections of 8 lines of code marked code group A and code group B, these are identical sections of 8 lines of code and it REALLY bugs me that I can't figure out a way to use the same 8 lines of code.

Does anyone have any suggestions on how to make this code simpler?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.