class ResourceFactory {

    static IResource toResource(String resourceName)
    {
        String[] parts = StringUtils.split(resourceName, SEPARATOR, 2);

        switch (parts[0])
        {
            case DATA_ROOT:
                DataResource dataResource = DataResource.fromName(resourceName);
                validateDataResourceName(dataResource);
                return dataResource;
            case ROLES_ROOT:
                RoleResource roleResource = RoleResource.fromName(resourceName);
                validateRoleResourceName(roleResource);
                return roleResource;
            case CONNECTIONS_ROOT:
                return ConnectionResource.fromName(resourceName);
            case FUNCTIONS_ROOT:
                FunctionResource functionResource = FunctionResource.fromName(resourceName);
                validateFunctionResourceName(functionResource);
                return functionResource;
            case GRANT_ROOT:
                if (parts.length == 1)
                {
                    return GrantResource.root();
                }
                String wrappedResourceName = parts[1];
                Preconditions.checkArgument(!wrappedResourceName.startsWith(GRANT_ROOT), "Invalid resource type: %s, recursive grants not allowed", resourceName);
                IResource wrappedResource = toResource(wrappedResourceName);
                return GrantResource.fromResource(wrappedResource);
            default:
                throw new IllegalArgumentException("Invalid resource type: " + resourceName);
        }
    }

    private  ResourceFactory();

}