1. 程式人生 > >獲得字串的拼音頭和全拼的寫法

獲得字串的拼音頭和全拼的寫法

public class SpellCache implements java.io.Serializable
{
private static final long serialVersionUID = -8208755962314651055L;
private static SpellCache obj_SpellCache;

static ArrayList<Spell> arr_Spell = null;

/**
* 例項初始化
*/
private SpellCache()
{
try
{
if(obj_SpellCache==null)
init();
}
catch(Exception e)
{
e.printStackTrace();
obj_SpellCache = null;
}
}

/**
* 獲得拼音快取的例項
* @return 拼音快取的例項
*/
public static synchronized SpellCache getInstance()
{
if(obj_SpellCache == null)
{
obj_SpellCache = new SpellCache();
}

return obj_SpellCache;
}

/**
* 拼音快取內容的初始化
*/
private void init() throws Exception
{
DBConnection dbc = new DBConnection();
ResultSet rst = null;
try
{
/// 查詢出拼音內容
String str_SQL = Common.SELECT + Field.WORD + Common.COMMA +
Field.SPELL + Common.COMMA +
Field.ASPELL +
Common.S_FROM + Table.SPELL +
Common.S_ORDER + Field.WORD;

/// 查詢
rst = dbc.excuteQuery(str_SQL);

/// 初始化 arr_Spell
arr_Spell = new ArrayList<Spell>();

while(rst.next())
{
char chr_Word = rst.getString(Field.WORD).charAt(0);
char chr_Spell = rst.getString(Field.SPELL).charAt(0);
String str_ASpell = rst.getString(Field.ASPELL);

arr_Spell.add(new Spell(chr_Word, chr_Spell, str_ASpell));
}

rst.close();

System.out.println("快取中已載入" + arr_Spell.size() + "個拼音");
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
rst.close();
dbc.freeConnection();
}
}

/**
* 獲得拼音的總數
* @return int 拼音的總數
*/
public synchronized int getCount()
{
return arr_Spell.size();
}

/**
* 獲得單個拼音物件
* @param chrWord 字元
* @return Spell 單個拼音快取物件
*/
public Spell getObject(char chrWord)
{
Iterator it = arr_Spell.iterator();

while(it.hasNext())
{
Spell obj_Spell = (Spell)it.next();

if (obj_Spell.equals(chrWord)) return obj_Spell;
}
return null;
}

/**
* 獲得字元的拼音頭
* @param chrWord 字元
* @return String 拼音頭
*/
public String getSpell(char chrWord)
{
Spell obj_Spell = getObject(chrWord);

if(obj_Spell==null) return null;

return String.valueOf(obj_Spell.getSpell());
}

/**
* 獲得字元的全拼
* @param chrWord 字元
* @return String 全拼
*/
public String getASpell(char chrWord)
{
Spell obj_Spell = getObject(chrWord);

if(obj_Spell==null) return null;

return String.valueOf(obj_Spell.getASpell());
}

/**
* 獲得字串的拼音頭
* @param strWord 字串
* @return String 拼音頭
*/
public String getSpell(String strWord)
{
if(strWord==null) return null;
if(strWord.equals("")) return null;

char[] chr_Single = strWord.toCharArray();

String str_Spell = "";

for(int i=0; i<chr_Single.length; i++)
{
str_Spell += getSpell(chr_Single[i]);
}

return str_Spell;
}

/**
* 獲得字串的全拼
* @param strWord 字串
* @return String 全拼
*/
public String getASpell(String strWord)
{
if(strWord==null) return null;
if(strWord.equals("")) return null;

char[] chr_Single = strWord.toCharArray();

String str_ASpell = "";

for(int i=0; i<chr_Single.length; i++)
{
str_ASpell += getASpell(chr_Single[i]);
}

return str_ASpell;
}

/**
* 更新拼音
* @param chrWord 字元
* @param chrSpell 拼音頭
* @param strASpell 全拼
*/
public synchronized void update(char chrWord,
char chrSpell,
String strASpell) throws Exception
{
update(new Spell(chrWord, chrSpell, strASpell));
return;
}

/**
* 更新拼音
* @param objSpell 單個拼音物件
*/
public synchronized void update(Spell objSpell) throws Exception
{
try
{
Spell obj_Spell = getObject(objSpell.getWord());

DataStorage obj_DS = new DataStorage();

/// 拼音快取存在,更新
if (obj_Spell!=null)
{
obj_Spell.setSpell(objSpell.getSpell());
obj_Spell.setASpell(objSpell.getASpell());

String str_Spell = String.valueOf(objSpell.getSpell());
String str_ASpell = String.valueOf(objSpell.getASpell());
String str_Word = String.valueOf(objSpell.getWord());

String str_SQL = Common.UPDATE + Table.SPELL +
Common.S_SET + Field.SPELL + Common.EQUAL + General.addQuotes(str_Spell) + Common.COMMA +
Field.ASPELL + Common.EQUAL + General.addQuotes(str_ASpell) +
Common.S_WHERE + Field.WORD + Common.EQUAL + General.addQuotes(str_Word);

obj_DS.addSQL(str_SQL);
obj_DS.runSQL();
}

/// 拼音快取不存在,新增
else
{
arr_Spell.add(objSpell);

String str_SQL = Common.INSERT + Table.SPELL +
General.addBracket(Field.WORD + Common.COMMA +
Field.SPELL + Common.COMMA +
Field.ASPELL) +
Common.S_VALUES +
General.addBracket(General.addQuotes(String.valueOf(objSpell.getWord())) + Common.COMMA +
General.addQuotes(String.valueOf(objSpell.getSpell())) + Common.COMMA +
General.addQuotes(objSpell.getASpell()));

obj_DS.addSQL(str_SQL);
obj_DS.runSQL();
}

return;
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
}

}
class Spell implements java.io.Serializable
{
private static final long serialVersionUID = 8537233176930218357L;
private char m_chr_Word;
private char m_chr_Spell;
private String m_str_ASpell;

public Spell()
{
}

public Spell(char chrWord, char chrSpell, String strASpell)
{
m_chr_Word = chrWord;
m_chr_Spell = chrSpell;
m_str_ASpell = strASpell;
}

public String getASpell()
{
return m_str_ASpell;
}

public void setASpell(String strASpell)
{
m_str_ASpell = strASpell;
}

public char getSpell()
{
return m_chr_Spell;
}

public void setSpell(char chrSpell)
{
m_chr_Spell = chrSpell;
}

public char getWord()
{
return m_chr_Word;
}

public void setWord(char chrWord)
{
m_chr_Word = chrWord;
}

public boolean equals(char chrValue)
{
if(m_chr_Word==chrValue) return true;
return false;
}
}