All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.shared.users.Roles Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.shared.users;

import com.google.common.base.Function;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

public abstract class Roles {

    @Nonnull
    public static RoleIdToNameFunction roleIdToNameFunction(Map idMap) {
        return new RoleIdToNameFunction(idMap);
    }

    @Nonnull
    public static RoleNameToIdFunction roleNameToIdFunction(Map nameMap) {
        return new RoleNameToIdFunction(nameMap);
    }

    public static RoleToNameFunction roleToNameFunction() {
        return new RoleToNameFunction(false);
    }

    public static RoleToNameFunction roleToNameFunction(boolean lowerCase) {
        return new RoleToNameFunction(lowerCase);
    }

    private static class RoleIdToNameFunction implements Function {
        private final Map idToRole;

        public RoleIdToNameFunction(Map idToRole) {
            this.idToRole = idToRole;
        }

        @Nullable
        @Override
        public String apply(String roleId) {
            if (roleId == null || !idToRole.containsKey(roleId)) {
                return null;
            }
            return idToRole.get(roleId).getName();
        }
    }

    private static class RoleNameToIdFunction implements Function {

        private final Map nameToRole;

        public RoleNameToIdFunction(Map nameToRole) {
            this.nameToRole = nameToRole;
        }

        @Nullable
        @Override
        public String apply(@Nullable String roleName) {
            if (roleName == null) {
                return null;
            }
            final Role role = nameToRole.get(roleName.toLowerCase());
            if (role == null) {
                return null;
            }
            return role.getId();
        }
    }

    private static class RoleToNameFunction implements Function {
        private final boolean lowerCase;

        public RoleToNameFunction(boolean lowerCase) {
            this.lowerCase = lowerCase;
        }

        @Nullable
        @Override
        public String apply(@Nullable Role input) {
            if (input != null) {
                final String name = input.getName();
                return lowerCase ? name.toLowerCase() : name;
            }
            else return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy