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