package com.izouma.wenlvju.converter;

import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringArrayConverter implements AttributeConverter<List<String>, String> {
    @Override
    public String convertToDatabaseColumn(List<String> strings) {
        if (strings != null && !strings.isEmpty())
            return StringUtils.join(strings, ",");
        return null;
    }

    @Override
    public List<String> convertToEntityAttribute(String s) {
        if (StringUtils.isNotEmpty(s)) {
            return Arrays.asList(s.split(","));
        }
        return new ArrayList<>();
    }
}
