package org.hibernate.service.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ConcurrentServiceBinding<K,V> {
@SuppressWarnings({ "unchecked", "rawtypes" })
private static final Node EMPTY_LEAF = new Node( new Entry( 0, null, null ), null, null );
@SuppressWarnings("unchecked")
private volatile Node<K,V> treeRoot = EMPTY_LEAF;
@SuppressWarnings("unchecked")
public synchronized void clear() {
treeRoot = EMPTY_LEAF;
}
public synchronized void put(final K key, final V value) {
final int code = hashKey( key );
final Entry<K,V> newEntry = new Entry<K, V>( code, key, value );
final ArrayList<Entry<K, V>> list = convertToArrayList( treeRoot, key );
list.add( newEntry );
Collections.sort( list );
final int size = list.size();
@SuppressWarnings("unchecked")
Entry<K, V>[] array = list.toArray( new Entry[size] );
treeRoot = treeFromRange( array, 0, size );
}
private Node<K, V> treeFromRange(final Entry<K, V>[] array, final int minInclusive, final int maxExclusive) {
if ( minInclusive == maxExclusive ) {
return null;
}
int mid = ( minInclusive + maxExclusive ) / 2;
while ( mid > minInclusive && array[mid].hash == array[mid-1].hash ) {
mid--;
}
return new Node( array[mid], treeFromRange( array, minInclusive, mid ), treeFromRange( array, mid + 1, maxExclusive ) );
}
public V get(final K key) {
final int hash = hashKey( key );
final Node<K,V> root = treeRoot;
return root.get( key, hash );
}
protected int hashKey(final K key) {
return System.identityHashCode( key );
}
public Iterable<V> values() {
@SuppressWarnings("rawtypes")
ArrayList<V> list = new ArrayList();
treeRoot.collectAllValuesInto( list );
return list;
}
private final ArrayList<Entry<K, V>> convertToArrayList(final Node<K, V> treeRoot, K exceptKey) {
@SuppressWarnings("rawtypes")
ArrayList<Entry<K, V>> list = new ArrayList();
if ( treeRoot != EMPTY_LEAF ) {
treeRoot.collectAllEntriesInto( list, exceptKey );
}
return list;
}
private static final class Entry<K,V> implements Comparable<Entry<K,V>> {
private final int hash;
private final K key;
private final V value;
Entry(int keyHashCode, K key, V value) {
this.hash = keyHashCode;
this.key = key;
this.value = value;
}
@Override
public int compareTo(Entry o) {
return ( hash < o.hash ) ? -1 : ( (hash == o.hash) ? 0 : 1 );
}
@Override
public boolean equals(Object obj) {
final Entry<K,V> other = (Entry<K,V>)obj;
return other.key == this.key;
}
@Override
public String toString() {
return "<" + key + ", " + value + ">";
}
}
private static final class Node<K,V> {
private final Entry<K,V> entry;
private final Node<K, V> left;
private final Node<K, V> right;
Node(Entry<K,V> entry, Node<K,V> left, Node<K,V> right) {
this.entry = entry;
this.left = left;
this.right = right;
}
public V get(final K key, final int hash) {
if ( entry.key == key ) {
return entry.value;
}
else if ( hash < this.entry.hash ) {
return left == null ? null : left.get( key, hash );
}
else {
return right == null ? null : right.get( key, hash );
}
}
public void collectAllEntriesInto(final List<Entry<K,V>> list, final K exceptKey) {
if ( entry != null && exceptKey != entry.key ) {
list.add( entry );
}
if ( left != null ) {
left.collectAllEntriesInto( list, exceptKey );
}
if ( right != null ) {
right.collectAllEntriesInto( list, exceptKey );
}
}
public void collectAllValuesInto(final List<V> list) {
if ( entry != null && entry.value != null ) {
list.add( entry.value );
}
if ( left != null ) {
left.collectAllValuesInto( list );
}
if ( right != null ) {
right.collectAllValuesInto( list );
}
}
private void renderToStringBuilder(final StringBuilder sb, final int indent) {
sb.append( entry );
appendIndented( sb, indent, "L-> ", left );
appendIndented( sb, indent, "R-> ", right );
}
private void appendIndented(final StringBuilder sb, final int indent, final String label, Node<K, V> node) {
if ( node == null ) {
return;
}
sb.append( "\n" );
for ( int i = 0; i < indent; i++ ) {
sb.append( "\t" );
}
sb.append( label );
node.renderToStringBuilder( sb, indent + 1 );
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
renderToStringBuilder( sb, 0 );
return sb.toString();
}
}
}