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

com.aol.cyclops.lambda.api.Mappable Maven / Gradle / Ivy

There is a newer version: 7.2.4
Show newest version
package com.aol.cyclops.lambda.api;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.aol.cyclops.invokedynamic.ExceptionSoftener;
import com.aol.cyclops.invokedynamic.ReflectionCache;

/**
 * Interface that represents an Object that can be converted to a map
 * 
 * @author johnmcclean
 *
 */
public interface Mappable {
	default Object unwrap(){
		return this;
	}
	/**
	 * default implementation maps field values on the host object by name
	 * 
	 * @return Map representation
	 */
	default Map toMap(){
		try {
			final Object o = unwrap();
			Map result = new HashMap<>();
			for (Field f : ReflectionCache.getFields(o.getClass())) {
			    result.put(f.getName(),f.get(o));
			}
			return result;
		} catch (Exception e) {
			throw ExceptionSoftener
					.throwSoftenedException(e);
			
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy