Reputation: 1577
I need to simplify following code in java. Are there any way to using set and do this ?
if (!(((AdministrativeState.PLANNED == dispensingOccasionModel.getOccasionDTO().getAdminState()) ||
(AdministrativeState.MISSED == dispensingOccasionModel.getOccasionDTO().getAdminState()) ||
(AdministrativeState.SKIPPED == dispensingOccasionModel.getOccasionDTO().getAdminState()) ||
(AdministrativeState.SELF_ADMINISTERED == dispensingOccasionModel.getOccasionDTO().getAdminState()) ||
(AdministrativeState.SELF_ADMINISTERED_BY_RANGE == dispensingOccasionModel.getOccasionDTO().getAdminState())) &&
isSpecialDoseType(doseDetail))
Upvotes: 2
Views: 83
Reputation: 31858
Using Set
, you can initialize the valid enum types and perform a contains as @shmosel also pointed out in the comments :
Set<AdministrativeState> administrativeStates = Set.of(PLANNED, MISSED, SKIPPED, SELF_ADMINISTERED, SELF_ADMINISTERED_BY_RANGE)
if (!administrativeStates.contains(dispensingOccasionModel.getOccasionDTO().getAdminState())
|| !isSpecialDoseType(doseDetail))
Upvotes: 3
Reputation: 880
Provided the get methods have no side effects and return always the same value (within the scope of your sample), you can use refactoring Extract variable
s =dispensingOccasionModel.getOccasionDTO().getAdminState());
Also, you can statically import AdministrativeState.*.
You then get:
AdministrativeState s =dispensingOccasionModel.getOccasionDTO().getAdminState();
if (!(((PLANNED == s) || (MISSED == s ) || (SKIPPED == s) ||
(SELF_ADMINISTERED == s) ||
(SELF_ADMINISTERED_BY_RANGE == s))
&& isSpecialDoseType(doseDetail))
Then taking into account that the double pipe (Boolean OR) operator has quite a lot priority you can remove the parentheses around single comparisons:
(a==b)||(c==d) ===> a==b||c==d.
You get:
if (!((PLANNED == s ||
MISSED == s ||
SKIPPED == s ||
SELF_ADMINISTERED == s ||
SELF_ADMINISTERED_BY_RANGE == s))
&& isSpecialDoseType(doseDetail))
There are double parentheses after ! And before &&. They can be reduced to a single one.
if (!(PLANNED == s ||
MISSED == s
|| SKIPPED == s ||
SELF_ADMINISTERED == s ||
SELF_ADMINISTERED_BY_RANGE == s)
&& isSpecialDoseType(doseDetail))
Now you can use the rule of inverting logical expressions:
!(a==b|| c==d) ===> a!=b&&c!=d,
Which basically inverts all the operations due to the NOT (!) Operator. Since you will have only && operators left to combine Boolean sub expressions, then you can drop the parentheses.
if (PLANNED != s
&& MISSED != s
&& SKIPPED != s
&& SELF_ADMINISTERED != s
&& SELF_ADMINISTERED_BY_RANGE != s
&& isSpecialDoseType(doseDetail)
)
Now if you have good domain knowledge you could know if you can combine self Administered into one variable and shipped/missed into another to have something like: neither ( planned nor stoppedOrMissed nor selfAdministered)&& isSpecial. But without such knowledge I would live the expression at this level.
Upvotes: 1
Reputation: 1943
It is unclear what data types are we dealing with here so I can't give you a better answer until you can shed more light on this. But assuming AdministrativeState
is an enumerator you can do something like this:
public enum AdministrativeState {
NONE, PLANNED, MISSED, SKIPPED, SELF_ADMINISTERED,
SELF_ADMINISTERED_BY_RANGE;
}
public static class OccasionModel {
AdministrativeState state;
OccasionModel() {
this.state = AdministrativeState.NONE;
}
OccasionModel setState(AdministrativeState state) {
this.state = state;
}
}
public static void checkAdminState(OccasionModel model) {
OccasionModel dispensingOccasionModel = model;
if (dispensingOccasionModel.state != AdministrativeState.NONE) {
// Do something here...
}
else System.out.println("Administrative state is not set yet");
}
public static void main(String[] args) throws IOException {
checkAdminState(new OccasionModel());
checkAdminState(new OccasionModel().setState(AdministrativeState.PLANNED));
}
I am not sure if this is what you're looking for but this doesn't have anything to do with Java 8 in particular. If you can provide more information about what's what I can help you out further.
Upvotes: 1