<commit_before>/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.objcache.spi;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import org.opendaylight.yangtools.concepts.ProductAwareBuilder;
import org.opendaylight.yangtools.objcache.ObjectCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.FinalizableReferenceQueue;
import com.google.common.base.FinalizableSoftReference;
import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;

/**
 * Abstract object cache implementation. This implementation takes care
 * of interacting with the user and manages interaction with the Garbage
 * Collector (via soft references). Subclasses are expected to provide
 * a backing {@link Cache} instance and provide the
 */
public abstract class AbstractObjectCache implements ObjectCache {
    /**
     * Key used when looking up a ProductAwareBuilder product. We assume
     * the builder is not being modified for the duration of the lookup,
     * anything else is the user's fault.
     */
    @VisibleForTesting
    static final class BuilderKey {
        private final ProductAwareBuilder<?> builder;

        private BuilderKey(final ProductAwareBuilder<?> builder) {
            this.builder = Preconditions.checkNotNull(builder);
        }

        @Override
        public int hashCode() {
            return builder.productHashCode();
        }

        @Override
        public boolean equals(Object obj) {
            /*
             * We can tolerate null objects coming our way, but we need
             * to be on the lookout for WeakKeys, as we cannot pass them
             * directly to productEquals().
             */
            if (obj != null && obj instanceof SoftKey) {
                obj = ((SoftKey<?>)obj).get();
            }

            return builder.productEquals(obj);
        }
    }

    /**
     * Key used in the underlying map. It is essentially a soft reference, with
     * slightly special properties.
     *<commit_msg>BUG-869: removed redundant check for null before an instanceof<commit_after>/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.objcache.spi;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.FinalizableReferenceQueue;
import com.google.common.base.FinalizableSoftReference;
import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import org.opendaylight.yangtools.concepts.ProductAwareBuilder;
import org.opendaylight.yangtools.objcache.ObjectCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract object cache implementation. This implementation takes care
 * of interacting with the user and manages interaction with the Garbage
 * Collector (via soft references). Subclasses are expected to provide
 * a backing {@link Cache} instance and provide the
 */
public abstract class AbstractObjectCache implements ObjectCache {
    /**
     * Key used when looking up a ProductAwareBuilder product. We assume
     * the builder is not being modified for the duration of the lookup,
     * anything else is the user's fault.
     */
    @VisibleForTesting
    static final class BuilderKey {
        private final ProductAwareBuilder<?> builder;

        private BuilderKey(final ProductAwareBuilder<?> builder) {
            this.builder = Preconditions.checkNotNull(builder);
        }

        @Override
        public int hashCode() {
            return builder.productHashCode();
        }

        @Override
        public boolean equals(Object obj) {
            /*
             * We can tolerate null objects coming our way, but we need
             * to be on the lookout for WeakKeys, as we cannot pass them
             * directly to productEquals().
             */
            if (obj instanceof SoftKey) {
                obj = ((SoftKey<?>)obj).get();
            }

            return builder.productEquals(obj);
        }
    }

    /**
     * Key used in the underlying map. It is essentially a soft reference, with
     * slightly special properties.
     *
{'fraction_non_alphanumeric': 0.07838283828382839, 'fraction_numerical': 0.004744224422442244, 'mean_word_length': 2.9358766233766236, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7397191', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1200, 'n_words': 430}