Java版Tuple简单实现。

Tuple类:

public class Tuple {
	
	private Map<String, Object> map;
	private String[] keyArr;
	private Pattern ptn;
	
	public Tuple(String...names) {
		if (names != null) {
			keyArr = names;
			map = new HashMap<>();
		}
	}
	
	/**
	 * 根据给定的key创建元组
	 * @param names
	 * @return
	 */
	public static Tuple def(String...names) {
		return new Tuple(names);
	}

	/**
	 * 引用匹配模式
	 * @param ptn 匹配模式
	 * @return 
	 */
	public Tuple use(Pattern ptn) {
		this.ptn = ptn;
		return this;
	}
	
	/**
	 * 根据匹配模式进行匹配,保存匹配结果,返回自身
	 * @param source 目标字符串
	 * @param containFullText 返回原则是否包含匹配全文
	 * @return
	 */
	public Tuple match(String source, boolean containFullText) {
		if (ptn == null) {
			throw new IllegalUnbindException("Pattern cannot be null!");
		}
		if (map != null) {
			this.map.clear();
		}
		Matcher matcher = ptn.matcher(source);
		if (matcher.find()) {
			int groupCount = matcher.groupCount();
			Object[] valArr = null;
			if (containFullText) {
				groupCount ++;
			}
			if (this.keyArr.length != groupCount) 
				throw new IllegalArgumentException("Tuple's key count doesn't match value count!");
			valArr = new Object[groupCount];
			for (int i = 0; i < groupCount; ++i) {
				valArr[i] = matcher.group(containFullText ? i : i+1);
			}
			this.val(valArr);
		}
		return this;
	}
	
	/**
	 * 根据匹配模式进行匹配,保存匹配结果,返回自身, 默认不返回匹配全文
	 * @param source
	 * @return
	 */
	public Tuple match(String source) {
		return this.match(source, false); // 默认不返回匹配的全文
	}
	
	/**
	 * 按key的顺序逐一赋值
	 * @param values 
	 * @return
	 */
	public Tuple val(Object...values) {
		if (values != null) {
			if (values.length != keyArr.length)
				throw new IllegalArgumentException("Tuple's key count doesn't match value count!");
			for (int i = 0; i < keyArr.length; ++i) {
				map.put(keyArr[i], values[i]);
			}
		}
		return this;
	}
	
	/**
	 * 将目标元组转换为指定key值的新元组,目标元组中的数据将会被清空并置为null;
	 * @param tuple 目标元组
	 * @return 返回指定了新key的新元组
	 */
	public Tuple with(Tuple tuple) {
		if (this.keyArr.length != tuple.keyArr.length)
			throw new IllegalArgumentException("Tuple's key count doesn't match value count!");
		for (int i = 0; i < tuple.keyArr.length; ++i) {
			this.map.put(this.keyArr[i], tuple.map.get(tuple.keyArr[i]));
		}
		tuple.map.clear(); // help gc
		tuple = null; // help gc
		return this;
	}
	
	/**
	 * 获取元组中指定key的value
	 * @param name
	 * @return
	 */
	public Object get(String name) {
		return this.map.get(name);
	}
	/**
	 * 根据指定类型从元组中获取key对应的value
	 * @param name key
	 * @param type 目标值的类型
	 * @return 
	 */
	public <T> T get(String name, Class<T> type) {
		return type.cast(this.map.get(name));
	}
	
	/**
	 * 根据元组的顺序取值
	 * @param index 值在元组中定义的下标
	 * @return
	 */
	public Object get(int index) {
		return this.map.get(this.keyArr[index]);
	}
	
	/**
	 * 根据元组的顺序取值
	 * @param index 值在元组中定义的下标
	 * @param type 目标值的类型
	 * @return
	 */
	public <T> T get(int index, Class<T> type) {
		return type.cast(this.map.get(this.keyArr[index]));
	}
}

使用:

public class TupleCase {

	public static void main(String[] args) {
		Tuple tuple = Tuple.def("num", "char", "date").with(getTupleData());
		System.out.println("main方法:");
		System.out.println(
				String.format("val: %s, type: %s", 
						tuple.get("num"), 
						tuple.get(0).getClass()));
		System.out.println(
				String.format("val: %s, type: %s", 
						tuple.get("char"), 
						tuple.get(1).getClass()));
		System.out.println(
				String.format("val: %s, type: %s", 
						tuple.get("date"), 
						tuple.get(2).getClass()));
		// 正则表达式匹配字符串
		System.out.println("正则表达式匹配字符串:");
		java.util.regex.Pattern ptn = java.util.regex.Pattern
				.compile("hello, my name is (.+?), i am ([0-9]+) years old and i like (.+?)!");
		Tuple match = Tuple.def("fulltext", "one", "two", "three")
						   .use(ptn)
						   .match("hello, my name is qkf, i am 24 years old and i like coding!", true);
		System.out.println(match.get("fulltext"));
		System.out.println(match.get("one"));
		System.out.println(match.get(2));
		System.out.println(match.get("three"));
		
	}
	
	private static Tuple getTupleData() {
		Tuple tuple = Tuple.def("a", "b", "c").val(3, 'l', LocalDate.now());
		// 名称取值
		System.out.println("名称取值:");
		System.out.println(tuple.get("a"));
		System.out.println(tuple.get("b"));
		System.out.println(tuple.get("c"));
		// 下标取值
		System.out.println("下标取值:");
		System.out.println(tuple.get(2));
		System.out.println(tuple.get(2, LocalDate.class));
		return tuple;
	}
}

运行结果:

 

转载于:https://my.oschina.net/u/2347651/blog/885368

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值