I have started learning Java8.I have been following traditional way to create a list(for the below code) using if
condition.My code is working but is there any effective way to achieve this using java8? Please let me know
private List<String> getDeliveryAddressTypeList() {
List<String> deliveryAddressTypeList = new ArrayList<>();
deliveryAddressTypeList.add(DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue());
if (SecurityUtils.getSubject().isPermitted(PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS)) {
deliveryAddressTypeList.add(DeliveryAddressType.DEPOT_ADDRESS.getValue());
}
if (SecurityUtils.getSubject().isPermitted(PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS)) {
deliveryAddressTypeList.add(DeliveryAddressType.CONTACT_ADDRESS.getValue());
}
if (SecurityUtils.getSubject().isPermitted(PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS)) {
deliveryAddressTypeList.add(DeliveryAddressType.AN_ALTERNATIVE_ADDRESS.getValue());
}
return deliveryAddressTypeList;
}
I believe the cleanest way is as you have it. Nonetheless, you could use a helper method to avoid repeated if
statements:
static void addIfPermitted(List<String> list, DeliveryAddressType addressType,
PermissionConstant permission) {
if (SecurityUtils.getSubject().isPermitted(permission) {
list.add(addressType.getValue());
}
}
Now you can use this helper method this way:
List<String> deliveryAddressTypeList = new ArrayList<>(Arrays.asList(
DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue()));
addIfPermitted(deliveryAddressTypeList,
DeliveryAddressType.DEPOT_ADDRESS,
PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS);
addIfPermitted(deliveryAddressTypeList,
DeliveryAddressType.CONTACT_ADDRESS,
PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS);
addIfPermitted(deliveryAddressTypeList,
DeliveryAddressType.AN_ALTERNATIVE_ADDRESS,
PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS);
If you want to use java 8 stuff, you could create a Map<DeliveryAddressType, PermissionConstant>
:
List<String> deliveryAddressTypeList = new ArrayList<>(Arrays.asList(
DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue()));
Map<DeliveryAddressType, PermissionConstant> map = new HashMap<>();
map.put(DeliveryAddressType.DEPOT_ADDRESS,
PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS);
map.put(DeliveryAddressType.CONTACT_ADDRESS,
PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS);
map.put(DeliveryAddressType.AN_ALTERNATIVE_ADDRESS,
PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS);
And then:
map.forEach((addressType, permission) ->
addIfPermitted(deliveryAddressList, adressType, permission));
Another way, using streams and the same map, but creating the final list from the map keys:
List<String> deliveryAddressTypeList = map.entrySet().stream()
.filter(e -> SecurityUtils.getSubject().isPermitted(e.getValue()))
.map(Map.Entry::getKey)
.map(DeliveryAddressType::getValue)
.collect(Collectors.toList());
deliveryAddressTypeList.add(DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue());
Here you filter map entries and keep only the ones whose value (PermissionConsdtant
) is permitted by the security. Then you transform each filtered entry into its key (DeliveryAddressType
) and then transform each DeliveryAddressType
into its value. Finally, you collect all these delivery address type values to a list.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments