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.context;
018
019import ca.stellardrift.permissionsex.subject.CalculatedSubject;
020
021import java.util.Objects;
022import java.util.function.BiConsumer;
023import java.util.function.Consumer;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * Context definition for types that have no complex data.
029 */
030public class SimpleContextDefinition extends ContextDefinition<String> {
031
032    /**
033     * Create a basic context definition with the provided key and accumulation function.
034     *
035     * @param name context name
036     * @param valueAccumulator a callback taking a subject and a value accumulator
037     * @return a new context definition
038     * @see ContextDefinitionProvider#registerContextDefinition(ContextDefinition) to add this
039     *       context to an engine.
040     */
041    public static SimpleContextDefinition context(final String name, final BiConsumer<CalculatedSubject, Consumer<String>> valueAccumulator) {
042        requireNonNull(name, "name");
043        requireNonNull(valueAccumulator, "valueAccumulator");
044
045        return new SimpleContextDefinition.FromFunction(name, valueAccumulator);
046    }
047
048    protected SimpleContextDefinition(final String name) {
049        super(name);
050    }
051
052    @Override
053    public void accumulateCurrentValues(final CalculatedSubject subject, final Consumer<String> consumer) {
054    }
055
056    @Override
057    public String serialize(final String canonicalValue) {
058        return canonicalValue;
059    }
060
061    @Override
062    public String deserialize(final String userValue) {
063        return userValue;
064    }
065
066    public static final class Fallback extends SimpleContextDefinition {
067        public Fallback(String name) {
068            super(name);
069        }
070    }
071
072    static final class FromFunction extends SimpleContextDefinition {
073        private final BiConsumer<CalculatedSubject, Consumer<String>> valueAccumulator;
074
075        FromFunction(String name, final BiConsumer<CalculatedSubject, Consumer<String>> valueAccumulator) {
076            super(name);
077            this.valueAccumulator = valueAccumulator;
078        }
079
080        @Override
081        public void accumulateCurrentValues(CalculatedSubject subject, Consumer<String> consumer) {
082            this.valueAccumulator.accept(subject, consumer);
083        }
084    }
085}
086