001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.preferences;
003
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.LinkedHashMap;
007import java.util.List;
008import java.util.Map;
009
010/**
011 * Setting containing a {@link List} of {@link Map}s of {@link String} values.
012 * @since 9759
013 */
014public class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
015
016    /**
017     * Constructs a new {@code MapListSetting} with the given value
018     * @param value The setting value
019     */
020    public MapListSetting(List<Map<String, String>> value) {
021        super(value);
022        consistencyTest();
023    }
024
025    @Override
026    public MapListSetting copy() {
027        if (value == null)
028            return new MapListSetting(null);
029        List<Map<String, String>> copy = new ArrayList<>(value.size());
030        for (Map<String, String> map : value) {
031            Map<String, String> mapCopy = new LinkedHashMap<>(map);
032            copy.add(Collections.unmodifiableMap(mapCopy));
033        }
034        return new MapListSetting(Collections.unmodifiableList(copy));
035    }
036
037    private void consistencyTest() {
038        if (value == null)
039            return;
040        if (value.contains(null))
041            throw new IllegalArgumentException("Error: Null as list element in preference setting");
042        for (Map<String, String> map : value) {
043            if (map.keySet().contains(null))
044                throw new IllegalArgumentException("Error: Null as map key in preference setting");
045            if (map.values().contains(null))
046                throw new IllegalArgumentException("Error: Null as map value in preference setting");
047        }
048    }
049
050    @Override
051    public void visit(SettingVisitor visitor) {
052        visitor.visit(this);
053    }
054
055    @Override
056    public MapListSetting getNullInstance() {
057        return new MapListSetting(null);
058    }
059}