soorapadman
soorapadman

Reputation: 4509

How to effectively create list using if in java8?

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;
}

Upvotes: 2

Views: 138

Answers (3)

fps
fps

Reputation: 34470

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.

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100279

You may separate the code and the data. Initialize the static map like this:

Map<DeliveryAddressType, Predicate<Subject>> permissions = new HashMap<>();
permissions.put(DeliveryAddressType.MY_DEFAULT_ADDRESS, s -> true);
permissions.put(DeliveryAddressType.DEPOT_ADDRESS, 
    s -> s.isPermitted(PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS));
permissions.put(DeliveryAddressType.CONTACT_ADDRESS, 
    s -> s.isPermitted(PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS));
permissions.put(DeliveryAddressType.AN_ALTERNATIVE_ADDRESS, 
    s -> s.isPermitted(PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS));

Now you can use

private List<String> getDeliveryAddressTypeList() {
    Subject subject = SecurityUtils.getSubject();
    return permissions.entrySet().stream()
        .filter(entry -> entry.getValue().test(subject))
        .map(entry -> entry.getKey().getValue())
        .collect(Collectors.toList());
}

Upvotes: 4

Flown
Flown

Reputation: 11740

The problem here is the mapping PermissionConstant -> DeliveryAddressType. Assuming you have defined a constant Map<PermissionConstant, DeliveryAddressType> like (if insertion order matters you can also use a LinkedHashMap)

Map<PermissionConstant, DeliveryAddressType> mappings = new HashMap<>();
mappings.put(PermissionConstant.PHARMISA_DELIVERY_PFDEPORTS, DeliveryAddressType.DEPOT_ADDRESS);
mappings.put(PermissionConstant.PHARMISA_DELIVERY_ALL_CONTACTS, DeliveryAddressType.CONTACT_ADDRESS);
mappings.put(PermissionConstant.PHARMISA_DELIVERY_FREE_FROM_ADDRESS, DeliveryAddressType.AN_ALTERNATIVE_ADDRESS);

Then you cans simply concat and stream the map to get the result:

private static List<String> getDeliveryAddressTypeList() {
  Subject subject = SecurityUtils.getSubject();
  return Stream.concat(
      Stream.of(DeliveryAddressType.MY_DEFAULT_ADDRESS.getValue()),
        mappings.entrySet().stream()
          .filter(m -> subject.isPermitted(m.getKey()))
          .map(m -> m.getValue().getValue())
      ).collect(Collectors.toList());
}

Upvotes: 1

Related Questions