Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Last active December 14, 2015 08:38
Show Gist options
  • Select an option

  • Save gastaldi/5059549 to your computer and use it in GitHub Desktop.

Select an option

Save gastaldi/5059549 to your computer and use it in GitHub Desktop.
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.convert;
import java.util.Arrays;
import java.util.List;
import javax.enterprise.inject.Vetoed;
/**
* A {@link ChainedConverter} converts elements in the specified sequence. <br/>
*
* The result of a {@link Converter} serves as the input for the next converter
*
* <br>
* The {@link ChainedConverter#convert(Object)} method always returns the last object converted
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
@Vetoed
@SuppressWarnings("rawtypes")
public class ChainedConverter implements Converter<Object, Object>
{
private List<Converter> converters;
public ChainedConverter(Converter... converters)
{
this.converters = Arrays.asList(converters);
}
/**
* This method always returns the last object converted from the list
*/
@Override
public Object convert(Object source)
{
Object value = source;
for (Converter<Object, Object> converter : converters)
{
if (converter != null)
{
value = converter.convert(value);
}
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment