/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.nanopool.cas;

import java.lang.reflect.Field;
import net.nanopool.Connector;
import sun.misc.Unsafe;

/**
 *
 * @author vest
 */
public class UnsafeCasArray implements CasArray<Connector> {
    private final Unsafe theUnsafe;
    private final int offset;
    private final int scale;
    private final Connector[] array;
    
    public UnsafeCasArray(int poolSize) {
        if (poolSize == 0)
            throw new IllegalArgumentException(
                    "Pool size must be greater than 0.");
        try {
            array = new Connector[poolSize];
            Class<Unsafe> unsafeClass = Unsafe.class;
            Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
            unsafeField.setAccessible(true);
            theUnsafe = (Unsafe)unsafeField.get(unsafeClass);
            offset = theUnsafe.arrayBaseOffset(Object[].class);
            scale = theUnsafe.arrayIndexScale(Object[].class);
            /*
             * Ensure JMM visibility of 'array'.
             * Do we *really* need this? AFAIK setting a final field
             * happens-before the ctor returns, and would this not be
             * anough to ensure visibility of 'array'? Cliff Click of
             * high-scale-lib thinks so in his NonBlockingHashMap impl.
             * but Doug Lea seems to disagree in his AtomicReferenceArray.
             * What gives??
             */
            theUnsafe.putObjectVolatile(array, idx(0), null);
        } catch (Exception ex) {
            throw new UnsupportedOperationException(
                    "UnsafeCasArray probably not supported on your JVM.", ex);
        }
    }
    
    private final int idx(int i) {
        return offset + i * scale;
    }

    public boolean cas(int idx, Connector newValue, Connector oldValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Connector get(int idx) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int length() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public static void main(String[] args) {
        UnsafeCasArray uca = new UnsafeCasArray(5);
        System.out.println("array base offset: " + uca.theUnsafe.arrayBaseOffset(Object[].class));
        System.out.println("array index scale: " + uca.theUnsafe.arrayIndexScale(Object[].class));
    }
}

@@ -157,39 +157,9 @@
 ld;%0A
-import net.nanopool.Connector;
 %0A
+
 impo
@@ -233,16 +233,19 @@
 CasArray
+%3CT%3E
  impleme
@@ -257,25 +257,17 @@
 asArray%3C
-Connector
+T
 %3E %7B%0A    
@@ -375,25 +375,17 @@
 e final 
-Connector
+T
 %5B%5D array
@@ -604,21 +604,24 @@
 y = 
-new Conn
+(T%5B%5D) new Obj
 ect
-or
 %5Bpoo
@@ -1812,25 +1812,17 @@
 nt idx, 
-Connector
+T
  newValu
@@ -1824,25 +1824,17 @@
 wValue, 
-Connector
+T
  oldValu
@@ -1931,17 +1931,9 @@
 lic 
-Connector
+T
  get

make UnsafeCasArray generic as it should be