001package io.konik.csv.processor;
002
003import io.konik.zugferd.unece.codes.UnitOfMeasurement;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006import org.supercsv.cellprocessor.ift.CellProcessor;
007import org.supercsv.util.CsvContext;
008
009/**
010 * Custom {@link CellProcessor} for {@link UnitOfMeasurement}.
011 */
012public final class UnitOfMeasurementProcessor implements CellProcessor {
013
014        private static final Logger log = LoggerFactory.getLogger(UnitOfMeasurementProcessor.class);
015
016        @Override
017        public Object execute(Object value, CsvContext context) {
018
019                if (value instanceof String) {
020                        String code = (String) value;
021                        try {
022                                return UnitOfMeasurement.valueOf(code);
023                        } catch (IllegalArgumentException e) {
024                                log.warn("UnitOfMeasurement for value {} does not exist", value);
025                        }
026                }
027
028                if (value instanceof UnitOfMeasurement) {
029                        UnitOfMeasurement unitOfMeasurement = (UnitOfMeasurement) value;
030                        return unitOfMeasurement.name();
031                }
032
033                return null;
034        }
035}