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

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

相关文章

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代