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.Collections;
007import java.util.List;
008
009/**
010 * Setting containing a {@link List} of {@code List}s of {@link String} values.
011 * @since 12881 (moved from package {@code org.openstreetmap.josm.data.preferences})
012 */
013public class ListListSetting extends AbstractSetting<List<List<String>>> {
014
015    /**
016     * Constructs a new {@code ListListSetting} with the given value
017     * @param value The setting value
018     */
019    public ListListSetting(List<List<String>> value) {
020        super(value);
021        consistencyTest();
022    }
023
024    /**
025     * Convenience factory method.
026     * @param value the value
027     * @return a corresponding ListListSetting object
028     */
029    public static ListListSetting create(Collection<Collection<String>> value) {
030        if (value != null) {
031            List<List<String>> valueList = new ArrayList<>(value.size());
032            for (Collection<String> lst : value) {
033                valueList.add(new ArrayList<>(lst));
034            }
035            return new ListListSetting(valueList);
036        }
037        return new ListListSetting(null);
038    }
039
040    @Override
041    public ListListSetting copy() {
042        if (value == null)
043            return new ListListSetting(null);
044
045        List<List<String>> copy = new ArrayList<>(value.size());
046        for (Collection<String> lst : value) {
047            List<String> lstCopy = new ArrayList<>(lst);
048            copy.add(Collections.unmodifiableList(lstCopy));
049        }
050        return new ListListSetting(Collections.unmodifiableList(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}