<commit_before>/**
 * PermissionsEx
 * Copyright (C) zml and PermissionsEx contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ninja.leaping.permissionsex.backends.memory;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import ninja.leaping.configurate.objectmapping.ObjectMapper;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import ninja.leaping.permissionsex.data.ImmutableOptionSubjectData;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Map.Entry;

public class MemoryOptionSubjectData implements ImmutableOptionSubjectData {
    protected static final ObjectMapper<DataEntry> MAPPER;
    static {
        try {
            MAPPER = ObjectMapper.forClass(DataEntry.class);
        } catch (ObjectMappingException e) {
            throw new ExceptionInInitializerError(e); // This error indicates a programming issue
        }
    }

    protected static <K, V> Map<K, V> updateImmutable(Map<K, V> input, K newKey, V newVal) {
        Map<K, V> ret = new HashMap<>(input);
        ret.put(newKey, newVal);
        return Collections.unmodifiableMap(ret);
    }
    @ConfigSerializable
    protected static class DataEntry {
        @Setting private Map<String, Integer> permissions;
        @Setting private Map<String, String> options;
        @Setting private List<String> parents;
        @Setting("permissions-default") private int defaultValue;

        private DataEntry(Map<String, Integer> permissions, Map<String, String> options, List<String> parents, int defaultValue) {
            this.permissions = permissions;
            this.options = options;
            this.parents = parents;
            this.defaultValue = defaultValue;
        }<commit_msg>Correct handling of updating when starting with a null map<commit_after>/**
 * PermissionsEx
 * Copyright (C) zml and PermissionsEx contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ninja.leaping.permissionsex.backends.memory;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import ninja.leaping.configurate.objectmapping.ObjectMapper;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import ninja.leaping.permissionsex.data.ImmutableOptionSubjectData;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Map.Entry;

public class MemoryOptionSubjectData implements ImmutableOptionSubjectData {
    protected static final ObjectMapper<DataEntry> MAPPER;
    static {
        try {
            MAPPER = ObjectMapper.forClass(DataEntry.class);
        } catch (ObjectMappingException e) {
            throw new ExceptionInInitializerError(e); // This error indicates a programming issue
        }
    }

    protected static <K, V> Map<K, V> updateImmutable(Map<K, V> input, K newKey, V newVal) {
        if (input == null) {
            return ImmutableMap.of(newKey, newVal);
        }
        Map<K, V> ret = new HashMap<>(input);
        ret.put(newKey, newVal);
        return Collections.unmodifiableMap(ret);
    }
    @ConfigSerializable
    protected static class DataEntry {
        @Setting private Map<String, Integer> permissions;
        @Setting private Map<String, String> options;
        @Setting private List<String> parents;
        @Setting("permissions-default") private int defaultValue;

        private DataEntry(Map<String, Integer> permissions, Map<String, String> options, List<String> parents, int defaultValue) {
            this.permissions = permissions;
            this.options = options;
            this.parents = parents;
            this.defaultValue = defaultValue;
        }
{'fraction_non_alphanumeric': 0.08804920913884007, 'fraction_numerical': 0.00140597539543058, 'mean_word_length': 3.814720812182741, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6276536', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1503, 'n_words': 453}