package com.leanwo.activiti.dto.base;

import java.io.Serializable;
import java.util.List;

public class KeyValue implements Serializable  {
	private Long key;
	private String keyStr;
	private String value;
	
	public KeyValue() {
	}
	
	public KeyValue(Long key, String value) {
		super();
		this.key = key;
		this.value = value;
	}
	

	public KeyValue(String keyStr, String value) {
		super();
		this.keyStr = keyStr;
		this.value = value;
	}
	

	public Long getKey() {
		return key;
	}

	public void setKey(Long key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * 获取keyStr 
	 * @return the keyStr
	 */
	public String getKeyStr() {
		return keyStr;
	}
	

	/**
	 * 设置keyStr
	 * @param keyStr the keyStr to set
	 */
	public void setKeyStr(String keyStr) {
		this.keyStr = keyStr;
	}
	

	@Override
	public boolean equals(Object obj) {
		return this.toString().equals(obj.toString());
	}

	@Override
	public String toString() {
		return key + "_" + keyStr + "_" + value;
	}
	
	public KeyValue clone(){
		KeyValue lookUpModel = new KeyValue();
		lookUpModel.setKey(this.key);
		lookUpModel.setValue(this.value);
		return lookUpModel;
	}
	
	/**
	 * 判断 KeyValue 集合中是否包含指定的 Key
	 * @param keyValues KeyValue 集合
	 * @param key 需要搜索的key
	 * @return true:集合中包含key  false:集合中不包含key
	 */
	public static boolean containsKey(List<KeyValue> keyValues, Long key){
		if(keyValues == null || key == null)
			return false;
		for(KeyValue keyValue : keyValues){
			if(keyValue.getKey().equals(key)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 从KeyValue 集合获取key对应的value
	 * @param keyValues KeyValue 集合
	 * @param key 需要查询的key
	 * @return
	 */
	public static String getValue(List<KeyValue> keyValues, Long key){
		if(keyValues == null || key == null)
			return null;
		for(KeyValue keyValue : keyValues){
			if(keyValue.getKey().equals(key)){
				return keyValue.getValue();
			}
		}
		return null;
	}
	
	
	/**
	 * 从KeyValue 集合获取value对应的key
	 * @param keyValues KeyValue 集合
	 * @param value 需要查询的key
	 * @return
	 */
	public static Long getKey(List<KeyValue> keyValues, String value){
		if(keyValues == null || value == null)
			return null;
		for(KeyValue keyValue : keyValues){
			if(keyValue.getValue().equals(value)){
				return keyValue.getKey();
			}
		}
		return null;
	}
}
