本文主要是介绍【Agile Pair Coding】Data Type Mapping,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
今天下午用了1个小时左右,和同事Agile Pair Coding敏捷开发了一把,感觉挺爽的。
Agile Pair Coding给我们带来的直接好处是:相互不浪费时间(就两个人),高效;idea很快达成共识(就两个人),不纠结于无谓的讨论;idea立马coding,不沉迷于头脑风暴;代码更严谨;重构概率大;加深基情;相互学习,相互欣赏,相互指正;避免无知,避免自我感觉良好......
代码主要实现:从所有类型文件中,得到所有NE类型下的所有Object类型下的所有属性的数据类型。
当然,本文只是一个短时间内的Draft版本,可能会有一些问题,敬请指正。
package shuai.study.spring.validator;/*** @ClassName: Service* @Description: TODO* @author Zhou Shengshuai* @date 2014年8月8日 下午3:40:45* */
public interface Service {public void initialize();public void destroy();
}
package shuai.study.spring.validator;/*** @ClassName: TypeMapper* @Description: TODO* @author Zhou Shengshuai* @date 2014年8月8日 下午3:40:26* */
public interface TypeMapper {public String getType(String neType, String objectType, String fieldName);}
package shuai.study.spring.validator;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;/*** @ClassName: FileTypeMapper* @Description: TODO* @author Zhou Shengshuai* @date 2014年8月8日 下午3:10:06* */
public class DataTypeMapper implements TypeMapper, Service {private String filePath = null;private Map<String, Map<String, Map<String, String>>> allNeTypeMap = null;public DataTypeMapper() {}public void setFilePath(String filePath) {this.filePath = filePath;}@Overridepublic void initialize() {allNeTypeMap = getAllNeTypeMap(filePath);}@Overridepublic void destroy() {allNeTypeMap.clear();allNeTypeMap = null;}@Overridepublic String getType(String neType, String objectType, String fieldName) {if (allNeTypeMap != null && allNeTypeMap.containsKey(neType)) {Map<String, Map<String, String>> neTypeMap = allNeTypeMap.get(neType);if (neTypeMap != null && neTypeMap.containsKey(objectType)) {Map<String, String> objectTypeMap = neTypeMap.get(objectType);if (objectTypeMap != null && objectTypeMap.containsKey(fieldName)) {return objectTypeMap.get(fieldName);}}}return null;}private static Map<String, Map<String, Map<String, String>>> getAllNeTypeMap(String filepath) {Map<String, Map<String, Map<String, String>>> allNeTypeMap = new HashMap<String, Map<String, Map<String, String>>>();File[] files = new File(filepath).listFiles();for (File file : files) {String filename = file.getName();if (filename != null && filename.matches("\\w+-.*")) {allNeTypeMap.put(filename.split("-")[0], getNeTypeMap(file));}}return allNeTypeMap;}private static Map<String, Map<String, String>> getNeTypeMap(File file) {Map<String, Map<String, String>> neTypeMap = new HashMap<String, Map<String, String>>();BufferedReader reader = null;String line = null;Map<String, String> objectTypeMap = null;try {reader = new BufferedReader(new FileReader(file));while ((line = reader.readLine()) != null) {if (line.matches("\\[\\w+\\]")) {String objectType = line.substring(1, line.length() - 1).trim();if (!neTypeMap.containsKey(objectType)) {neTypeMap.put(objectType, new HashMap<String, String>());}objectTypeMap = neTypeMap.get(objectType);} else if (line.matches("\\b*\\w+\\b*:\\b*\\w+\\b*")) {String[] array = line.split(":");if (objectTypeMap != null && array != null && array.length == 2) {objectTypeMap.put(array[0].trim(), array[1].trim());}}}} catch (FileNotFoundException fnfe) {fnfe.printStackTrace();} catch (IOException ioe) {ioe.printStackTrace();} finally {try {reader.close();} catch (IOException ioe) {ioe.printStackTrace();}}return neTypeMap;}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="TypeMapper" class="shuai.study.spring.validator.DataTypeMapper" init-method="initialize" destroy-method="destroy" scope="singleton"><property name="filePath" value="D:/userdata/shengshu/Desktop/validation" /></bean></beans>
import org.springframework.context.support.FileSystemXmlApplicationContext;/*** @ClassName: Test* @Description: TODO* @author Zhou Shengshuai* @date 2014年8月8日 下午3:52:55* */
public class MapperApp {public static void main(String[] args) {@SuppressWarnings("resource")FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("spring-context-mapper.xml");TypeMapper mapper = (DataTypeMapper) context.getBean("TypeMapper");System.out.println(mapper.getType("CSCF", "PcscfFunction", "MaxBHSA"));context.getBeanFactory().destroySingletons();}
}
这篇关于【Agile Pair Coding】Data Type Mapping的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!