001package org.unix4j.convert;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006
007public class CompositeValueConverter<V> implements ValueConverter<V> {
008        private final List<ValueConverter<? extends V>> converters = new ArrayList<ValueConverter<? extends V>>();
009        public CompositeValueConverter() {
010                super();
011        }
012        public CompositeValueConverter(Collection<? extends ValueConverter<? extends V>> converters) {
013                addAll(converters);
014        }
015        protected CompositeValueConverter<V> add(ValueConverter<? extends V> converter) {
016                this.converters.add(converter);
017                return this;
018        }
019        protected CompositeValueConverter<V> addAll(Collection<? extends ValueConverter<? extends V>> converters) {
020                this.converters.addAll(converters);
021                return this;
022        }
023        @Override
024        public V convert(Object value) throws IllegalArgumentException {
025                for (final ValueConverter<? extends V> converter : converters) {
026                        final V converted = converter.convert(value);
027                        if (converted != null) {
028                                return converted;
029                        }
030                }
031                return null;
032        }
033}