| 1 | /* |
| 2 | * This file is part of JON. |
| 3 | * |
| 4 | * JON is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * JON is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright 2007 Michael Doberenz |
| 18 | */ |
| 19 | package org.fuwjax.util; |
| 20 | |
| 21 | /** |
| 22 | * A conversion tool for arbitrary based numbers. Given an in-order set of |
| 23 | * digits, BaseConverter will map strings to ints and vice versa. Mapping to a |
| 24 | * string can optionally prepend the zero digit to pad to a specified length. |
| 25 | * The base of the conversion is implicitly defined by the length of the string |
| 26 | * specifying the digits. |
| 27 | * @author michaeldoberenz |
| 28 | */ |
| 29 | public class BaseConverter{ |
| 30 | private int[] map; |
| 31 | private char[] digits; |
| 32 | private int base; |
| 33 | private char zero; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new converter backed by the specified in-order set of digits. |
| 37 | * @param digits the in-order set of digits. |
| 38 | * @throws IllegalArgumentException if <code>digits</code> is not at least |
| 39 | * 2 characters long with no repeats. |
| 40 | * @throws NullPointerException if <code>digits</code> is null. |
| 41 | */ |
| 42 | public BaseConverter(final String digits) throws IllegalArgumentException, NullPointerException{ |
| 43 | base = digits.length(); |
| 44 | if(base <= 1){ |
| 45 | throw new IllegalArgumentException(); |
| 46 | } |
| 47 | this.digits = digits.toCharArray(); |
| 48 | this.map = new int[Byte.MAX_VALUE]; |
| 49 | for(int i = 0; i < map.length; ++i){ |
| 50 | map[i] = -1; |
| 51 | } |
| 52 | buildMap(digits); |
| 53 | zero = this.digits[0]; |
| 54 | } |
| 55 | |
| 56 | private void buildMap(final CharSequence digitList){ |
| 57 | char c; |
| 58 | for(int i = 0; i < base; ++i){ |
| 59 | c = digitList.charAt(i); |
| 60 | if(map[c] != -1){ |
| 61 | throw new IllegalArgumentException(); |
| 62 | } |
| 63 | map[c] = i; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Maps <code>num</code> to a string. Equivalent to calling |
| 69 | * toCharSequence(num,1). |
| 70 | * @param num the int to map to a string |
| 71 | * @return the string equivalent to this int. |
| 72 | * @throws IllegalArgumentException if <code>num</code> is negative. |
| 73 | */ |
| 74 | public CharSequence toCharSequence(final int num) throws IllegalArgumentException{ |
| 75 | return toCharSequence(num, 1); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Maps <code>num</code> to a padded string of length <code>length</code>. |
| 80 | * The length of the returned string will always be at least one character; |
| 81 | * any value of <code>length</code> less than 1 will be ignored. |
| 82 | * @param num the int to map to a string. |
| 83 | * @param length the length of the mapped string. |
| 84 | * @return the string equivalent to this int. |
| 85 | * @throws IllegalArgumentException if <code>num</code> is negative. |
| 86 | */ |
| 87 | public CharSequence toCharSequence(final int num, final int length) throws IllegalArgumentException{ |
| 88 | assertPositive(num); |
| 89 | int rem = num; |
| 90 | final StringBuilder builder = new StringBuilder(); |
| 91 | while(rem > 0){ |
| 92 | builder.append(digits[rem % base]); |
| 93 | rem = rem / base; |
| 94 | } |
| 95 | while(builder.length() < length){ |
| 96 | builder.append(zero); |
| 97 | } |
| 98 | return builder.reverse(); |
| 99 | } |
| 100 | |
| 101 | private static void assertPositive(final int num){ |
| 102 | if(num < 0){ |
| 103 | throw new IllegalArgumentException(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Maps <code>num</code> to an int. Note that this method is not required |
| 109 | * to check for overflow, so <code>toString( toInt( longString ) )</code> |
| 110 | * may not return <code>longString</code>. |
| 111 | * @param num the string to map to an int. |
| 112 | * @return the int equivalent to this string. |
| 113 | * @throws IllegalArgumentException if <code>num</code> contains characters |
| 114 | * which are not digits. |
| 115 | * @throws NullPointerException if <code>num</code> is null |
| 116 | */ |
| 117 | public int toInt(final CharSequence num) throws IllegalArgumentException, NullPointerException{ |
| 118 | int res = 0; |
| 119 | int val; |
| 120 | for(int i = 0; i < num.length(); ++i){ |
| 121 | val = map[num.charAt(i)]; |
| 122 | if(val == -1){ |
| 123 | throw new IllegalArgumentException(); |
| 124 | } |
| 125 | res = res * base + val; |
| 126 | } |
| 127 | return res; |
| 128 | } |
| 129 | } |