001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.preferences; 003 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.Collections; 007import java.util.Iterator; 008import java.util.List; 009 010import org.openstreetmap.josm.tools.Utils; 011 012/** 013 * Setting containing a {@link List} of {@code List}s of {@link String} values. 014 * @since 9759 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 = new ArrayList<>(value.size()); 035 for (Collection<String> lst : value) { 036 valueList.add(new ArrayList<>(lst)); 037 } 038 return new ListListSetting(valueList); 039 } 040 return new ListListSetting(null); 041 } 042 043 @Override 044 public boolean equalVal(List<List<String>> otherVal) { 045 if (value == null) 046 return otherVal == null; 047 if (otherVal == null) 048 return false; 049 if (value.size() != otherVal.size()) 050 return false; 051 Iterator<List<String>> itA = value.iterator(); 052 Iterator<List<String>> itB = otherVal.iterator(); 053 while (itA.hasNext()) { 054 if (!Utils.equalCollection(itA.next(), itB.next())) 055 return false; 056 } 057 return true; 058 } 059 060 @Override 061 public ListListSetting copy() { 062 if (value == null) 063 return new ListListSetting(null); 064 065 List<List<String>> copy = new ArrayList<>(value.size()); 066 for (Collection<String> lst : value) { 067 List<String> lstCopy = new ArrayList<>(lst); 068 copy.add(Collections.unmodifiableList(lstCopy)); 069 } 070 return new ListListSetting(Collections.unmodifiableList(copy)); 071 } 072 073 private void consistencyTest() { 074 if (value != null) { 075 if (value.contains(null)) 076 throw new RuntimeException("Error: Null as list element in preference setting"); 077 for (Collection<String> lst : value) { 078 if (lst.contains(null)) { 079 throw new RuntimeException("Error: Null as inner list element in preference setting"); 080 } 081 } 082 } 083 } 084 085 @Override 086 public void visit(SettingVisitor visitor) { 087 visitor.visit(this); 088 } 089 090 @Override 091 public ListListSetting getNullInstance() { 092 return new ListListSetting(null); 093 } 094 095 @Override 096 public boolean equals(Object other) { 097 if (!(other instanceof ListListSetting)) 098 return false; 099 return equalVal(((ListListSetting) other).getValue()); 100 } 101}