01: /*
02: *******************************************************************************
03: * Copyright (C) 1996-2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */
07: package com.ibm.icu.dev.test.util;
08:
09: import com.ibm.icu.impl.Utility;
10: import com.ibm.icu.text.UTF16;
11:
12: public abstract class UnicodeLabel {
13:
14: public abstract String getValue(int codepoint, boolean isShort);
15:
16: public String getValue(String s, String separator,
17: boolean withCodePoint) {
18: if (s.length() == 1) { // optimize simple case
19: return getValue(s.charAt(0), withCodePoint);
20: }
21: StringBuffer sb = new StringBuffer();
22: int cp;
23: for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
24: cp = UTF16.charAt(s, i);
25: if (i != 0)
26: sb.append(separator);
27: sb.append(getValue(cp, withCodePoint));
28: }
29: return sb.toString();
30: }
31:
32: public int getMaxWidth(boolean isShort) {
33: return 0;
34: }
35:
36: private static class Hex extends UnicodeLabel {
37: public String getValue(int codepoint, boolean isShort) {
38: if (isShort)
39: return Utility.hex(codepoint, 4);
40: return "U+" + Utility.hex(codepoint, 4);
41: }
42: }
43:
44: public static class Constant extends UnicodeLabel {
45: private String value;
46:
47: public Constant(String value) {
48: if (value == null)
49: value = "";
50: this .value = value;
51: }
52:
53: public String getValue(int codepoint, boolean isShort) {
54: return value;
55: }
56:
57: public int getMaxWidth(boolean isShort) {
58: return value.length();
59: }
60: }
61:
62: public static final UnicodeLabel NULL = new Constant("");
63: public static final UnicodeLabel HEX = new Hex();
64: }
|