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

com.app55.converter.Converter Maven / Gradle / Ivy

The newest version!
package com.app55.converter;

import java.util.ArrayList;
import java.util.List;

public class Converter implements IConverter
{
	private List	converters;

	private Converter()
	{
		converters = new ArrayList();
		register(new PrimitiveConverter());
	}

	private static Converter	instance;

	public static Converter getInstance()
	{
		if (instance == null)
			instance = new Converter();
		return instance;
	}

	private void register(IConverter converter)
	{
		if (converter == this)
			throw new IllegalArgumentException();
		converters.add(converter);
	}

	public boolean canConvert(Object o)
	{
		for (IConverter converter : converters)
			if (converter.canConvert(o))
				return true;

		return false;
	}

	public String convert(Object o)
	{
		if (o == null)
			return null;

		for (IConverter converter : converters)
			if (converter.canConvert(o))
				return converter.convert(o);

		return null;
	}

	public  T convert(String s, Class type)
	{
		for (IConverter converter : converters)
			if (converter.canConvert(s))
				return converter.convert(s, type);

		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy