Example usage for java.util.concurrent ConcurrentMap putIfAbsent

List of usage examples for java.util.concurrent ConcurrentMap putIfAbsent

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentMap putIfAbsent.

Prototype

V putIfAbsent(K key, V value);

Source Link

Document

If the specified key is not already associated with a value, associates it with the given value.

Usage

From source file:com.tamnd.core.util.ZConfig.java

private static void ConfigToMap(ConcurrentMap<String, String> map, Configuration config, boolean overwrDup) {
    if (config == null) {
        return;//  w  w w  .  ja  v a2 s . co m
    }
    Iterator<String> keyIt = config.getKeys();
    while (keyIt.hasNext()) {
        String key = keyIt.next();
        if (key == null || key.isEmpty()) {
            continue;
        }
        try {
            String value = config.getString(key);
            if (value == null) {
                continue;
            }
            if (overwrDup) {
                String oldVal = map.put(key, value);
                if (oldVal != null) {
                    System.out.println("Configuration key \"" + key + "\" has old value \"" + oldVal
                            + "\" has been overwritten by new value \"" + value + "\"");
                }
            } else {
                String oldVal = map.putIfAbsent(key, value);
                if (oldVal != null) {
                    System.out.println("Configuration key \"" + key + "\" has value \"" + oldVal
                            + "\" NOT be overwrited by new value \"" + value + "\"");
                }
            }
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
}

From source file:com.graphaware.reco.generic.stats.DefaultStatistics.java

/**
 * {@inheritDoc}//from ww  w.  j a  va2  s  .com
 */
@Override
public void incrementStatistic(String task, String name) {
    ConcurrentMap<String, Object> taskStats = getStatistics(task);

    AtomicInteger count = (AtomicInteger) taskStats.get(name);
    if (count == null) {
        taskStats.putIfAbsent(name, new AtomicInteger());
        count = (AtomicInteger) taskStats.get(name);
    }

    count.incrementAndGet();
}

From source file:org.jiemamy.utils.collection.CollectionsUtil.java

/**
 * ?????????????????/*  ww w.ja  v  a2  s  . c  o  m*/
 * 
 * <p>
 * ???????????? ???????? ???? ??????
 * ????????????? ????? ?????????????
 * ???????????????????
 * </p>
 * 
 * @param <K> {@link HashMap}??
 * @param <V> {@link HashMap}??
 * @param map 
 * @param key ???
 * @param value ???
 * @return ???????????????
 * @throws IllegalArgumentException {@code map}?{@code null}???
 * @see ConcurrentHashMap#putIfAbsent(Object, Object)
 */
public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) {
    Validate.notNull(map);
    V exists = map.putIfAbsent(key, value);
    if (exists != null) {
        return exists;
    }
    return value;
}

From source file:com.weibo.api.motan.util.StatsUtil.java

public static ConcurrentMap<String, AccessStatisticResult> getTotalAccessStatistic(int peroid) {
    if (peroid > MotanConstants.STATISTIC_PEROID) {
        throw new RuntimeException("peroid need <= " + MotanConstants.STATISTIC_PEROID);
    }/*from  w  w w .j a  v a 2  s.  c o m*/

    long currentTimeMillis = System.currentTimeMillis();

    ConcurrentMap<String, AccessStatisticResult> totalResults = new ConcurrentHashMap<String, AccessStatisticResult>();

    for (Map.Entry<String, AccessStatisticItem> entry : accessStatistics.entrySet()) {
        AccessStatisticItem item = entry.getValue();

        AccessStatisticResult result = item.getStatisticResult(currentTimeMillis,
                MotanConstants.STATISTIC_PEROID);

        String key = entry.getKey();
        String[] keys = key.split(SEPARATE);
        if (keys.length != 3) {
            continue;
        }
        String application = keys[1];
        String module = keys[2];
        key = application + "|" + module;
        AccessStatisticResult appResult = totalResults.get(key);
        if (appResult == null) {
            totalResults.putIfAbsent(key, new AccessStatisticResult());
            appResult = totalResults.get(key);
        }

        appResult.totalCount += result.totalCount;
        appResult.bizExceptionCount += result.bizExceptionCount;
        appResult.slowCount += result.slowCount;
        appResult.costTime += result.costTime;
        appResult.bizTime += result.bizTime;
        appResult.otherExceptionCount += result.otherExceptionCount;

    }

    return totalResults;

}

From source file:org.archive.crawler.reporting.StatisticsTracker.java

/**
 * Increment a counter for a key in a given HashMap by an arbitrary amount.
 * Used for various aggregate data. The increment amount can be negative.
 *
 *
 * @param map/*from   w w w  .ja va 2 s  .  c om*/
 *            The HashMap
 * @param key
 *            The key for the counter to be incremented, if it does not exist
 *            it will be added (set to equal to <code>increment</code>).
 *            If null it will increment the counter "unknown".
 * @param increment
 *            The amount to increment counter related to the <code>key</code>.
 */
protected static void incrementMapCount(ConcurrentMap<String, AtomicLong> map, String key, long increment) {
    if (key == null) {
        key = "unknown";
    }
    AtomicLong lw = (AtomicLong) map.get(key);
    if (lw == null) {
        lw = new AtomicLong(0);
        AtomicLong prevVal = map.putIfAbsent(key, lw);
        if (prevVal != null) {
            lw = prevVal;
        }
    }
    lw.addAndGet(increment);
}

From source file:com.github.lightdocs.model.Operation.java

private Parameter getOrCreateParam(String name, ConcurrentMap<String, Parameter> paramMap) {
    Parameter result = paramMap.get(name);
    if (result == null) {
        Parameter newParam = new Parameter(name);
        result = paramMap.putIfAbsent(name, newParam);
        if (result == null) {
            result = newParam;/*  w w w. j  a  v  a 2  s. c o  m*/
        }
    }
    return result;
}

From source file:com.ebay.pulsar.sessionizer.impl.SessionizerErrorManager.java

private long incCounter(String errorName, ErrorType type) {

    ConcurrentMap<String, LongCounter> map = errorCounterMap.get(type.name());
    LongCounter counter = map.get(errorName);
    if (counter == null) {
        counter = new LongCounter();

        LongCounter existed = map.putIfAbsent(errorName, counter);
        if (existed != null) {
            counter = existed;/*from  w  w w. j  av  a 2 s.c o m*/
        }
    }
    return counter.increment();
}

From source file:com.opengamma.component.ComponentConfigLoader.java

private void parseReplacement(String line, ConcurrentMap<String, String> properties) {
    String key = StringUtils.substringBefore(line, "=").trim();
    String value = StringUtils.substringAfter(line, "=").trim();
    // do not overwrite properties that were passed in
    properties.putIfAbsent(key, value);
}

From source file:com.ejisto.event.listener.SessionRecorderManager.java

private <K, V> Set<V> putIfAbsent(K key, ConcurrentMap<K, Set<V>> container) {
    Set<V> values = container.get(key);
    if (values == null) {
        container.putIfAbsent(key, new HashSet<>());
        values = container.get(key);/* w  ww.  j a  v  a 2s.c om*/
    }
    return new HashSet<>(values);
}

From source file:cz.cuni.mff.ufal.AllBitstreamZipArchiveReader.java

/** Creates unique filename based on map of counters of already used filenames */
protected String createUniqueFilename(String filename, ConcurrentMap<String, AtomicInteger> usedFilenames) {
    String uniqueFilename = filename;
    usedFilenames.putIfAbsent(filename, new AtomicInteger(0));
    int occurence = usedFilenames.get(filename).incrementAndGet();
    if (occurence > 1) {
        uniqueFilename = addSuffixToFilename(filename, String.valueOf(occurence));
    }//from w w w  . j ava2s . co  m
    return uniqueFilename;
}