获得字符串的拼音头和全拼的写法

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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值