2015-01-14 6 views
7

NormalizerクラスのgetClass(char c)メソッドがJava 6以降ではないようです。JavaのNormalizer.getClass(c)メソッドの置き換え6

このメソッドは、従来のコードに存在し、以下のように使用されています。 Java 6に移行する必要があります。どのように置き換えることができますか? java.textから

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); 
} 
+0

あなたはsun.text.Normailerを書きましたが、javadocをjava.text.Normalizerからリンクしました。これは間違いですか、それとも偶然あなたの質問に答えましたか? –

+0

申し訳ありませんが、参照を削除しました。 – dazzle

+0

'sun'パッケージからのクラスは時間の経過とともに' java'にマージされます。 'CharSequence'は別の例です。 –

答えて

1

ノーマだけあなたが入力したコードのこの部分に基づいてsun.text

からNormalizerのない同じ機能を持っていない、あなたが欲しいものを行うための簡単な方法はICU4J依存関係を使用しています。

<dependency> 
    <groupId>com.ibm.icu</groupId> 
    <artifactId>icu4j</artifactId> 
    <version>4.6</version> 
</dependency> 

その後、あなたはこのようなクラスを書くことができます:あなたはこのような達人、使用している場合

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; 
    } 

} 

DecompDataがパッケージプライベート可視性を持っているので、あなたのアプリケーションで同じパッケージでNormalizerを作成します。

+0

これは従来のコードなので、依存関係を追加するのは難しいかもしれません。 – tbodt

1

あなたのコードスニペットは、他の人が指摘しているように、sun.text.Normalizerであり、java.text.Normalizerではありません。 Java 6では、sun.text.NormalizergetCombiningClass(int ch)というメソッドがあり、これはintであってもcharではなく、「指定された文字の結合クラスを返します」と記載されています。おそらくこれはあなたが探している方法です。

sun.*クラスのこれらの種類のメソッドは、予告なしにこれらの変更(名前の変更、消滅)の対象となり、自己責任で使用することに注意してください。注意書きコーダー!ジャワ6において

2

、方法はgetCharacterClassに改名し、そして置換のソートが65,535よりも大きい値でUnicode文字に対応するためにどこでも行われていたため、パラメータは、intcharから変更されました。

sunで始まるパッケージのメソッドは、最初に使用されたことはありません。これはおそらく、メソッドが削除された場合に再記述する必要がある場合に備えて、呼び出しが別のメソッドにある理由です。残念ながら、私はパブリックJava APIで同等のものを見つけることができないので、置き換えは最初から書かれていても、ドキュメント化されていなくてもかまいません。