com.signalfx.shaded.fasterxml.jackson.jr.ob.impl.DeferredMap Maven / Gradle / Ivy
Show all versions of signalfx-codahale Show documentation
package com.signalfx.shaded.fasterxml.jackson.jr.ob.impl;
import java.util.*;
/**
* A specialized {@link java.util.Map} implementation that will collect
* entries during building, but only materialize full lookup structure
* when needed; that is, actual building of hash lookup is deferred.
*
* Inspired by lazily initialized Map used by Boon library.
*/
public class DeferredMap extends AbstractMap
{
private Map _map;
private Object[] _entries;
private int _end;
private final boolean _ordered;
public DeferredMap(boolean ordered) {
this(ordered, 4);
}
public DeferredMap(boolean ordered, int initialSize) {
_ordered = ordered;
}
@Override
public Object put(String key, Object value)
{
if (_map == null) {
if (_entries == null) {
_entries = new Object[8];
} else if (_end == _entries.length) {
final int newSize = _newSize(_end);
_entries = Arrays.copyOf(_entries, newSize);
}
_entries[_end] = key;
_entries[++_end] = value;
++_end;
// here's assuming no dups are added
return null;
}
return _map.put(key, value);
}
@Override
public Set> entrySet() {
buildIfNeeded();
return _map.entrySet();
}
@Override
public int size() {
// assuming no dups; otherwise could overestimate
return (_map == null) ? (_end >> 1) : _map.size();
}
@Override
public boolean isEmpty() {
return (_map == null) ? (_end == 0) : _map.isEmpty();
}
@Override
public boolean containsValue(Object value) {
buildIfNeeded();
return _map.containsValue(value);
}
@Override
public boolean containsKey(Object key) {
buildIfNeeded();
return _map.containsKey(key);
}
@Override
public Object get(Object key) {
buildIfNeeded();
return _map.get( key );
}
@Override
public Object remove(Object key) {
buildIfNeeded();
return _map.remove( key );
}
@Override
public void clear() {
if (_map != null ) {
_map.clear();
} else {
_end = 0;
}
}
@Override
public Set keySet() {
buildIfNeeded();
return _map.keySet();
}
@Override
public Collection