// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.util;

import java.util.HashMap;
import java.util.Map;
import org.bouncycastle.crypto.AlphabetMapper;

public class BasicAlphabetMapper implements AlphabetMapper
{
    private Map<Character, Integer> indexMap;
    private Map<Integer, Character> charMap;
    
    public BasicAlphabetMapper(final String s) {
        this(s.toCharArray());
    }
    
    public BasicAlphabetMapper(final char[] array) {
        this.indexMap = new HashMap<Character, Integer>();
        this.charMap = new HashMap<Integer, Character>();
        for (int i = 0; i != array.length; ++i) {
            if (this.indexMap.containsKey(array[i])) {
                throw new IllegalArgumentException("duplicate key detected in alphabet: " + array[i]);
            }
            this.indexMap.put(array[i], i);
            this.charMap.put(i, array[i]);
        }
    }
    
    @Override
    public int getRadix() {
        return this.indexMap.size();
    }
    
    @Override
    public byte[] convertToIndexes(final char[] array) {
        byte[] array2;
        if (this.indexMap.size() <= 256) {
            array2 = new byte[array.length];
            for (int i = 0; i != array.length; ++i) {
                array2[i] = this.indexMap.get(array[i]).byteValue();
            }
        }
        else {
            array2 = new byte[array.length * 2];
            for (int j = 0; j != array.length; ++j) {
                final int intValue = this.indexMap.get(array[j]);
                array2[j * 2] = (byte)(intValue >> 8 & 0xFF);
                array2[j * 2 + 1] = (byte)(intValue & 0xFF);
            }
        }
        return array2;
    }
    
    @Override
    public char[] convertToChars(final byte[] array) {
        char[] array2;
        if (this.charMap.size() <= 256) {
            array2 = new char[array.length];
            for (int i = 0; i != array.length; ++i) {
                array2[i] = this.charMap.get(array[i] & 0xFF);
            }
        }
        else {
            if ((array.length & 0x1) != 0x0) {
                throw new IllegalArgumentException("two byte radix and input string odd length");
            }
            array2 = new char[array.length / 2];
            for (int j = 0; j != array.length; j += 2) {
                array2[j / 2] = this.charMap.get((array[j] << 8 & 0xFF00) | (array[j + 1] & 0xFF));
            }
        }
        return array2;
    }
}
