package com.salesforce.phoenix.schema;

import com.google.common.base.Preconditions;
import com.sun.istack.NotNull;
import com.sun.istack.Nullable;

public enum ColumnModifier {
	
	SORT_DESC() {
        @Override
		public byte[] apply(byte[] src, byte[] dest, int offset, int length) {
            Preconditions.checkNotNull(src);            
            if (dest == null) {
                dest = new byte[src.length];
            }
            int maxIndex = length + offset;
			for (int i = 0; i < src.length; i++) {
				if (i >= offset && i < maxIndex) {
					dest[i] = (byte)(src[i] ^ 0xFF);
				} else {
					dest[i] = src[i];
				}
			}			
			return dest;
		}
	};
	
	public static ColumnModifier fromDDLValue(String modifier) {
		if (modifier == null) {
			return null;
		} else if (modifier.equalsIgnoreCase("ASC")) {
			return null;
		} else if (modifier.equalsIgnoreCase("DESC")) {
			return SORT_DESC;
		} else {
			return null;
		}			
	}
	
	public static ColumnModifier fromSystemValue(int value) {
		switch (value) {
		    case 1: return SORT_DESC;
		    default: return null;
		}
	}
	
	public static int toSystemValue(ColumnModifier columnModifier) {
		if (columnModifier == null) {
			return Integer.MIN_VALUE;
		}
		switch (columnModifier) {
		    case SORT_DESC: return 1;
		    default: return Integer.MIN_VALUE;
		}
	}
	
	/**
	 * Copy the bytes from the src array to the dest array and apply the column modifier operation on the bytes
	 * starting at the specified offset index.  The column modifier is applied to the number of bytes matching the 
	 * specified length.  If dest is null, a new byte array is allocated.
	 * 
	 * @param src    the src byte array to copy from, cannot be null
	 * @param dest   the byte array to copy into, if it is null, a new byte array with the same lenght as src is allocated
	 * @param offset start applying the column modifier from this index
	 * @param length apply the column modifier for this many bytes 
	 * @return       dest or a new byte array if dest is null
	 */
	public abstract byte[] apply(@NotNull byte[] src, @Nullable byte[] dest, int offset, int length);
}
@@ -1,183 +1,2422 @@
-package com.salesforce.phoenix.schema;%0A%0Aimport com.google.common.base.Preconditions;%0Aimport com.sun.istack.NotNull;%0Aimport com.sun.istack.Nullable;%0A%0Apublic enum ColumnModifier %7B%0A%09
+/*******************************************************************************%0A * Copyright (c) 2013, Salesforce.com, Inc.%0A * All rights reserved.%0A * %0A * Redistribution and use in source and binary forms, with or without%0A * modification, are permitted provided that the following conditions are met:%0A * %0A *     Redistributions of source code must retain the above copyright notice,%0A *     this list of conditions and the following disclaimer.%0A *     Redistributions in binary form must reproduce the above copyright notice,%0A *     this list of conditions and the following disclaimer in the documentation%0A *     and/or other materials provided with the distribution.%0A *     Neither the name of Salesforce.com nor the names of its contributors may %0A *     be used to endorse or promote products derived from this software without %0A *     specific prior written permission.%0A * %0A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22 AND%0A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED%0A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE%0A * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE %0A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL %0A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR %0A * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER %0A * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, %0A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE %0A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%0A ******************************************************************************/%0Apackage com.salesforce.phoenix.schema;%0A%0Aimport com.google.common.base.Preconditions;%0Aimport com.sun.istack.NotNull;%0Aimport com.sun.istack.Nullable;%0A%0A/**%0A * A ColumnModifier implementation modifies how bytes are stored in a primary key column.%3C/p%3E  %0A * The %7B@link ColumnModifier#apply apply%7D method is called when the bytes for a specific column are first written to HBase and again%0A * when they are read back.  Phoenix attemps to minimize calls to apply when bytes are read out of HBase.   %0A * %0A * @author stoens%0A * @since 1.2%0A */%0Apublic enum ColumnModifier %7B%0A%09%0A    /**%0A     * Invert the bytes in the src byte array to support descending ordering of row keys.%0A     */
 %0A%09SO
@@ -3601,20 +3601,16 @@
 es from 
-the 
 src arra
@@ -3614,20 +3614,16 @@
 rray to 
-the 
 dest arr

Update comments.