com.force.i18n.commons.util.collection.AbstractIntMap Maven / Gradle / Ivy
Show all versions of grammaticus Show documentation
/*
* 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.commons.util.collection;
import java.util.Iterator;
import com.google.common.annotations.Beta;
/**
* This class provides a skeletal implementation of the {@code IntMap}
* interface, to minimize the effort required to implement this interface.
*
* This borrows heavily from java.util.AbstractMap
*
* Beta class. Classes under com.force.i18n.commons package will be moved into a dedicated project.
*
* @author stamm
*/
@Beta
public abstract class AbstractIntMap implements IntMap {
private static final long serialVersionUID = 1L;
public AbstractIntMap() {
super();
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public void putAll(IntMap extends V> t) {
for (IntMap.Entry extends V> e : t.entrySet()) {
put(e.getKey(), e.getValue());
}
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof IntMap))
return false;
@SuppressWarnings("unchecked")
IntMap t = (IntMap)o;
if (t.size() != size())
return false;
try {
Iterator> i = entrySet().iterator();
while (i.hasNext()) {
Entry e = i.next();
int key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(t.get(key) == null && t.containsKey(key)))
return false;
} else {
if (!value.equals(t.get(key)))
return false;
}
}
}
catch (ClassCastException unused) {
return false;
}
catch (NullPointerException unused) {
return false;
}
return true;
}
@Override
public int hashCode() {
int h = 0;
for (Entry entry : entrySet()) {
h += entry.hashCode();
}
return h;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(this.size() * 16);
buf.append("{");
Iterator> i = entrySet().iterator();
boolean hasNext = i.hasNext();
while (hasNext) {
Entry e = i.next();
int key = e.getKey();
V value = e.getValue();
buf.append(key);
buf.append("=");
if (value == this)
buf.append("(this Map)");
else
buf.append(value);
hasNext = i.hasNext();
if (hasNext)
buf.append(", ");
}
buf.append("}");
return buf.toString();
}
}