
com.newrelic.agent.service.analytics.LimitedSizeHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of newrelic-agent Show documentation
Show all versions of newrelic-agent Show documentation
Jar required to run with a java application to monitor performance.
The newest version!
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.newrelic.agent.service.analytics;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class LimitedSizeHashMap implements Map {
private final int max;
private final Map map;
public LimitedSizeHashMap(int max) {
this.max = max;
this.map = new HashMap<>();
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public V get(Object key) {
return map.get(key);
}
@Override
public V put(K key, V value) {
if (map.size() < max) {
return map.put(key, value);
}
return null;
}
@Override
public V remove(Object key) {
return map.remove(key);
}
@Override
public void putAll(Map extends K, ? extends V> m) {
for (Map.Entry extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
if (map.size() >= max) {
// fail fast
return;
}
}
}
public void putAllIfAbsent(Map extends K, ? extends V> m) {
for (Map.Entry extends K, ? extends V> entry : m.entrySet()) {
if (!map.containsKey(entry.getKey())) {
put(entry.getKey(), entry.getValue());
if (map.size() >= max) {
// fail fast
return;
}
}
}
}
@Override
public void clear() {
map.clear();
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public Collection values() {
return map.values();
}
@Override
public Set> entrySet() {
return map.entrySet();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LimitedSizeHashMap, ?> that = (LimitedSizeHashMap, ?>) o;
return max == that.max &&
Objects.equals(map, that.map);
}
@Override
public int hashCode() {
return Objects.hash(max, map);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy