を書く避けるために、私は以下のようなコードの一部があります分割のJavaビルダーは、同じコードを複数回
Problem hashProblem;
String indexName;
if(condition.getOwner() != null) {
indexName = sourceLocation ;
hashProblem = new Problem().builder()
.locationID(condition.getLocationID())
.sourceLocation(condition.getSourceLocation())
.build();
}
else {
indexName = currentLocation;
hashProblem = new Problem().builder()
.locationID(condition.getLocationID())
.currentLocation(criteria.getcurrentLocation())
.build();
}
をよりエレガントな方法でこのコードを記述する方法はありますか? hashProblemオブジェクトを構築する際には、常にlocationIDが必要です。 .locationIDを1回だけ書くことができるように、ビルダーを分割する方法を考えることができません。私はビルダーのためにロンボク(https://projectlombok.org/features/Builder.html)を使用しています
私はロンボクをビルダーに使用しています。私が新しいProblem()を使用しないと、エラーメソッド呼び出しが発生します。 – user1692342
LombokのBuilderを使用している場合:Builder b = new Problem()。builder()。locationID(condition.getLocationID());エラーが発生する互換性のない型 – user1692342
'Builder b'の代わりに' Problem().builder() 'が返す型を' b'にしてください。ビルダークラスの通常の名前なので、ビルダーと呼んでいます。 –