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

com.force.i18n.grammar.parser.ConcurrentUniquefy Maven / Gradle / Ivy

There is a newer version: 1.2.30
Show newest version
/*
 * Copyright (c) 2017, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

package com.force.i18n.grammar.parser;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * A version of uniquefy that works in a "concurrent" fashion, so that it's not actually
 * "unique", but close enough to prevent leakage.  Use this for fixed-set caches.
 * @author stamm
 */
public class ConcurrentUniquefy implements Serializable {

    private final ConcurrentMap pool;

    /**
     * Create a new string pool.
     */
    public ConcurrentUniquefy() {
        this.pool = new ConcurrentHashMap(128, .75f, 2);
    }

    /**
     * Look to see if the given value is already in the given string pool.
     * If so, return the one already in the pool.  Otherwise, add it to the pool.
     * @param value the value to make unique
     * @return the value or an equivalent already in the pool.
     */
    public T unique(T value) {
        if (value == null) return null;

        T result = this.pool.get(value);
        if (result != null) return result;

        result = this.pool.putIfAbsent(value, value);
        assert result == null || result.equals(value) : "There's a flaw in the equals logic associated with " + value.getClass();
        return result == null ? value : result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy