All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tsc9526.monalisa.tools.datatable.DataMap Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*******************************************************************************************
 *	Copyright (c) 2016, zzg.zhou([email protected])
 * 
 *  Monalisa is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.

 *	You should have received a copy of the GNU Lesser General Public License
 *	along with this program.  If not, see .
 *******************************************************************************************/
package com.tsc9526.monalisa.tools.datatable;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.tsc9526.monalisa.orm.model.Model;
import com.tsc9526.monalisa.tools.clazz.MelpClass;
import com.tsc9526.monalisa.tools.clazz.MelpClass.ClassHelper;
import com.tsc9526.monalisa.tools.clazz.MelpClass.FGS;
import com.tsc9526.monalisa.tools.json.MelpJson;
import com.tsc9526.monalisa.tools.misc.MelpException;
import com.tsc9526.monalisa.tools.string.MelpDate;
import com.tsc9526.monalisa.tools.string.MelpString;

/**
 *  
 * case insensitive map
 * 
 * @author zzg.zhou([email protected])
 */

public class DataMap extends CaseInsensitiveMap{ 
	private static final long serialVersionUID = -8132926422921115814L;	
	 
	public static DataMap fromXml(String xml){
		return MelpString.xml2Map(xml);
	}
	
	public static DataMap fromJson(String json){
		return MelpString.json2Map(json);
	}
	
	public DataMap(){
		
	}
	
	public String toJson() {
		return toJson(true);
	}

	public String toJson(boolean pretty) {
		GsonBuilder gb=MelpJson.createGsonBuilder();
		
		if(pretty){
			gb.setPrettyPrinting();
		}
		
		return gb.create().toJson(this);
	}
	
	public DataMap(Map m){
		putAll(m);
	}
	
	@SuppressWarnings("unchecked")
	public  T gets(Object key){
		return (T)super.get(key);
	}
	
	@SuppressWarnings("unchecked")
 	public  T as(Class toClass){
		try {
			if(toClass.isAssignableFrom(DataMap.class)){
				return (T)this;
			}
			
			T r = toClass.newInstance();
			
			ClassHelper mc=MelpClass.getClassHelper(toClass);
			for(FGS fgs:mc.getFields()){
				String name=fgs.getFieldName();
				if(containsKey(name)){
					fgs.setObject(r, get(name));
				}
			} 
			 
			return r;
		}catch(Exception e) {
			return MelpException.throwRuntimeException(e);
		}
	}
	 	
	
	/**
	 * 
	 * @param index  The first key is 0.
	 * @return Object value
	 */
	@SuppressWarnings("unchecked")
	public Object get(int index){
		Map.Entry entry=(Map.Entry)this.entrySet().toArray()[index];
		 
		return entry.getValue();
	}
	
	/**
	 * For example: 
* * DataMap m=DataMap.fromJson("{'f1':{'f2':'yes'}}"); *
* m.getByPath("f1/f2") = "yes" * *
*

* Another example(array):
* * DataMap m=DataMap.fromJson("{'f1':{'f2':['a','b','c']}}"); *
m.getByPath("f1/f2[1]") = "a" *
m.getByPath("f1/f2[2]") = "b" *
m.getByPath("f1/f2[3]") = "c" *
*
m.getByPath("f1/f2[-1]") = "c" *
m.getByPath("f1/f2[-2]") = "b" *
m.getByPath("f1/f2[-3]") = "a" *
* * @param pathString split by / * @param result type * @return the object value, maybe null */ public T getByPath(String pathString){ String paths=pathString; if(paths.startsWith("/")){ paths=paths.substring(1); } String[] sv=paths.split("/"); Object ret=null; Object m=this; for(int i=0;i T getByPath(String paths,T defaultValue){ T r=getByPath(paths); if(r==null){ r=defaultValue; } return r; } private Object getValue(Object v,String fieldName){ // name[0] String name = fieldName; Integer index = null; int x=name.indexOf('['); if(x>0 && name.endsWith("]")){ index=Integer.parseInt(name.substring(x+1,name.length()-1).trim()); name=name.substring(0,x); } Object ret=null; if(v instanceof Map){ ret= ((Map)v).get(name); }else if(v instanceof JsonObject){ ret= ((JsonObject)v).get(name); }else if(v instanceof Model){ ret= ((Model)v).get(name); }else{ ClassHelper mc=MelpClass.getClassHelper(v); FGS fgs=mc.getField(name); if(fgs!=null){ ret= fgs.getObject(v); } } return getFromList(ret,index); } private Object getFromList(Object obj,Integer index){ if(index!=null && obj instanceof List){ List list=(List)obj; if(index < 0){ return list.get(list.size()+index); }else{ return list.get(index-1); } } return obj; } protected Object getOne(String key) { Object v=get(key); if(v!=null && v.getClass().isArray() ) { Object[] os=(Object[])v; return os[0]; }else{ return v; } } public String getString(String key,String defaultValue){ String v=getString(key); if(v==null){ v= defaultValue; } return v; } public String getString(String key){ Object v=getOne(key); if(v==null){ return null; }else { return v.toString(); } } public Integer[] getIntegerValues(String key){ String[] xs=getStringValues(key); if(xs!=null){ Integer[] rs=new Integer[xs.length]; for(int i=0;i * yyyy-MM-dd
* yyyy-MM-dd HH
* yyyy-MM-dd HH:mm
* yyyy-MM-dd HH:mm:ss
* yyyy-MM-dd HH:mm:ss.SSS
* * @param key label of the column * * @return date value */ public Date getDate(String key){ return getDate(key,null,null); } /** * @param key label of the column * @param defaultValue return this value if null * @return date value * * @see #getDate(String) */ public Date getDate(String key, Date defaultValue){ return getDate(key,null,defaultValue); } /** * * @param key label of the column * @param format new SimpleDateFormat(format): auto detect date format if null or '' * @param defaultValue return this value if null * @return date value * * @see #getDate(String) */ public Date getDate(String key,String format,Date defaultValue){ return MelpDate.getDate(getOne(key), format, defaultValue); } }