summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/ioutils/format/RadixDirective.java
blob: 37425824def522201ad12043f2220ec971e07fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package bjc.utils.ioutils.format;

import bjc.utils.esodata.Tape;
import bjc.utils.math.NumberUtils;

import java.util.IllegalFormatConversionException;
import java.util.regex.Matcher;

class RadixDirective extends GeneralNumberDirective {

	@Override
	public void format(StringBuffer buff, Object arg, CLModifiers mods, CLParameters params, Tape<Object> tParams,
			Matcher dirMatcher, CLFormatter fmt) {
		CLFormatter.checkItem(arg, 'R');
		
		if(!(arg instanceof Number)) {
			throw new IllegalFormatConversionException('R', arg.getClass());
		}

		/*
		 * @TODO see if this is the way we want to do this.
		 */
		long val = ((Number) arg).longValue();

		if(params.length() == 0) {
			if(mods.atMod) {
				buff.append(NumberUtils.toRoman(val, mods.colonMod));
			} else if(mods.colonMod) {
				buff.append(NumberUtils.toOrdinal(val));
			} else {
				buff.append(NumberUtils.toCardinal(val));
			}
		} else {
			if(params.length() < 1) throw new IllegalArgumentException(
					"R directive requires at least one parameter, the radix");

			int radix = params.getInt(0, "radix", 'R');

			handleNumberDirective(buff, mods, params, 0, val, radix);
		}
		
		tParams.right();
	}
}