オブジェクトのPOJOスタイルがあり、その属性のいくつかについて、が特定の値に等しいかどうかを確認する必要があるとします。その場合、この属性をリストに追加して例外をスローする必要があります(属性の1つが特定の値と等しい場合は1回だけ)。この方法よりも良い方法はありますか?
// pseudocode
List<String> list = new ArrayList<String>();
boolean haveToThrowException = false;
if (object.getAttributeA().equals(“0”) {
list.add(object.getAttributeA());
haveToThrowException = true;
}
if (object.getAttributeB().equals(“0”) {
list.add(object.getAttributeB());
haveToThrowException = true;
}
if (object.getAttributeC().equals(“0”) {
list.add(object.getAttributeC());
haveToThrowException = true;
}//and so on
if (haveToThrownException) {
throw new Exception(list.toString());
}
あなたはこのようにそれを行うことができます:
List<String> list =
Stream.of(object.getAttributeA(), object.getAttributeB(), object.getAttributeC())
.filter("0"::equals)
.collect(toList());
if (!list.isEmpty()) {
throw new Exception(list.toString());
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加