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

com.rathravane.till.data.mapper.DataMapper Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package com.rathravane.till.data.mapper;

import java.util.HashMap;

public class DataMapper, E2, T2 extends Mappable>
{
	public static class UnsupportedValueType extends Exception
	{
		public UnsupportedValueType ( String msg ) { super ( msg ); }
		private static final long serialVersionUID = 1L;
	}
	
	public interface valIo
	{
		T2 translate ( T1 v ) throws UnsupportedValueType;
	}

	public void registerForward ( E1 fromType, valIo vio )
	{
		forwardMap.put ( fromType, vio );
	}

	public void registerBack ( E2 toType, valIo vio )
	{
		reverseMap.put ( toType, vio );
	}

	public T2 translateForward ( T1 val ) throws UnsupportedValueType
	{
		final E1 fromType = val.getType ();
		final valIo io = forwardMap.get ( fromType );
		if ( io == null )
		{
			throw new UnsupportedValueType ( "No writer for type [" + fromType.toString () + "]." );
		}
		return io.translate ( val );
	}
	
	public T1 translateBack ( T2 val ) throws UnsupportedValueType
	{
		final E2 fromType = val.getType ();
		final valIo io = reverseMap.get ( fromType );
		if ( io == null )
		{
			throw new UnsupportedValueType ( "No reader for type [" + fromType.toString () + "]." );
		}
		return io.translate ( val );
	}

	private final HashMap> forwardMap = new HashMap> ();
	private final HashMap> reverseMap = new HashMap> ();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy