/*
 * Copyright (C) 2013 The Calrissian Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.calrissian.mango.types.encoders.lexi;


import org.calrissian.mango.types.TypeEncoder;
import org.calrissian.mango.types.exception.TypeDecodingException;
import org.calrissian.mango.types.exception.TypeEncodingException;

import java.nio.charset.Charset;

import static com.google.common.base.Preconditions.checkNotNull;

public class ReverseEncoder<T> implements TypeEncoder<T, String> {
    private static final long serialVersionUID = 1L;

    //This is defined in Java 7 under StandardCharsets and should be replaced in the future.
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    private final TypeEncoder<T, String> encoder;

    public ReverseEncoder(TypeEncoder<T, String> encoder) {
        this.encoder = encoder;
    }

    @Override
    public String getAlias() {
        return encoder.getAlias();
    }

    @Override
    public Class<T> resolves() {
        return encoder.resolves();
    }

    @Override
    public String encode(T value) throws TypeEncodingException {
        String encoded = encoder.encode(value);
        return new String(reverse(encoded.getBytes()), UTF_8);
    }

    @Override
    public T decode(String value) throws TypeDecodingException {
        checkNotNull(value, "Null values are not allowed");
        String reversed = new String(reverse(value.getBytes(UTF_8)));
        return encoder.decode(reversed);
    }

    private static byte[] reverse(byte[] bytes) {
        byte[] result = new byte[bytes.length];

        for (int i = 0; i < bytes.length; i++)
            result[i] = (byte) (0x8f - (0xff & bytes[i]));

        return result;
    }
}

@@ -865,16 +865,58 @@
 Charset;
+%0Aimport java.nio.charset.StandardCharsets;
 %0A%0Aimport
@@ -1223,20 +1223,23 @@
 harset U
-TF_8
+S-ASCII
  = Chars
@@ -1255,12 +1255,15 @@
 e(%22U
-TF-8
+S-ASCII
 %22);%0A
@@ -1772,20 +1772,23 @@
 es()), U
-TF_8
+S-ASCII
 );%0A    %7D
@@ -1993,12 +1993,15 @@
 es(U
-TF_8
+S-ASCII
 )));

Change reverseEncoder to US_ASCII for now as the reverse function does not correctly handle all UTF-8 codepoints, just the ascii range.