/**
 * © David Attias 2015
 */
package io.spacedog.client.credentials;

import java.util.HashMap;
import java.util.Set;

import com.google.common.collect.Sets;

@SuppressWarnings("serial")
public class ObjectRolePermissions extends HashMap<String, RolePermissions> {

	public ObjectRolePermissions put(String objectId, String role, Permission... permissions) {
		RolePermissions rolePermissions = super.get(objectId);
		if (rolePermissions == null) {
			rolePermissions = new RolePermissions();
			super.put(objectId, rolePermissions);
		}
		rolePermissions.put(role, permissions);
		return this;
	}

	public boolean containsOne(String objectId, String role, Permission... permissions) {
		RolePermissions roles = super.get(objectId);
		return roles == null ? false : roles.containsOne(role, permissions);
	}

	@Override
	public RolePermissions get(Object objectId) {
		RolePermissions roles = super.get(objectId);
		return roles == null ? new RolePermissions() : roles;
	}

	public Set<String> accessList(Credentials credentials, Permission permission) {
		Set<String> ids = Sets.newHashSet();

		if (credentials.isAtLeastSuperAdmin())
			ids.addAll(keySet());
		else
			for (String id : keySet()) {
				if (containsOne(id, Roles.all, permission))
					ids.add(id);
				else
					for (String role : credentials.roles())
						if (containsOne(id, role, permission))
							ids.add(id);
			}

		return ids;
	}

}

@@ -386,30 +386,24 @@
 rmissions = 
-super.
 get(objectId
@@ -409,130 +409,8 @@
 d);%0A
-%09%09if (rolePermissions == null) %7B%0A%09%09%09rolePermissions = new RolePermissions();%0A%09%09%09super.put(objectId, rolePermissions);%0A%09%09%7D%0A
 %09%09ro
@@ -774,39 +774,36 @@
 et(objectId);%0A%09%09
-return 
+if (
 roles == null ? 
@@ -799,18 +799,30 @@
  == null
- ?
+) %7B%0A%09%09%09roles =
  new Rol
@@ -835,18 +835,72 @@
 ssions()
- :
+;%0A%09%09%09super.put(objectId.toString(), roles);%0A%09%09%7D%0A%09%09return
  roles;%0A

Improve get(Object) of ObjectRolePermissions class