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

package com.google.common.flogger;

import com.google.common.flogger.util.Checks;
import com.google.common.flogger.backend.Metadata;
import java.util.concurrent.ConcurrentHashMap;

public abstract class LogSiteMap<V>
{
    private final ConcurrentHashMap<LogSiteKey, V> concurrentMap;
    
    protected LogSiteMap() {
        this.concurrentMap = new ConcurrentHashMap<LogSiteKey, V>();
    }
    
    protected abstract V initialValue();
    
    boolean contains(final LogSiteKey key) {
        return this.concurrentMap.containsKey(key);
    }
    
    public final V get(final LogSiteKey key, final Metadata metadata) {
        V value = this.concurrentMap.get(key);
        if (value != null) {
            return value;
        }
        value = Checks.checkNotNull(this.initialValue(), "initial map value");
        final V race = this.concurrentMap.putIfAbsent(key, value);
        if (race != null) {
            return race;
        }
        this.addRemovalHook(key, metadata);
        return value;
    }
    
    private void addRemovalHook(final LogSiteKey key, final Metadata metadata) {
        Runnable removalHook = null;
        for (int i = 0, count = metadata.size(); i < count; ++i) {
            if (LogContext.Key.LOG_SITE_GROUPING_KEY.equals(metadata.getKey(i))) {
                final Object groupByKey = metadata.getValue(i);
                if (groupByKey instanceof LoggingScope) {
                    if (removalHook == null) {
                        removalHook = new Runnable() {
                            @Override
                            public void run() {
                                LogSiteMap.this.concurrentMap.remove(key);
                            }
                        };
                    }
                    ((LoggingScope)groupByKey).onClose(removalHook);
                }
            }
        }
    }
}
