/*
 * 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 java.nio.charset.StandardCharsets;

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 US-ASCII = Charset.forName("US-ASCII");

    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()), US-ASCII);
    }

    @Override
    public T decode(String value) throws TypeDecodingException {
        checkNotNull(value, "Null values are not allowed");
        String reversed = new String(reverse(value.getBytes(US-ASCII)));
        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;
    }
}

@@ -869,50 +869,8 @@
 set;
-%0Aimport java.nio.charset.StandardCharsets;
 %0A%0Aim
@@ -1180,24 +1180,26 @@
 Charset 
-US-ASCII
+ISO_8859_1
  = Chars
@@ -1210,24 +1210,26 @@
 orName(%22
-US-ASCII
+ISO-8859-1
 %22);%0A%0A   
@@ -1733,24 +1733,26 @@
 tes()), 
-US-ASCII
+ISO_8859_1
 );%0A    %7D
@@ -1956,16 +1956,18 @@
 tes(
-US-ASCII
+ISO_8859_1
 )));
@@ -2195,17 +2195,17 @@
 yte) (0x
-8
+f
 f - (0xf

Change reverseEncoder to ISO_8859_1 to take advantage of the full 8 bits of the character encoding