001/*
002 * PermissionsEx
003 * Copyright (C) zml and PermissionsEx contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ca.stellardrift.permissionsex.impl.context;
018
019import ca.stellardrift.permissionsex.impl.config.PermissionsExConfiguration;
020import ca.stellardrift.permissionsex.subject.CalculatedSubject;
021import org.checkerframework.checker.nullness.qual.Nullable;
022
023import java.util.Collections;
024import java.util.List;
025import java.util.Objects;
026import java.util.function.Consumer;
027
028/**
029 * Applies {@code server-tag} context values based on tags defined in the configuration.
030 */
031public class ServerTagContextDefinition extends PEXContextDefinition<String> {
032    public static final ServerTagContextDefinition INSTANCE = new ServerTagContextDefinition();
033    private List<String> activeTags = Collections.emptyList();
034
035    private ServerTagContextDefinition() {
036        super("server-tag");
037    }
038
039    @Override
040    public String serialize(final String canonicalValue) {
041        return canonicalValue;
042    }
043
044    @Override
045    public @Nullable String deserialize(final String userValue) {
046        return userValue;
047    }
048
049    @Override
050    public boolean matches(final String ownVal, final String testVal) {
051        return Objects.equals(ownVal, testVal);
052    }
053
054    @Override
055    public void accumulateCurrentValues(final CalculatedSubject subject, final Consumer<String> consumer) {
056        this.activeTags.forEach(consumer);
057    }
058
059    @Override
060    public void update(PermissionsExConfiguration<?> config) {
061        activeTags = config.getServerTags();
062    }
063}