本文主要是介绍抽象模型,严谨代码,开源分享,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言首先,感谢Eric对我代码上的建议,感谢Stone在FTP Lab环境部署上对我的指导。
今年4月份的时候,做了一个小的项目,当时也没有去总结整理,现在想想总结整理是很有必要的,这也是一个很好的工作研究的习惯。
关于项目,不论大小,其实做到极致也不是一件容易的事。只有做到极致,才算真正的项目经验;只有做到极致,才能让编程真正成为一门艺术;只有体会编程是一门有趣的艺术时,你的职业生涯才经久不衰,常青不老。
当然,我现在也只是一个走在编程艺术道路上的小孩,不停探索,充满好奇,我也希望志同道合的同仁们给我指教与分享。
我相信,只有开源了,才会有更多更大的进步,才会有活力,有创造力。我支持开源与自由,我唾弃陈旧死板的开发与管理!
项目简介
略......
编程点滴
1. 同样一个文件能只打开一次搞定的,就只打开一次,避免频繁的I/O流操作。比如:读一个属性文件,文件中有很多属性项目,我们可以将属性项预定义在一个数组里,当打开属性文件时,遍历属性数组,将得到的属性键值对预存在一个哈希表里,这样之后在用到属性值时,只需从哈希表里获取就可以了。
String[] propertyArray = new String[] { "templateDirectory", "sourceDirectory", "cm", "filereadyfile", "xmlCMIRPVersion", "expireHour", "meLimitCount", "fileNamePrefix", "subObjectLimitCount" }; Map<String, String> propertyMap = CommonTool.getPropertyMap(ConstantPool.CM_CONFIGRATION_FILE, propertyArray);
public static Map<String, String> getPropertyMap(String cmccCmConfigurationFile, String[] propertyArray) { InputStream inputStream = null; Map<String, String> propertyMap = new HashMap<String, String>(); int propertyArrayLength = propertyArray.length; Properties properties = new Properties(); try { inputStream = Files.newInputStream(Paths.get(cmccCmConfigurationFile)); properties.load(inputStream); } catch (IOException ioe) { LOG.error(ioe.getMessage()); ioe.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); } for (int index = 0; index < propertyArrayLength; index++) { String propertyName = propertyArray[index]; String propertyValue = properties.getProperty(propertyName); if (StringUtils.trimToEmpty(propertyValue).equals("")) { throw new NullPointerException("There is no property " + propertyName + " in the configuration file " + cmccCmConfigurationFile); } else { propertyValue = StringUtils.trim(propertyValue); } propertyMap.put(propertyName, propertyValue); } return propertyMap; }
2. 我们能计算一次搞定的,就只计算一次,比如:我们会经常遍历数组,往往会让数组长度的计算在循环中计算多次,这是低效的。
int propertyArrayLength = propertyArray.length; for (int index = 0; index < propertyArrayLength; index++) { ......
}
for (int index = 0; index < propertyArray.length; index++) { ......
}
3. 我们处理属性文件时,一定要严谨,比如:有个属性项是表示数量的,当我们获取到该属性项时,我们要对其进行数字正则判断,要不然你得到一个非数字的属性,你怎么拿来运算?
public static boolean isDigit(String str) { boolean isDigitFlag = false; if (str.matches("(^[-|+]?)\\d+")) { isDigitFlag = true; } return isDigitFlag;
}
4. 我们经常会拼凑,传参等方式来定义文件名,所以在这种情况下,有必要对文件名合法性与否作出正则判断。
public static boolean isInvalidFileName(String fileName) { boolean isInvalidFileNameFlag = false; if (fileName.matches(".*[\\/:\\*\\?\"<>\\|].*")) { isInvalidFileNameFlag = true; } return isInvalidFileNameFlag;
}
5. 我们对特殊字符的处理,也要严谨,考虑方方面面,比如:在文件中将'&'转换成'&','<'转换成'<','>'转换成'>',在这种情况下,你就不能将'&'和'<',以及'>'中的'&'再转换成'&'了。
public static String replaceReserveSymbel(String convertPartStr) { if (convertPartStr != null) { if (convertPartStr.contains("&")) { StringBuffer sb = new StringBuffer(""); convertPartStr = replaceReserveSymbel(convertPartStr, sb); } convertPartStr = convertPartStr.replace("<", "<"); convertPartStr = convertPartStr.replace(">", ">"); convertPartStr = convertPartStr.replace("\\", "\\\\"); convertPartStr = convertPartStr.replace("{", "\\{"); convertPartStr = convertPartStr.replace("}", "\\}"); convertPartStr = convertPartStr.replace("(", "\\("); convertPartStr = convertPartStr.replace(")", "\\)"); convertPartStr = convertPartStr.replace(",", "\\,"); } return convertPartStr; } public static String replaceReserveSymbel(String convertPartStr, StringBuffer sb) { int index = convertPartStr.indexOf("&"); String leftStr = ""; String rightStr = ""; if (index >= 0) { leftStr = convertPartStr.substring(0, index + 1); rightStr = convertPartStr.substring(index + 1); sb.append(leftStr); if (!rightStr.startsWith("amp;") && !rightStr.startsWith("lt;") && !rightStr.startsWith("gt;")) { sb.append("amp;"); } replaceReserveSymbel(rightStr, sb); } else { sb.append(convertPartStr); } return sb.toString(); }
6. 在多线程编程中,我们很多时候要实现接口Callable<?>, 而非实现接口Runnable,接口Callable<?>给我们带来的方便是不言而喻的,因为,可以重写它的方法call(),获得我们想要的返回值。
/** * @author shengshu * */ public class ConverterTask implements Callable<File> { private static final Logger LOG = Logger.getLogger(ConverterTask.class); private File templateFile = null; private File fragmentFile = null; private String exportDirectoryStr = null; private String notificationDirectoryStr = null; public String fileNamePrefix = null; private static final Date date = new Date(); public ConverterTask(File fragmentFile, File templateFile, String destDirectoryStr, String notificationDirectoryStr, String fileNamePrefix) { this.fragmentFile = fragmentFile; this.templateFile = templateFile; this.exportDirectoryStr = destDirectoryStr; this.notificationDirectoryStr = notificationDirectoryStr; this.fileNamePrefix = fileNamePrefix; } public File convertFile(File fragmentFile, File templateFile, String exportDirectoryStr, String notificationDirectoryStr, String fileNamePrefix) { ...... return exportFile; } public void convertFile(File fragmentFile, File templateFile, File exportDirectory, File notificationDirectory, String fileNamePrefix) throws Throwable { convertFile(fragmentFile, templateFile, exportDirectory.getAbsolutePath(), notificationDirectory.getAbsolutePath(), fileNamePrefix); } @Override public File call() throws Exception { File exportFile = convertFile(fragmentFile, templateFile, exportDirectoryStr, notificationDirectoryStr, fileNamePrefix); return exportFile; } }
private static void initConverter() { LOG.info("Converter thread pool size: " + threadPoolSize); converterThreadPool = Executors.newFixedThreadPool(threadPoolSize); converterFutureList = new ArrayList<Future<File>>(); if (sourceDirectory != null && sourceDirectory.isDirectory()) { fragmentFileCollection = FileUtils.listFiles(sourceDirectory, new String[] { "xml", "XML" }, false); } else { LOG.warn("The directory " + sourceDirectoryStr + " doesn't exist or is invalid!"); } if (templateDirectory != null && templateDirectory.isDirectory()) { templateFileCollection = FileUtils.listFiles(templateDirectory, new String[] { "xsl", "XSL" }, false); } else { LOG.warn("The directory " + templateDirectoryStr + " doesn't exist or is invalid!"); } } public void converter() { LOG.info("==================Converter Start=================="); startTime = System.currentTimeMillis(); initConverter(); if ((fragmentFileCollection != null && !fragmentFileCollection.isEmpty()) && (templateFileCollection != null && !templateFileCollection.isEmpty())) { Iterator<File> fragmentFileIterator = fragmentFileCollection.iterator(); while (fragmentFileIterator.hasNext()) { File fragmentFile = fragmentFileIterator.next(); LOG.info("Fragment file: " + fragmentFile); Iterator<File> templateFileIterator = templateFileCollection.iterator(); while (templateFileIterator.hasNext()) { File templateFile = templateFileIterator.next(); LOG.info("Template file: " + templateFile); ConverterTask converterTask = new ConverterTask(fragmentFile, templateFile, exportDirectoryStr, notificationDirectoryStr, fileNamePrefix); Future<File> converterFuture = converterThreadPool.submit(converterTask); converterFutureList.add(converterFuture); } } } destroyConverter(); endTime = System.currentTimeMillis(); deltaTime = endTime - startTime; LOG.info("Converter spend " + deltaTime / 1000 + " seconds"); LOG.info("==================Converter End=================="); } private static void destroyConverter() { LOG.info("===Start to destroy thread pool and delete files under source directory==="); for (Future<File> future : converterFutureList) { try { File exportFile = future.get(); if (exportFile.exists()) { LOG.info("Invoke method call() and return : " + exportFile); exportFileCollection.add(exportFile); } } catch (InterruptedException ie) { ie.printStackTrace(); } catch (ExecutionException ee) { ee.printStackTrace(); } } converterThreadPool.shutdown(); LOG.info("Thread pool is changed as status: SHUTDOWN"); while (!converterThreadPool.isTerminated()) ; LOG.info("Thread pool is changed as status: STOP"); if (!CommonTool.deleteFileCollection(fragmentFileCollection)) { LOG.error("Fail to delete fragment files under directory " + sourceDirectoryStr); } LOG.info("===End to destroy thread pool and delete files under source directory==="); }
7. 我们会经常处理多线程,高并发的问题,往往会用到线程池,所以我们既要保证任务都能被执行,又能保证线程池正常终止。
subSpliterThreadPool.shutdown(); while (!subSpliterThreadPool.isTerminated()) ;
8. 在处理XML文件时,对XSLT的应用将会大大简化我们的代码逻辑与数量,实现代码与文本处理的解耦。
这篇关于抽象模型,严谨代码,开源分享的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!