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.Map;

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.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 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];
		
		Object v=entry.getValue();
		return v;
	}
	
	/**
	 * Map example: 
* * DataMap m=DataMap.fromJson("{'f1':{'f2':'yes'}}"); *
* m.getPath("f1/f2") will be return string: "yes" * *
* * @param paths split by / * @param result type * @return the object value */ @SuppressWarnings("unchecked") public T getByPath(String paths){ if(paths.startsWith("/")){ paths=paths.substring(1); } String sv[]=paths.split("/"); Object ret=null; Object m=this; for(int i=0;i)v).get(name); }else if(v instanceof JsonObject){ return ((JsonObject)v).get(name); }else if(v instanceof Model){ return ((Model)v).get(name); }else{ ClassHelper mc=MelpClass.getClassHelper(v); FGS fgs=mc.getField(name); if(fgs!=null){ return fgs.getObject(v); }else{ return null; } } } protected Object getOne(String key) { Object v=get(key); if(v!=null && v.getClass().isArray() ) { return ((Object[])v)[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); } }