org.eclipse.core.internal.utils.ObjectMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aspectjtools Show documentation
Show all versions of aspectjtools Show documentation
AspectJ tools most notably contains the AspectJ compiler (AJC). AJC applies aspects to Java classes during
compilation, fully replacing Javac for plain Java classes and also compiling native AspectJ or annotation-based
@AspectJ syntax. Furthermore, AJC can weave aspects into existing class files in a post-compile binary weaving step.
This library is a superset of AspectJ weaver and hence also of AspectJ runtime.
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* James Blackburn (Broadcom Corp.) - ongoing development
* Lars Vogel - Bug 473427
*******************************************************************************/
package org.eclipse.core.internal.utils;
import java.util.*;
/**
* A specialized map implementation that is optimized for a
* small set of object keys.
*
* Implemented as a single array that alternates keys and values.
*/
@SuppressWarnings("unchecked")
public class ObjectMap implements Map, IStringPoolParticipant {
// 8 attribute keys, 8 attribute values
protected static final int DEFAULT_SIZE = 16;
protected static final int GROW_SIZE = 10;
protected int count = 0;
protected Object[] elements = null;
/**
* Creates a new object map of default size
*/
public ObjectMap() {
this(DEFAULT_SIZE);
}
/**
* Creates a new object map.
* @param initialCapacity The initial number of elements that will fit in the map.
*/
public ObjectMap(int initialCapacity) {
if (initialCapacity > 0)
elements = new Object[Math.max(initialCapacity * 2, 0)];
}
/**
* Creates a new object map of the same size as the given map and
* populate it with the key/attribute pairs found in the map.
* @param map The entries in the given map will be added to the new map.
*/
public ObjectMap(Map map) {
this(map.size());
putAll(map);
}
/**
* @see Map#clear()
*/
@Override
public void clear() {
elements = null;
count = 0;
}
/**
* @see java.lang.Object#clone()
*/
@Override
public Object clone() {
return new ObjectMap<>(this);
}
/**
* @see Map#containsKey(java.lang.Object)
*/
@Override
public boolean containsKey(Object key) {
if (elements == null || count == 0)
return false;
for (int i = 0; i < elements.length; i = i + 2)
if (elements[i] != null && elements[i].equals(key))
return true;
return false;
}
/**
* @see Map#containsValue(java.lang.Object)
*/
@Override
public boolean containsValue(Object value) {
if (elements == null || count == 0)
return false;
for (int i = 1; i < elements.length; i = i + 2)
if (elements[i] != null && elements[i].equals(value))
return true;
return false;
}
/**
* @see Map#entrySet()
* This implementation does not conform properly to the specification
* in the Map interface. The returned collection will not be bound to
* this map and will not remain in sync with this map.
*/
@Override
public Set> entrySet() {
return count == 0 ? Collections.EMPTY_SET : toHashMap().entrySet();
}
/**
* See Object#equals
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof Map))
return false;
Map