1 /* 2 ******************************************************************************* 3 * Copyright (C) 1996-2012, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 package org.unicode.cldr.util.props; 8 9 import com.ibm.icu.impl.Utility; 10 import com.ibm.icu.text.UTF16; 11 12 public abstract class UnicodeLabel implements com.ibm.icu.text.Transform<Integer, String> { 13 getValue(int codepoint, boolean isShort)14 public abstract String getValue(int codepoint, boolean isShort); 15 16 @Override transform(Integer codepoint)17 public String transform(Integer codepoint) { 18 return getValue(codepoint, true); 19 } 20 getValue(String s, String separator, boolean withCodePoint)21 public String getValue(String s, String separator, boolean withCodePoint) { 22 if (s.length() == 1) { // optimize simple case 23 return getValue(s.charAt(0), withCodePoint); 24 } 25 StringBuffer sb = new StringBuffer(); 26 int cp; 27 for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) { 28 cp = UTF16.charAt(s, i); 29 if (i != 0) sb.append(separator); 30 sb.append(getValue(cp, withCodePoint)); 31 } 32 return sb.toString(); 33 } 34 getMaxWidth(boolean isShort)35 public int getMaxWidth(boolean isShort) { 36 return 0; 37 } 38 39 private static class Hex extends UnicodeLabel { 40 @Override getValue(int codepoint, boolean isShort)41 public String getValue(int codepoint, boolean isShort) { 42 if (isShort) return Utility.hex(codepoint, 4); 43 return "U+" + Utility.hex(codepoint, 4); 44 } 45 } 46 47 public static class Constant extends UnicodeLabel { 48 private String value; 49 Constant(String value)50 public Constant(String value) { 51 if (value == null) value = ""; 52 this.value = value; 53 } 54 55 @Override getValue(int codepoint, boolean isShort)56 public String getValue(int codepoint, boolean isShort) { 57 return value; 58 } 59 60 @Override getMaxWidth(boolean isShort)61 public int getMaxWidth(boolean isShort) { 62 return value.length(); 63 } 64 } 65 66 public static final UnicodeLabel NULL = new Constant(""); 67 public static final UnicodeLabel HEX = new Hex(); 68 } 69