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

org.onetwo.common.utils.map.MappableMap Maven / Gradle / Ivy

There is a newer version: 4.7.2
Show newest version
package org.onetwo.common.utils.map;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.onetwo.common.reflect.ReflectUtils;

/***
 * 把对象映射为map
 * @author way
 *
 */
@SuppressWarnings("serial")
public class MappableMap extends HashMap{
	
	public static  StaticMappingBuilder newMappingBuilder(){
		return new StaticMappingBuilder();
	}
	
//	final private Object sourceObject;
//	final private List mappingInfos = new ArrayList<>();

	public MappableMap() {
	    super();
    }
	
	public MappableMap from(Object sourceObject){
	    PropertyDescriptor[] props = ReflectUtils.desribProperties(sourceObject.getClass());
	    Stream.of(props).forEach(p->{
	    	Object value = ReflectUtils.getProperty(sourceObject, p);
	    	if(value==null)
	    		return ;
	    	super.put(p.getName(), value);
	    });
	    return this;
	}
	
	public  MappingBuilder mapFrom(Object sourceObject){
	    return new MappingBuilder(this, sourceObject);
	}
	
	/***
	 * 
	 * @author way
	 *
	 * @param  sourceObject
	 */
	public static class StaticMappingBuilder {
		final private List> mappingInfos = new ArrayList<>();
		private boolean mapAllFields = true;
//		final private List sourceObjects;
		public StaticMappingBuilder() {
//	        this.sourceObjects = sourceObjects;//List sourceObjects
        }

		public StaticMappingBuilder mapAllFields(){
			this.mapAllFields = true;
			return this;
		}
		public StaticMappingBuilder specifyMappedFields(){
			this.mapAllFields = false;
			return this;
		}
		public StaticMappingBuilder addMapping(String jsonFieldName, String objectFieldName){
			this.mappingInfos.add(new MappingInfo(jsonFieldName, objectFieldName));
			return this;
		}
		
		public StaticMappingBuilder addMapping(String jsonFieldName, MappingValueFunc valueFunc){
			this.mappingInfos.add(new MappingInfo(jsonFieldName, valueFunc));
			return this;
		}
		
		
		public List bindValues(List sourceObjects){
		    return sourceObjects.stream().map(obj->bindMappings(new MappableMap(), obj))
										    .collect(Collectors.toList());
		}
		
		public MappableMap bindValue(T sourceObject){
			return bindMappings(new MappableMap(), sourceObject);
		}
		
		private MappableMap bindMappings(MappableMap mappingObject, T sourceObject){
			if(mapAllFields){
				mappingObject.from(sourceObject);
			}
		    mappingInfos.stream().forEach(maping->{
		    	if(maping.isMappingValueFunc()){
//			    	mappingObject.put(maping.jsonFieldName, maping.addMappingValueFunc.mapping(sourceObject, maping));
			    	mappingObject.put(maping.jsonFieldName, maping.addMappingValueFunc.mapping(sourceObject));
		    	}else{
			    	Object value = ReflectUtils.getPropertyValue(sourceObject, maping.objectFieldName);
			    	mappingObject.put(maping.jsonFieldName, value);
		    	}
		    });
		    return mappingObject;
		}
		
	}

	public static interface MappingValueFunc {
//		R mapping(T sourceObject, MappingInfo mapping);
		R mapping(T sourceObject);
	}
	
	public static class MappingBuilder {
		final private Object sourceObject;
		final private List> mappingInfos = new ArrayList<>();
		final private MappableMap mappingObject;
		public MappingBuilder(MappableMap mappingObject, Object sourceObject) {
	        super();
	        this.sourceObject = sourceObject;
	        this.mappingObject = mappingObject;
        }
		
		public MappingBuilder mapping(String sourceName, String targetName){
			this.mappingInfos.add(new MappingInfo(sourceName, targetName));
			return this;
		}

		
		public MappableMap bindValues(){
		    mappingInfos.stream().forEach(maping->{
		    	Object value = ReflectUtils.getPropertyValue(sourceObject, maping.jsonFieldName);
		    	mappingObject.put(maping.objectFieldName, value);
		    });
		    return mappingObject;
		}
		
	}
	
	/***
	 * 
	 * @author way
	 *
	 * @param  sourceObject
	 */
	public static final class MappingInfo {
		final private String jsonFieldName;
		final private String objectFieldName;
		final private MappingValueFunc addMappingValueFunc;
		public MappingInfo(String jsonFieldName, String objectFieldName) {
	        super();
	        this.jsonFieldName = jsonFieldName;
	        this.objectFieldName = objectFieldName;
	        this.addMappingValueFunc = null;
        }
		public MappingInfo(String sourceName, MappingValueFunc addMappingValueFunc) {
	        super();
	        this.jsonFieldName = sourceName;
	        this.objectFieldName = null;
	        this.addMappingValueFunc = addMappingValueFunc;
        }
		
		public boolean isMappingValueFunc(){
			return this.addMappingValueFunc!=null;
		}
		public String getJsonFieldName() {
			return jsonFieldName;
		}
		public String getObjectFieldName() {
			return objectFieldName;
		}
		public MappingValueFunc getAddMappingValueFunc() {
			return addMappingValueFunc;
		}
		
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy