class GenerationSafePointerPair {

    public static long read( PageCursor cursor, long stableGeneration, long unstableGeneration, GBPTreeGenerationTarget generationTarget )
    {
        // Try A
        long generationA = readGeneration( cursor );
        long pointerA = readPointer( cursor );
        short readChecksumA = readChecksum( cursor );
        short checksumA = checksumOf( generationA, pointerA );
        boolean correctChecksumA = readChecksumA == checksumA;

        // Try B
        long generationB = readGeneration( cursor );
        long pointerB = readPointer( cursor );
        short readChecksumB = readChecksum( cursor );
        short checksumB = checksumOf( generationB, pointerB );
        boolean correctChecksumB = readChecksumB == checksumB;

        byte pointerStateA = pointerState( stableGeneration, unstableGeneration, generationA, pointerA, correctChecksumA );
        byte pointerStateB = pointerState( stableGeneration, unstableGeneration, generationB, pointerB, correctChecksumB );

        if ( pointerStateA == UNSTABLE )
        {
            if ( pointerStateB == STABLE || pointerStateB == EMPTY )
            {
                return buildSuccessfulReadResult( FLAG_SLOT_A, generationA, pointerA, generationTarget );
            }
        }
        else if ( pointerStateB == UNSTABLE )
        {
            if ( pointerStateA == STABLE || pointerStateA == EMPTY )
            {
                return buildSuccessfulReadResult( FLAG_SLOT_B, generationB, pointerB, generationTarget );
            }
        }
        else if ( pointerStateA == STABLE && pointerStateB == STABLE )
        {
            // compare generation
            if ( generationA > generationB )
            {
                return buildSuccessfulReadResult( FLAG_SLOT_A, generationA, pointerA, generationTarget );
            }
            else if ( generationB > generationA )
            {
                return buildSuccessfulReadResult( FLAG_SLOT_B, generationB, pointerB, generationTarget );
            }
        }
        else if ( pointerStateA == STABLE )
        {
            return buildSuccessfulReadResult( FLAG_SLOT_A, generationA, pointerA, generationTarget );
        }
        else if ( pointerStateB == STABLE )
        {
            return buildSuccessfulReadResult( FLAG_SLOT_B, generationB, pointerB, generationTarget );
        }

        generationTarget.accept( EMPTY_GENERATION );
        return FLAG_FAIL | FLAG_READ | generationState( generationA, generationB ) |
               ((long) pointerStateA) << SHIFT_STATE_A | ((long) pointerStateB) << SHIFT_STATE_B;
    }

    private  GenerationSafePointerPair();

}