【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;
	}

	@Override
	public void initialize() {
		allNeTypeMap = getAllNeTypeMap(filePath);
	}

	@Override
	public void destroy() {
		allNeTypeMap.clear();
		allNeTypeMap = null;
	}

	@Override
	public 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();
	}
}
时间: 2024-08-02 21:59:56

【Agile Pair Coding】Data Type Mapping的相关文章

【Python数据挖掘课程】六.Numpy、Pandas和Matplotlib包基础知识

        前面几篇文章采用的案例的方法进行介绍的,这篇文章主要介绍Python常用的扩展包,同时结合数据挖掘相关知识介绍该包具体的用法,主要介绍Numpy.Pandas和Matplotlib三个包.目录:        一.Python常用扩展包         二.Numpy科学计算包         三.Pandas数据分析包         四.Matplotlib绘图包         前文推荐:        [Python数据挖掘课程]一.安装Python及爬虫入门介绍    

【Python数据挖掘课程】五.线性回归知识及预测糖尿病实例

        今天主要讲述的内容是关于一元线性回归的知识,Python实现,包括以下内容:         1.机器学习常用数据集介绍         2.什么是线性回顾         3.LinearRegression使用方法        4.线性回归判断糖尿病        前文推荐:       [Python数据挖掘课程]一.安装Python及爬虫入门介绍       [Python数据挖掘课程]二.Kmeans聚类数据分析及Anaconda介绍       [Python数据挖

【Python数据挖掘课程】四.决策树DTC数据分析及鸢尾数据集分析

        今天主要讲述的内容是关于决策树的知识,主要包括以下内容:         1.分类及决策树算法介绍         2.鸢尾花卉数据集介绍         3.决策树实现鸢尾数据集分析         前文推荐:       [Python数据挖掘课程]一.安装Python及爬虫入门介绍       [Python数据挖掘课程]二.Kmeans聚类数据分析及Anaconda介绍        [Python数据挖掘课程]三.Kmeans聚类代码实现.作业及优化        希望

【Python数据挖掘课程】三.Kmeans聚类代码实现、作业及优化

        这篇文章直接给出上次关于Kmeans聚类的篮球远动员数据分析案例,同时介绍这次作业同学们完成的图例,最后介绍Matplotlib包绘图的优化知识.         前文推荐:        [Python数据挖掘课程]一.安装Python及爬虫入门介绍        [Python数据挖掘课程]二.Kmeans聚类数据分析及Anaconda介绍        希望这篇文章对你有所帮助,尤其是刚刚接触数据挖掘以及大数据的同学,同时准备尝试以案例为主的方式进行讲解.如果文章中存在不足

【Python爬虫8】Scrapy 爬虫框架

安装Scrapy 新建项目 1定义模型 2创建爬虫 3优化设置 4测试爬虫 5使用shell命令提取数据 6提取数据保存到文件中 7中断和恢复爬虫 使用Portia编写可视化爬虫 1安装 2标注 3优化爬虫 4检查结果 使用Scrapely实现自动化提取 1.安装Scrapy 用pip命令安装Scrapy:pip install Scrapy wu_being@ubuntukylin64:~/GitHub/WebScrapingWithPython$ scrapy -h Scrapy 1.3.0

【Python爬虫1】网络爬虫简介

调研目标网站背景 1 检查robotstxt 2 检查网站地图 3 估算网站大小 4 识别网站所有技术 5 寻找网站所有者 第一个网络爬虫 1 下载网页 重试下载 设置用户代理user_agent 2 爬取网站地图 3 遍历每个网页的数据库ID 4 跟踪网页链接 高级功能 解析robotstxt 支持代理Proxy 下载限速 避免爬虫陷阱 最终版本 1 调研目标网站背景 1.1 检查robots.txt http://example.webscraping.com/robots.txt # se

【Python爬虫6】表单交互

手工处理发送POST请求提交登录表单 1分析表单内容 2手工测试post请求提交表单 3手工处理post请求登录的完整源代码 从FF浏览器加载cookie登录网站 1session文件位置 2FF浏览器cookie内容 3使用cookie测试加载登录 4使用cookie登录源代码 使用高级模块Mechanize自动化处理表单提交 1用高级模块Mechanize自动化处理表单提交并支持登录后网页内容更新 2用普通方法支持登录后网页内容更新 严格来说,本篇表单交互和下一篇验证码处理不算是网络爬虫,而

【Python数据挖掘课程】二.Kmeans聚类数据分析及Anaconda介绍

        这次课程主要讲述一个关于Kmeans聚类的数据分析案例,通过这个案例让同学们简单了解大数据分析的基本流程,以及使用Python实现相关的聚类分析.         主要内容包括:         1.Anaconda软件的安装过程及简单配置         2.聚类及Kmeans算法介绍        3.案例分析:Kmeans实现运动员位置聚集         前文推荐:[Python数据挖掘课程]一.安装Python及爬虫入门介绍         希望这篇文章对你有所帮助,尤

【Python数据挖掘课程】七.PCA降维操作及subplot子图绘制

        这篇文章主要介绍四个知识点,也是我那节课讲课的内容.         1.PCA降维操作:         2.Python中Sklearn的PCA扩展包:         3.Matplotlib的subplot函数绘制子图:        4.通过Kmeans对糖尿病数据集进行聚类,并绘制子图.         前文推荐:        [Python数据挖掘课程]一.安装Python及爬虫入门介绍        [Python数据挖掘课程]二.Kmeans聚类数据分析及An