Chris Garsonn
Chris Garsonn

Reputation: 777

How to use Java stream map with filters and optional?

Basically what I want to do is following code(I will get system param and will check if it is not null then if the current system on the code is not equal to that then set the dbName as parameter)

if (Objects.nonNull(query.getSystem())) {
            if (!query.getSystem().equals(dbContextHolder.getCurrentDb().toString())) {
                dbContextHolder.setCurrentDb(Enum.valueOf(DbTypeEnum.class, query.getSystem()));
            }
        }

I also want if the currentDb system is null then return null. What I try to do is something like

var res = Optional.ofNullable(dbContextHolder.getCurrentDb().toString())
                .map(String::toString)
                .filter(s -> !s.equals(dbType))
                .orElse(Optional.ofNullable(dbType).orElse(null));

But as you see it is wrong and not working. How can I achieve that if parameter dbType is not equal to getCurrentDb then call the method setDbType(paramDbType) if they are equal then return one of them if currentDb is null then return null.

Upvotes: 0

Views: 680

Answers (1)

Igor Flakiewicz
Igor Flakiewicz

Reputation: 793

By reducing your problem down I just realized that you always want the value of query.getSystem() to be the context, therefore:

I reduced your code like this:

MockDbTypeEnum newMethod(MockQuery query, MockDbContextHolder dbContextHolder) {
    return Optional
            .ofNullable(query.getSystem())
            .map(MockDbTypeEnum::valueOf)
            .orElse(null);
}

MockDbTypeEnum oldMethod(MockQuery query, MockDbContextHolder dbContextHolder) {

    if (Objects.nonNull(query.getSystem())) {
        if (!query.getSystem().equals(dbContextHolder.getCurrentDb().toString())) {
            dbContextHolder.setCurrentDb(Enum.valueOf(MockDbTypeEnum.class, query.getSystem()));
        }

        return dbContextHolder.getCurrentDb();
    }

    return null;
}

Also here are the mocks and tests I used to prove these methods are functionally the same for your purposes:

@ParameterizedTest
@CsvSource(value = {
        "PSQL, PSQL, PSQL",
        "PSQL, SQL, PSQL",
        "SQL, SQL, SQL",
        "SQL, PSQL, SQL",
        "null, SQL, null",
        "null, PSQL, null"
}, nullValues = {"null"})
void test(String system, MockDbTypeEnum currentDb, MockDbTypeEnum expectedResult) {
    MockQuery query = new MockQuery(system);
    MockDbContextHolder dbContextHolder = new MockDbContextHolder(currentDb);

    MockDbTypeEnum result = oldMethod(query, dbContextHolder);
    assertEquals(expectedResult, result);

    MockDbTypeEnum newResult = newMethod(query, dbContextHolder);
    assertEquals(expectedResult, newResult);
}

enum MockDbTypeEnum {
    PSQL,
    SQL
}

static class MockQuery {

    private final String system;

    public MockQuery(String system) {
        this.system = system;
    }

    public String getSystem() {
        return system;
    }

}

static class MockDbContextHolder {

    private MockDbTypeEnum currentDb;

    public MockDbContextHolder(MockDbTypeEnum currentDb) {
        this.currentDb = currentDb;
    }

    public MockDbTypeEnum getCurrentDb() {
        return currentDb;
    }

    public void setCurrentDb(MockDbTypeEnum currentDb) {
        this.currentDb = currentDb;
    }

}

Here is the result: enter image description here

Upvotes: 2

Related Questions