// 
// Decompiled by Procyon v0.6.0
// 

package org.jline.style;

import java.util.Set;
import java.util.Collections;
import java.util.Objects;
import javax.annotation.Nullable;
import java.util.logging.Level;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
import java.util.logging.Logger;

public class MemoryStyleSource implements StyleSource
{
    private static final Logger log;
    private final Map<String, Map<String, String>> backing;
    
    public MemoryStyleSource() {
        this.backing = new ConcurrentHashMap<String, Map<String, String>>();
    }
    
    @Nullable
    @Override
    public String get(final String group, final String name) {
        String style = null;
        final Map<String, String> styles = this.backing.get(group);
        if (styles != null) {
            style = styles.get(name);
        }
        if (MemoryStyleSource.log.isLoggable(Level.FINEST)) {
            MemoryStyleSource.log.finest(String.format("Get: [%s] %s -> %s", group, name, style));
        }
        return style;
    }
    
    @Override
    public void set(final String group, final String name, final String style) {
        Objects.requireNonNull(group);
        Objects.requireNonNull(name);
        Objects.requireNonNull(style);
        this.backing.computeIfAbsent(group, k -> new ConcurrentHashMap()).put(name, style);
        if (MemoryStyleSource.log.isLoggable(Level.FINEST)) {
            MemoryStyleSource.log.finest(String.format("Set: [%s] %s -> %s", group, name, style));
        }
    }
    
    @Override
    public void remove(final String group) {
        Objects.requireNonNull(group);
        if (this.backing.remove(group) != null && MemoryStyleSource.log.isLoggable(Level.FINEST)) {
            MemoryStyleSource.log.finest(String.format("Removed: [%s]", group));
        }
    }
    
    @Override
    public void remove(final String group, final String name) {
        Objects.requireNonNull(group);
        Objects.requireNonNull(name);
        final Map<String, String> styles = this.backing.get(group);
        if (styles != null) {
            styles.remove(name);
            if (MemoryStyleSource.log.isLoggable(Level.FINEST)) {
                MemoryStyleSource.log.finest(String.format("Removed: [%s] %s", group, name));
            }
        }
    }
    
    @Override
    public void clear() {
        this.backing.clear();
        MemoryStyleSource.log.finest("Cleared");
    }
    
    @Override
    public Iterable<String> groups() {
        return (Iterable<String>)Collections.unmodifiableSet((Set<?>)this.backing.keySet());
    }
    
    @Override
    public Map<String, String> styles(final String group) {
        Objects.requireNonNull(group);
        Map<String, String> result = this.backing.get(group);
        if (result == null) {
            result = Collections.emptyMap();
        }
        return Collections.unmodifiableMap((Map<? extends String, ? extends String>)result);
    }
    
    static {
        log = Logger.getLogger(MemoryStyleSource.class.getName());
    }
}
