KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > math > BaseNCoder


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on May 17, 2005
33  */

34 package com.nightlabs.math;
35
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * @author Marco Schulze - marco at nightlabs dot de
41  */

42 public class BaseNCoder
43 {
44     private int base;
45     private char[] symbols = null;
46     private Map JavaDoc symbolMap = new HashMap JavaDoc();
47
48     protected BaseNCoder()
49     {
50     }
51
52     /**
53      * @param digitSymbols The list of symbols used to express the digits.
54      * Each <tt>char</tt> represents one digit with the value represented
55      * by the array index.
56      */

57     public BaseNCoder(char[] digitSymbols)
58     {
59         init(digitSymbols);
60     }
61
62     /**
63      * @param digitSymbols The list of symbols used to express the digits.
64      * Each <tt>char</tt> represents one digit with the value represented
65      * by the array index.
66      */

67     protected void init(char[] digitSymbols)
68     {
69         this.symbols = digitSymbols;
70         base = digitSymbols.length;
71         symbolMap.clear();
72         for (int i = 0; i < digitSymbols.length; ++i) {
73             char c = digitSymbols[i];
74             symbolMap.put(new Character JavaDoc(c), new Integer JavaDoc(i));
75         }
76     }
77
78     public String JavaDoc encode(long number, int minDigitCount)
79     {
80         if (symbols == null)
81             throw new IllegalStateException JavaDoc("symbols undefined! Call init(...) first!");
82
83         boolean numberStarted = false;
84         int digitCount = number < base ? 1 : 1 + (int)MathUtil.log(base, number);
85         long divisor;
86         int digitValue;
87         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
88         for (int digitIdx = digitCount - 1; digitIdx >= 0; --digitIdx) {
89             divisor = (long)Math.pow(base, digitIdx);
90             digitValue = (int)(number / divisor);
91             if (numberStarted || digitIdx < minDigitCount || digitValue > 0) {
92                 numberStarted = true;
93                 number = number - (long)(digitValue * divisor);
94                 res.append(symbols[digitValue]);
95             }
96         }
97         return res.toString();
98     }
99
100     public long decode(String JavaDoc s)
101     {
102         if (symbols == null)
103             throw new IllegalStateException JavaDoc("symbols undefined! Call init(...) first!");
104
105         int digitIdx = 0;
106         long res = 0;
107         for (int i = s.length() - 1; i >= 0; --i) {
108             char c = s.charAt(i);
109             Integer JavaDoc digitValue = (Integer JavaDoc) symbolMap.get(new Character JavaDoc(c));
110             if (digitValue == null)
111                 throw new NumberFormatException JavaDoc("Unknown digit: "+c);
112             
113             res = res + ((long)digitValue.intValue()) * (long)Math.pow(base, digitIdx);
114             ++digitIdx;
115         }
116         return res;
117     }
118
119     /**
120      * @return Returns the base.
121      */

122     public int getBase()
123     {
124         return base;
125     }
126 }
127
Popular Tags