码迷,mamicode.com
首页 > 移动开发 > 详细

【Agile Pair Coding】Data Type Mapping

时间:2014-08-08 21:23:26      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

介绍

    今天下午用了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();
	}
}


【Agile Pair Coding】Data Type Mapping,布布扣,bubuko.com

【Agile Pair Coding】Data Type Mapping

标签:des   style   blog   http   color   java   os   io   

原文地址:http://blog.csdn.net/three_man/article/details/38444525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!