getClass(char c)从Java 6开始,似乎缺少Normalizer类的方法.
此方法存在于我们的遗留代码中,正在使用,如下所示.我们需要将它迁移到Java 6.有关如何替换它的任何建议吗?
import sun.text.Normalizer;
/**
* Returns an array of strings that have all the possible
* permutations of the characters in the input string.
* This is used to get a list of all possible orderings
* of a set of combining marks. Note that some of the permutations
* are invalid because of combining class collisions, and these
* possibilities must be removed because they are not canonically
* equivalent.
*/
private String[] producePermutations(String input) {
if (input.length() == 1)
return new String[] {input};
if (input.length() == 2) {
if (getClass(input.charAt(1)) ==
getClass(input.charAt(0))) {
return new String[] {input};
}
String[] result = new String[2];
result[0] = input;
StringBuffer sb = new StringBuffer(2);
sb.append(input.charAt(1));
sb.append(input.charAt(0));
result[1] = sb.toString();
return result;
}
int length = 1;
for(int x=1; x<input.length(); x++)
length = length * (x+1);
String[] temp = new String[length];
int combClass[] = new int[input.length()];
for(int x=0; x<input.length(); x++)
combClass[x] = getClass(input.charAt(x));
// For each char, take it out and add the permutations
// of the remaining chars
int index = 0;
loop: for(int x=0; x<input.length(); x++) {
boolean skip = false;
for(int y=x-1; y>=0; y--) {
if (combClass[y] == combClass[x]) {
continue loop;
}
}
StringBuffer sb = new StringBuffer(input);
String otherChars = sb.delete(x, x+1).toString();
String[] subResult = producePermutations(otherChars);
String prefix = input.substring(x, x+1);
for(int y=0; y<subResult.length; y++)
temp[index++] = prefix + subResult[y];
}
String[] result = new String[index];
for (int x=0; x<index; x++)
result[x] = temp[x];
return result;
}
private int getClass(char c) {
return Normalizer.getClass(c);
}
Run Code Online (Sandbox Code Playgroud)
标准化器 from与fromjava.text没有相同的功能Normalizersun.text
仅基于您输入的这段代码,执行您想要的操作的简单方法是使用ICU4J依赖项。如果你使用maven,像这样:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>4.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后,你可以编写一个这样的类:
package com.ibm.icu.text;
public class Normalizer {
public static final int getClass(final char ch) {
final int value = DecompData.canonClass.elementAt(ch);
return value >= 0 ? value : value + 256;
}
}
Run Code Online (Sandbox Code Playgroud)
由于DecompData具有包私有可见性,因此请Normalizer在应用程序中的同一包中创建。
| 归档时间: |
|
| 查看次数: |
408 次 |
| 最近记录: |