Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.remoting3._private;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.IntFunction;
import java.util.function.ToIntFunction;
/**
* Lock-free concurrent integer-indexed hash map.
*
* @param the value type
*
* @author David M. Lloyd
*/
public final class IntIndexHashMap extends AbstractCollection implements IntIndexMap {
private static final int DEFAULT_INITIAL_CAPACITY = 512;
private static final int MAXIMUM_CAPACITY = 1 << 30;
private static final float DEFAULT_LOAD_FACTOR = 0.3f;
/** A row which has been resized into the new view. */
private static final Object[] RESIZED = new Object[0];
/** A non-existent table entry (as opposed to a {@code null} value). */
private static final Object NONEXISTENT = new Object();
private final ToIntFunction super V> indexer;
private final Equaller super V> ve;
private volatile Table table;
private final float loadFactor;
private final int initialCapacity;
@SuppressWarnings({"unchecked", "rawtypes"})
private static final AtomicIntegerFieldUpdater
sizeUpdater = AtomicIntegerFieldUpdater.newUpdater(Table.class, "size");
@SuppressWarnings({"unchecked", "rawtypes"})
private static final AtomicReferenceFieldUpdater tableUpdater = AtomicReferenceFieldUpdater.newUpdater(IntIndexHashMap.class, Table.class, "table");
/**
* Construct a new instance.
*
* @param indexer the key indexer
* @param valueEqualler the value equaller
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
*/
public IntIndexHashMap(ToIntFunction super V> indexer, Equaller super V> valueEqualler, int initialCapacity, float loadFactor) {
if (valueEqualler == null) {
throw new IllegalArgumentException("valueEqualler is null");
}
this.indexer = indexer;
ve = valueEqualler;
if (initialCapacity < 0) {
throw new IllegalArgumentException("Initial capacity must be > 0");
}
if (initialCapacity > MAXIMUM_CAPACITY) {
initialCapacity = MAXIMUM_CAPACITY;
}
if (loadFactor <= 0.0 || Float.isNaN(loadFactor) || loadFactor >= 1.0) {
throw new IllegalArgumentException("Load factor must be between 0.0f and 1.0f");
}
int capacity = 1;
while (capacity < initialCapacity) {
capacity <<= 1;
}
this.loadFactor = loadFactor;
this.initialCapacity = capacity;
final Table table = new Table(capacity, loadFactor);
tableUpdater.set(this, table);
}
/**
* Construct a new instance.
*
* @param indexer the key indexer
* @param valueEqualler the value equaller
*/
public IntIndexHashMap(ToIntFunction super V> indexer, Equaller super V> valueEqualler) {
this(indexer, valueEqualler, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
/**
* Construct a new instance.
*
* @param indexer the key indexer
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
*/
public IntIndexHashMap(ToIntFunction super V> indexer, int initialCapacity, final float loadFactor) {
this(indexer, Equaller.DEFAULT, initialCapacity, loadFactor);
}
/**
* Construct a new instance.
*
* @param indexer the key indexer
* @param loadFactor the load factor
*/
public IntIndexHashMap(ToIntFunction super V> indexer, final float loadFactor) {
this(indexer, DEFAULT_INITIAL_CAPACITY, loadFactor);
}
/**
* Construct a new instance.
*
* @param indexer the key indexer
* @param initialCapacity the initial capacity
*/
public IntIndexHashMap(ToIntFunction super V> indexer, final int initialCapacity) {
this(indexer, initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* Construct a new instance.
*
* @param indexer the key indexer
*/
public IntIndexHashMap(ToIntFunction super V> indexer) {
this(indexer, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public V putIfAbsent(final V value) {
final V result = doPut(value, true, table);
return result == NONEXISTENT ? null : result;
}
public V computeIfAbsent(final int index, final IntFunction producer) {
final Table table = this.table;
V result = doGet(table, index);
if (result == NONEXISTENT) {
final V newVal = producer.apply(index);
result = doPut(newVal, true, table);
return result == NONEXISTENT ? newVal : result;
} else {
return result;
}
}
public V removeKey(final int index) {
final V result = doRemove(index, table);
return result == NONEXISTENT ? null : result;
}
@SuppressWarnings({ "unchecked" })
public boolean remove(final Object value) {
return doRemove((V) value, table);
}
public boolean containsKey(final int index) {
return doGet(table, index) != NONEXISTENT;
}
public V get(final int index) {
final V result = doGet(table, index);
return result == NONEXISTENT ? null : result;
}
public V put(final V value) {
final V result = doPut(value, false, table);
return result == NONEXISTENT ? null : result;
}
public V replace(final V value) {
final V result = doReplace(value, table);
return result == NONEXISTENT ? null : result;
}
public boolean replace(final V oldValue, final V newValue) {
if (indexer.applyAsInt(oldValue) != indexer.applyAsInt(newValue)) {
throw new IllegalArgumentException("Can only replace with value which has the same key");
}
return doReplace(oldValue, newValue, table);
}
public int applyAsInt(final V argument) {
return indexer.applyAsInt(argument);
}
public boolean add(final V v) {
return doPut(v, true, table) == NONEXISTENT;
}
@SuppressWarnings("unchecked")
public T[] toArray(final T[] a) {
final ArrayList list = new ArrayList(size());
for (final V item : this) {
list.add((T) item);
}
return list.toArray(a);
}
public Object[] toArray() {
final ArrayList