Java版Tuple簡單實現。

Tuple類:java

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

運行結果:this

相關文章
相關標籤/搜索