Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SimplifyThis.java
List<MappedTable> mappedTables1() {
if (mappedTables == null) {
mappedTables = T_BOOK
.getSchema()
.getTables()
.stream()
.map(t -> {
Table<?> l = lookupTable(t.getName());
return l == null ? null : new MappedTable().withInput(t.getName()).withOutput(l.getName());
})
.filter(Predicate.not(Objects::isNull))
.collect(Collectors.toList());
}
return mappedTables;
}
List<MappedTable> mappedTables2() {
if (mappedTables == null) {
mappedTables = T_BOOK
.getSchema()
.getTables()
.stream()
.flatMap(t ->
Optional.ofNullable(lookupTable(t.getName()))
.map(l -> new MappedTable().withInput(t.getName()).withOutput(l.getName()))
.stream()
)
.filter(Predicate.not(Objects::isNull))
.collect(Collectors.toList());
}
return mappedTables;
}
@Mordavolt
Copy link

Mordavolt commented Sep 8, 2020

    //Only improvement is .map(Something::getName)
    List<MappedTable> mappedTables2() {
        if (mappedTables == null) {
            mappedTables = T_BOOK
                    .getSchema()
                    .getTables()
                    .stream()
                    .map(Something::getName)
                    .flatMap(name ->
                            Optional.ofNullable(lookupTable(name))
                                    .map(l -> new MappedTable().withInput(name).withOutput(l.getName()))
                                    .stream()
                    )
                    .filter(Predicate.not(Objects::isNull))
                    .collect(Collectors.toList());
        }

        return mappedTables;
    }

    //If only we had tuples :( 
    // This one is cleaner, but recalculates lookupTable(name), so a no go.
    List<MappedTable> mappedTables2() {
        if (mappedTables == null) {
            mappedTables = T_BOOK
                    .getSchema()
                    .getTables()
                    .stream()
                    .map(Something::getName)
                    .filter(name -> lookupTable(name) != null)
                    .map(name -> new MappedTable()
                            .withInput(name)
                            .withOutput(lookupTable(name))
                            .getName()))
                    .collect(Collectors.toList());
        }

        return mappedTables;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment