]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/translit/src/com/ibm/icu/dev/test/util/UnicodeLabel.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / translit / src / com / ibm / icu / dev / test / util / UnicodeLabel.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2004, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.util;\r
8 \r
9 import com.ibm.icu.impl.Utility;\r
10 import com.ibm.icu.text.UTF16;\r
11 \r
12 public abstract class UnicodeLabel {\r
13     \r
14     public abstract String getValue(int codepoint, boolean isShort);\r
15     \r
16     public String getValue(String s, String separator, boolean withCodePoint) {\r
17         if (s.length() == 1) { // optimize simple case\r
18             return getValue(s.charAt(0), withCodePoint); \r
19         }\r
20         StringBuffer sb = new StringBuffer();\r
21         int cp;\r
22         for (int i = 0; i < s.length(); i+=UTF16.getCharCount(cp)) {\r
23             cp = UTF16.charAt(s,i);\r
24             if (i != 0) sb.append(separator);\r
25             sb.append(getValue(cp, withCodePoint));\r
26         }\r
27         return sb.toString();\r
28     }\r
29     \r
30     public int getMaxWidth(boolean isShort) {\r
31         return 0;\r
32     }\r
33     \r
34     private static class Hex extends UnicodeLabel {\r
35         public String getValue(int codepoint, boolean isShort) {\r
36             if (isShort) return Utility.hex(codepoint,4);\r
37             return "U+" + Utility.hex(codepoint,4);\r
38         }       \r
39     }\r
40     \r
41     public static class Constant extends UnicodeLabel {\r
42         private String value;\r
43         public Constant(String value) {\r
44             if (value == null) value = "";\r
45             this.value = value;\r
46         }\r
47         public String getValue(int codepoint, boolean isShort) {\r
48             return value;\r
49         }\r
50         public int getMaxWidth(boolean isShort) {\r
51             return value.length();      \r
52         }\r
53     }\r
54     public static final UnicodeLabel NULL = new Constant("");\r
55     public static final UnicodeLabel HEX = new Hex();\r
56 }