com.ibm.wala.util.collections.ObjectArrayMapping Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.ibm.wala.util Show documentation
Show all versions of com.ibm.wala.util Show documentation
T. J. Watson Libraries for Analysis
The newest version!
/*
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*/
package com.ibm.wala.util.collections;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.debug.UnimplementedError;
import com.ibm.wala.util.intset.OrdinalSetMapping;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
/**
* A bit set mapping based on an immutable object array. This is not terribly efficient, but is
* useful for prototyping.
*/
public class ObjectArrayMapping implements OrdinalSetMapping {
private final T[] array;
/** A mapping from object to Integer */
private final HashMap map = HashMapFactory.make();
public ObjectArrayMapping(final T[] array) {
if (array == null) {
throw new IllegalArgumentException("null array");
}
this.array = array;
for (int i = 0; i < array.length; i++) {
map.put(array[i], i);
}
}
@Override
public T getMappedObject(int n) throws NoSuchElementException {
try {
return array[n];
} catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("invalid n: " + n, e);
}
}
@Override
public int getMappedIndex(@Nullable Object o) {
if (map.get(o) == null) {
return -1;
}
return map.get(o);
}
@Override
public boolean hasMappedIndex(Object o) {
return map.get(o) != null;
}
@Override
public Iterator iterator() {
return map.keySet().iterator();
}
@Override
public Stream stream() {
return map.keySet().stream();
}
@Override
public int add(Object o) throws UnimplementedError {
Assertions.UNREACHABLE();
return 0;
}
@Override
public int getMaximumIndex() {
return array.length - 1;
}
@Override
public int getSize() {
return map.size();
}
}