package com.wisemen.hhb.module.worddictionary.util;
import java.io.File;
import java.io.FilenameFilter;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wisemen.framework.util.StringUtils;
public class WordDictionaryUtil {
private static final Logger logger = LoggerFactory.getLogger(WordDictionaryUtil.class);
/**
* 一个空格
*/
public static final String ONE_BLANK = " ";
public static final String UNDERLINE = "_";
public static final String TEMP_IMAGES_PATH = "images/";
public static final String TEMP_AUDIOS_PATH = "audios/";
private static final Random random = new Random(47);
private static final int ONE_DAY_MILLISECOND = 24*60*60*1000;
/**
* 用作锁对象
*/
private static final Object lock = new Object();
/**
* 最后更新的日期
*/
private static final AtomicReference<Long> lastClearMillsecond = new AtomicReference<Long>(0l);
/**
* 清空临时文件的线程是否已经创建
*/
private static final AtomicReference<Boolean> clearThreadCreated = new AtomicReference<Boolean>(false);
/**
*
* <p>Description:将单词或词组标准化处理</p>
* 1、去掉前后空格
* 2、如果词组,中间多空格转换为单空格
* <p>Paramenter:</p>
* <p>Return:</p>
* <p>Exception:</p>
* <p>Modified History: </p>
*/
public static String strictWord( String word ){
if( StringUtils.isEmpty(word) ){
return null;
}
word = word.trim();
word = word.replaceAll("\\s+", ONE_BLANK);
return word;
}
/**
*
* <p>Description:将词组用下划线连接</p>
* <p>Paramenter:</p>
* <p>Return:</p>
* <p>Exception:</p>
* <p>Modified History: </p>
*/
public static String wordJoinWithUnderline( String word ){
if( StringUtils.isEmpty(word) ){
return null;
}
word = word.trim();
word = word.replaceAll("\\s+", UNDERLINE);
return word;
}
/**
*
* <p>Description:构建存放于Server的临时文件的文件名</p>
* <p>Paramenter:</p>
* <p>Return:</p>
* <p>Exception:</p>
* <p>Modified History: </p>
*/
public static String createTempFileName( String inputFileName ){
String type = "";
int index = inputFileName.lastIndexOf(".");
if(index >= 0){
type = inputFileName.substring(index+1);
}
String tempFileName = Long.valueOf(System.currentTimeMillis()).toString() + "_" + randomNumber();
if( StringUtils.isNotEmpty(type) ){
tempFileName += "." + type;
}
return tempFileName;
}
/**
*
* <p>Description:文件名的四位数的随机后缀</p>
* <p>Paramenter:</p>
* <p>Return:</p>
* <p>Exception:</p>
* <p>Modified History: </p>
*/
private static String randomNumber(){
DecimalFormat format = new DecimalFormat("0000");
return format.format( random.nextInt(9999) );
}
/**
*
* <p>Description:清空临时文件</p>
* <p>Paramenter:</p>
* <p>Return:</p>
* <p>Exception:</p>
* <p>Modified History: </p>
*/
public static void clearTempFile( final String contextPath ){
try{
long curMillisecond = System.currentTimeMillis();
if( lastClearMillsecond.get() == 0l || curMillisecond-lastClearMillsecond.get() > ONE_DAY_MILLISECOND ){
Thread thread = null;
synchronized (lock) {
if( clearThreadCreated.get() ){
return;
}
thread = new Thread( new ClearTempFileTask(contextPath,curMillisecond) );
clearThreadCreated.set(true);
}
thread.start();
}
}
catch( Exception e ){
//无论出现任何异常都保证不会抛出
System.out.println( "清空临时文件错误:" + e.getMessage() );
}
}
private static class ClearTempFileTask implements Runnable{
private String contextPath;
private long curMillisecond;
public ClearTempFileTask( String contextPath,long curMillisecond ){
this.contextPath = contextPath;
this.curMillisecond = curMillisecond;
}
@Override
public void run() {
try{
File imageDir = new File( contextPath + TEMP_IMAGES_PATH);
File audioDir = new File( contextPath + TEMP_AUDIOS_PATH);
FilenameFilter filter = new OneDayAfterFileNameFilter(curMillisecond);
File[] deleteImages = imageDir.listFiles(filter);
for( File f: deleteImages ){
f.delete();
}
File[] deleteAudios = audioDir.listFiles(filter);
for( File f: deleteAudios ){
f.delete();
}
//更新最后清空日期
lastClearMillsecond.set(curMillisecond);
}
catch( Exception e ){
logger.error("清空字典临时文件error:" + e.getMessage() );
}
finally{
clearThreadCreated.set(false);
}
}
}
/**
*
* <p>Class Name: OneDayAfterFileNameFilter</p>
* <p>Description: 过滤一天前的文件</p>
* <p>Sample: </p>
* <p>Author: 龚强</p>
* <p>Date: 2014-9-5</p>
* <p>Modified History: </p>
*/
private static class OneDayAfterFileNameFilter implements FilenameFilter{
private long curMillisecond;
public OneDayAfterFileNameFilter( long curMillisecond ){
this.curMillisecond = curMillisecond;
}
@Override
public boolean accept(File dir, String name) {
int index = name.indexOf("_");
long mis = Long.valueOf( name.substring(0, index) );
return curMillisecond-mis > ONE_DAY_MILLISECOND;
}
}
public static void main(String[] args) {
System.out.println( strictWord("aaa aaaa "));
System.out.println( wordJoinWithUnderline("aaa aaaa "));
}
}
最主要部分逻辑
public static void clearTempFile( final String contextPath ){
try{
long curMillisecond = System.currentTimeMillis();
if( lastClearMillsecond.get() == 0l || curMillisecond-lastClearMillsecond.get() > ONE_DAY_MILLISECOND ){
Thread thread = null;
synchronized (lock) {
if( clearThreadCreated.get() ){
return;
}
thread = new Thread( new ClearTempFileTask(contextPath,curMillisecond) );
clearThreadCreated.set(true);
}
thread.start();
}
}
catch( Exception e ){
//无论出现任何异常都保证不会抛出
System.out.println( "清空临时文件错误:" + e.getMessage() );
}
}