All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.is24.deadcode4j.AnalysisContext Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package de.is24.deadcode4j;

import de.is24.guava.NonNullFunction;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

/**
 * The AnalysisContext extends {@link de.is24.deadcode4j.AnalysisSink} by providing access to the
 * {@link #getModule() analyzed module} and {@link #getIntermediateResult(Object) the intermediate results} of the
 * modules it depends on. Additionally, it provides a {@link #getCache() cache} to use for caching calculated
 * data relevant for one context.
 *
 * @since 1.1.0
 */
public class AnalysisContext extends AnalysisSink {
    @Nonnull
    private final Map cache = newHashMap();
    @Nonnull
    private final Module module;
    @Nonnull
    private final Map intermediateResults;

    /**
     * Creates a new instance of AnalysisContext for the specified module.
     *
     * @since 2.0.0
     */
    public AnalysisContext(@Nonnull Module module, @Nonnull Map intermediateResults) {
        this.module = module;
        this.intermediateResults = newHashMap(intermediateResults);
    }

    @Override
    public String toString() {
        return "AnalysisContext for [" + this.module + "]";
    }

    /**
     * Returns the associated Module.
     *
     * @since 2.0.0
     */
    @Nonnull
    public Module getModule() {
        return module;
    }

    /**
     * Returns a Map that can be used to cache things or pass along between analyzers.
     *
     * @return a simple {@link java.util.Map}
     */
    @Nonnull
    public Map getCache() {
        return cache;
    }

    @Nonnull
    public  T getOrCreateCacheEntry(Object key, NonNullFunction supplier) {
        @SuppressWarnings("unchecked")
        T entry = (T) getCache().get(key);
        if (entry == null) {
            entry = supplier.apply(this);
            getCache().put(key, entry);
        }
        return entry;
    }

    @Nullable
    public IntermediateResult getIntermediateResult(@Nonnull Object key) {
        return this.intermediateResults.get(key);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy