001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.spi.preferences;
003
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.List;
007import java.util.stream.Collectors;
008
009import org.openstreetmap.josm.tools.StreamUtils;
010import org.openstreetmap.josm.tools.Utils;
011
012/**
013 * Setting containing a {@link List} of {@code List}s of {@link String} values.
014 * @since 12881 (moved from package {@code org.openstreetmap.josm.data.preferences})
015 */
016public class ListListSetting extends AbstractSetting<List<List<String>>> {
017
018    /**
019     * Constructs a new {@code ListListSetting} with the given value
020     * @param value The setting value
021     */
022    public ListListSetting(List<List<String>> value) {
023        super(value);
024        consistencyTest();
025    }
026
027    /**
028     * Convenience factory method.
029     * @param value the value
030     * @return a corresponding ListListSetting object
031     */
032    public static ListListSetting create(Collection<Collection<String>> value) {
033        if (value != null) {
034            List<List<String>> valueList = value.stream()
035                    .map(ArrayList::new)
036                    .collect(Collectors.toList());
037            return new ListListSetting(valueList);
038        }
039        return new ListListSetting(null);
040    }
041
042    @Override
043    public ListListSetting copy() {
044        if (value == null)
045            return new ListListSetting(null);
046
047        List<List<String>> copy = value.stream()
048                .map(Utils::toUnmodifiableList)
049                .collect(StreamUtils.toUnmodifiableList());
050        return new ListListSetting(copy);
051    }
052
053    private void consistencyTest() {
054        if (value != null) {
055            if (value.contains(null))
056                throw new IllegalArgumentException("Error: Null as list element in preference setting");
057            for (Collection<String> lst : value) {
058                if (lst.contains(null)) {
059                    throw new IllegalArgumentException("Error: Null as inner list element in preference setting");
060                }
061            }
062        }
063    }
064
065    @Override
066    public void visit(SettingVisitor visitor) {
067        visitor.visit(this);
068    }
069
070    @Override
071    public ListListSetting getNullInstance() {
072        return new ListListSetting(null);
073    }
074}