抽象模型,严谨代码,开源分享

2024-04-01 21:58

本文主要是介绍抽象模型,严谨代码,开源分享,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言
    首先,感谢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. 我们对特殊字符的处理,也要严谨,考虑方方面面,比如:在文件中将'&'转换成'&amp;','<'转换成'&lt;','>'转换成'&gt;',在这种情况下,你就不能将'&amp;'和'&lt;',以及'&gt;'中的'&'再转换成'&amp;'了。

    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的应用将会大大简化我们的代码逻辑与数量,实现代码与文本处理的解耦。


待续...

这篇关于抽象模型,严谨代码,开源分享的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/868233

相关文章

Java集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工

Java 线程池+分布式实现代码

《Java线程池+分布式实现代码》在Java开发中,池通过预先创建并管理一定数量的资源,避免频繁创建和销毁资源带来的性能开销,从而提高系统效率,:本文主要介绍Java线程池+分布式实现代码,需要... 目录1. 线程池1.1 自定义线程池实现1.1.1 线程池核心1.1.2 代码示例1.2 总结流程2. J

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

使用MapStruct实现Java对象映射的示例代码

《使用MapStruct实现Java对象映射的示例代码》本文主要介绍了使用MapStruct实现Java对象映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、什么是 MapStruct?二、实战演练:三步集成 MapStruct第一步:添加 Mave

Java抽象类Abstract Class示例代码详解

《Java抽象类AbstractClass示例代码详解》Java中的抽象类(AbstractClass)是面向对象编程中的重要概念,它通过abstract关键字声明,用于定义一组相关类的公共行为和属... 目录一、抽象类的定义1. 语法格式2. 核心特征二、抽象类的核心用途1. 定义公共接口2. 提供默认实