gpt4 book ai didi

java - Java 6 中 Normalizer.getClass(c) 方法的替换

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:51:40 26 4
gpt4 key购买 nike

从 Java 6 开始,Normalizer 类中的 getClass(char c) 方法似乎缺失了。

此方法存在于我们的遗留代码中,其使用方式如下所示。我们需要将它迁移到 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);
}

最佳答案

正如其他人所指出的,您的代码片段是 sun.text.Normalizer 而不是 java.text.Normalizer。在 Java 6 中,我看到 sun.text.Normalizer 有一个名为 getCombiningClass(int ch) 的方法,它被描述为“返回给定字符的组合类”,尽管采取了int 而不是 char。这可能是您正在寻找的方法。

我应该注意,作为一个 sun.* 类,这些类型的方法会在没有通知的情况下发生这些类型的更改(重命名、消失),您使用它们需要您自担风险.警告编码员!

关于java - Java 6 中 Normalizer.getClass(c) 方法的替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27938926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com