How to convert a list of strings to a list of objects?

I have a list of roles in the database. They have the form

application.Role1.read
application.Role1.write
application.Role2.read
application.Role3.read

Thus, each role has a record based on read and write permission. I want to convert roles to POJOs, which I can then send JSON to the user interface. Each POJO will have a role name and a boolean for reading or writing.

Here is the RolePermission class:

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RolePermission {
    private String roleName;
    private boolean readAllowed;
    private boolean writeAllowed;

    public String getRoleName() {
        return roleName;
    }

    public RolePermission setRoleName(String roleName) {
        this.roleName = roleName;
        return this;
    }

    public boolean isReadAllowed() {
        return readAllowed;
    }

    public RolePermission setReadAllowed(boolean readAllowed) {
        this.readAllowed = readAllowed;
        return this;
    }

    public boolean isWriteAllowed() {
        return writeAllowed;
    }

    public RolePermission setWriteAllowed(boolean writeAllowed) {
        this.writeAllowed = writeAllowed;
        return this;
    }
}

I am doing the conversion as follows:

public static final String ROLE_PREFIX = "application.";
public static final String ROLE_READ_PERMISSION = "read";
public static final String ROLE_WRITE_PERMISSION = "write";

@Override
public List<RolePermission> getRoles(Backend backend) {
    List<String> allRoles = backend.getRoles()
            .stream()
            .map(s -> s.replace(ROLE_PREFIX, ""))
            .sorted()
            .collect(Collectors.toList());
    Map<String, RolePermission> roleMap = new HashMap<>();
    for (String role : allRoles) {
        String[] tokens = role.split(".");
        String roleName = tokens[0];
        String permission = tokens[1];
        if (!roleMap.containsKey(roleName))
            roleMap.put(roleName, new RolePermission().setRoleName(roleName));
        RolePermission permission = roleMap.get(roleName);
        if (ROLE_READ_PERMISSION.equals(permission))
            permission.setReadAllowed(true);
        if (ROLE_WRITE_PERMISSION.equals(permission))
            permission.setWriteAllowed(true);
    }
    return new LinkedList<>(roleMap.values());
}

Is there a way to make the foreach loop higher using Java 8 threads?

This is a mock instance of Backend that simply returns a list of roles:

public class Backend {
    public List<String> getRoles() {
        return Arrays.asList(
            "application.Role1.read",
            "application.Role1.write",
            "application.Role2.read",
            "application.Role3.read"
        );
    }
}
+6
source share
3 answers

groupingBy, .

public static final String ROLE_PREFIX = "application.";
public static final String ROLE_READ_PERMISSION = "read";
public static final String ROLE_WRITE_PERMISSION = "write";

@Override
public List<RolePermission> getRoles(Backend backend) {
    Map<String, List<String[]>> allRoles = backend.getRoles()
            .stream()
            .map(s -> s.replace(ROLE_PREFIX, "")) // something like "Role1.read"
            .map(s -> s.split("\\.")) // something like ["Role1", "read"]
            .collect(Collectors.groupingBy(split -> split[0]));
    return allRoles.values()
                   .stream()
                   .map(this::buildPermission)
                   .collect(Collectors.toList());
}

private RolePermission buildPermission(List<String[]> roleEntries) {
    RolePermission permission = new RolePermission().setRoleName(roleEntries.get(0)[0]);
    roleEntries.stream()
               .forEach(entry -> {
                   if (ROLE_READ_PERMISSION.equals(entry[1]))
                       permission.setReadAllowed(true);
                   if (ROLE_WRITE_PERMISSION.equals(entry[1]))
                       permission.setWriteAllowed(true);
               });
    return permission;
}

, String.split , . . , .

:

[RolePermission(roleName=Role3, readAllowed=true, writeAllowed=false), 
 RolePermission(roleName=Role2, readAllowed=true, writeAllowed=false),
 RolePermission(roleName=Role1, readAllowed=true, writeAllowed=true)]
+4

for map toMap collector:

public List<RolePermission> getRoles(Backend backend)
{
    Map<String, RolePermission> allRoles = backend.getRoles()
            .stream()
            .map(s -> s.replace(ROLE_PREFIX, ""))
            .sorted()
            .map(this::mapStringToRolePermission)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, RolePermission::merge));
    return new ArrayList<>(allRoles.values());
}

mapStringToRolePermission:

private static Map.Entry<String, RolePermission> mapStringToRolePermission(String role)
{
    String roleName = role.substring(0, role.indexOf('.'));
    RolePermission rolePermission = new RolePermission();
    rolePermission.setRoleName(roleName);
    rolePermission.setReadAllowed(role.endsWith(ROLE_READ_PERMISSION));
    rolePermission.setWriteAllowed(role.endsWith(ROLE_WRITE_PERMISSION));
    return new AbstractMap.SimpleEntry<>(roleName, rolePermission);
}

merge RolePermission:

public RolePermission merge(RolePermission another)
{
    if (another.isReadAllowed())
        setReadAllowed(true);
    if (another.isWriteAllowed())
        setWriteAllowed(true);
    return this;
}
0

Java , String RolePermission ( , ), . - ? , String RolePermission.

final List<RolePermission> roles = getRoles().stream().map(roleString -> { <your conversion code> }).collect(Collectors.toList());
-1

All Articles