/*
 * This file is part of LuckPerms, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package me.lucko.luckperms.common.util;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

public interface LoadingMap<K, V> extends Map<K, V> {
    static <K, V> LoadingMap<K, V> of(Map<K, V> map, Function<K, V> function) {
        return new LoadingMapImpl<>(map, function);
    }

    static <K, V> LoadingMap<K, V> of(Function<K, V> function) {
        return of(new ConcurrentHashMap<>(), function);
    }
}

final class LoadingMapImpl<K, V> implements LoadingMap<K, V> {
    private final Map<K, V> map;
    private final Function<K, V> function;

    LoadingMapImpl(Map<K, V> map, Function<K, V> function) {
        this.map = map;
        this.function = function;
    }

    @Override
    public int size() {
        return this.map.size();
    }

    @Override
    public boolean isEmpty() {
        return this.map.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return this.map.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return this.map.containsValue(value);
    }

    @Override
    public V get(Object key) {
        V value = this.map.get(key);
        if (value != null) {
            return value;
        }
        return this.map.computeIfAbsent((K) key, this.function);
    }

    @Override
    public V put(K key, V value) {
        return this.map.put(key, value);
    }

    @Override
    public V remove(Object key) {
        return this.map.remove(key);
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> that) {
        this.map.putAll(that);
    }

    @Override
    public void clear() {
        this.map.clear();
    }

    @Override
    public Set<K> keySet() {
        return this.map.keySet();
    }

    @Override
    public Collection<V> values() {
        return this.map.values();
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return this.map.entrySet();
    }
}
@@ -1291,52 +1291,50 @@
 ort 
-java.util.Collection;%0Aimport java.util.
+com.google.common.collect.Forwarding
 Map;%0A
+%0A
 impo
@@ -1346,19 +1346,19 @@
 va.util.
-Set
+Map
 ;%0Aimport
@@ -1446,17 +1446,13 @@
 lic 
-interface
+class
  Loa
@@ -1473,16 +1473,47 @@
 extends 
+ForwardingMap%3CK, V%3E implements 
 Map%3CK, V
@@ -1516,24 +1516,31 @@
 K, V%3E %7B%0A    
+public 
 static %3CK, V
@@ -1632,20 +1632,16 @@
 adingMap
-Impl
 %3C%3E(map, 
@@ -1662,16 +1662,23 @@
  %7D%0A%0A    
+public 
 static %3C
@@ -1796,73 +1796,8 @@
   %7D%0A
-%7D%0A%0Afinal class LoadingMapImpl%3CK, V%3E implements LoadingMap%3CK, V%3E %7B
 %0A   
@@ -1874,16 +1874,24 @@
 n;%0A%0A    
+private 
 LoadingM
@@ -1896,12 +1896,8 @@
 gMap
-Impl
 (Map
@@ -2023,320 +2023,36 @@
    p
-ublic int size() %7B%0A        return this.map.size();%0A    %7D%0A%0A    @Override%0A    public boolean isEmpty() %7B%0A        return this.map.isEmpty();%0A    %7D%0A%0A    @Override%0A    public boolean containsKey(Object key) %7B%0A        return this.map.containsKey(key);%0A    %7D%0A%0A    @Override%0A    public boolean containsValue(Object value
+rotected Map%3CK, V%3E delegate(
 ) %7B%0A
@@ -2078,29 +2078,8 @@
 .map
-.containsValue(value)
 ;%0A  
@@ -2304,654 +2304,6 @@
    %7D
-%0A%0A    @Override%0A    public V put(K key, V value) %7B%0A        return this.map.put(key, value);%0A    %7D%0A%0A    @Override%0A    public V remove(Object key) %7B%0A        return this.map.remove(key);%0A    %7D%0A%0A    @Override%0A    public void putAll(Map%3C? extends K, ? extends V%3E that) %7B%0A        this.map.putAll(that);%0A    %7D%0A%0A    @Override%0A    public void clear() %7B%0A        this.map.clear();%0A    %7D%0A%0A    @Override%0A    public Set%3CK%3E keySet() %7B%0A        return this.map.keySet();%0A    %7D%0A%0A    @Override%0A    public Collection%3CV%3E values() %7B%0A        return this.map.values();%0A    %7D%0A%0A    @Override%0A    public Set%3CEntry%3CK, V%3E%3E entrySet() %7B%0A        return this.map.entrySet();%0A    %7D
 %0A%7D

Implement LoadingMap using Guava's ForwardingMap utility