Skip to content

Instantly share code, notes, and snippets.

@dilnei
Last active February 18, 2016 11:55
Show Gist options
  • Select an option

  • Save dilnei/b000dec72dbfff4e2b89 to your computer and use it in GitHub Desktop.

Select an option

Save dilnei/b000dec72dbfff4e2b89 to your computer and use it in GitHub Desktop.
Transformation class to populate the data in XML, following the pattern Transfer Object Assembler.
package br.com.appweb.model.assembler;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Function;
/**
* Transformation class to populate the data in XML, following the pattern
* Transfer Object Assembler.
*
* @author Dilnei_Cunha
*/
public class Transformer {
/**
* Execute the transformation of an object to an XML.
*
* @param from
* @param function
* @return <F, T> T
*/
public static <F, T> T transform(F from, Function<? super F, ? extends T> function) {
return (from == null) ? null : function.apply(from);
}
/**
* Execute the transformation of a list of objects to a list of XML.
*
* @param fromList
* @param function
* @return <F, T> List<T>
*/
public static <F, T> List<T> transform(List<F> source, Function<? super F, ? extends T> function) {
List<T> out = new ArrayList<>(source.size());
for (F from : source) {
out.add(function.apply(from));
}
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment