class GenerationSafePointerPair {

    public static long write( PageCursor cursor, long pointer, long stableGeneration, long unstableGeneration )
    {
        // Later there will be a selection which "slot" of GSP out of the two to write into.
        int offset = cursor.getOffset();
        pointer = pointer( pointer );

        // 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 );

        long writeResult = writeResult( pointerStateA, pointerStateB, generationA, generationB );

        if ( isSuccess( writeResult ) )
        {
            boolean writeToA = ( writeResult & SLOT_MASK) == FLAG_SLOT_A;
            int writeOffset = writeToA ? offset : offset + GenerationSafePointer.SIZE;
            cursor.setOffset( writeOffset );
            GenerationSafePointer.write( cursor, unstableGeneration, pointer );
        }
        return writeResult;
    }

    private  GenerationSafePointerPair();

}