1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.spi.preferences;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Setting containing a {@link List} of {@code List}s of {@link String} values.
|
---|
11 | * @since 12881 (moved from package {@code org.openstreetmap.josm.data.preferences})
|
---|
12 | */
|
---|
13 | public class ListListSetting extends AbstractSetting<List<List<String>>> {
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Constructs a new {@code ListListSetting} with the given value
|
---|
17 | * @param value The setting value
|
---|
18 | */
|
---|
19 | public ListListSetting(List<List<String>> value) {
|
---|
20 | super(value);
|
---|
21 | consistencyTest();
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Convenience factory method.
|
---|
26 | * @param value the value
|
---|
27 | * @return a corresponding ListListSetting object
|
---|
28 | */
|
---|
29 | public static ListListSetting create(Collection<Collection<String>> value) {
|
---|
30 | if (value != null) {
|
---|
31 | List<List<String>> valueList = new ArrayList<>(value.size());
|
---|
32 | for (Collection<String> lst : value) {
|
---|
33 | valueList.add(new ArrayList<>(lst));
|
---|
34 | }
|
---|
35 | return new ListListSetting(valueList);
|
---|
36 | }
|
---|
37 | return new ListListSetting(null);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public ListListSetting copy() {
|
---|
42 | if (value == null)
|
---|
43 | return new ListListSetting(null);
|
---|
44 |
|
---|
45 | List<List<String>> copy = new ArrayList<>(value.size());
|
---|
46 | for (Collection<String> lst : value) {
|
---|
47 | List<String> lstCopy = new ArrayList<>(lst);
|
---|
48 | copy.add(Collections.unmodifiableList(lstCopy));
|
---|
49 | }
|
---|
50 | return new ListListSetting(Collections.unmodifiableList(copy));
|
---|
51 | }
|
---|
52 |
|
---|
53 | private void consistencyTest() {
|
---|
54 | if (value != null) {
|
---|
55 | if (value.contains(null))
|
---|
56 | throw new IllegalArgumentException("Error: Null as list element in preference setting");
|
---|
57 | for (Collection<String> lst : value) {
|
---|
58 | if (lst.contains(null)) {
|
---|
59 | throw new IllegalArgumentException("Error: Null as inner list element in preference setting");
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void visit(SettingVisitor visitor) {
|
---|
67 | visitor.visit(this);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public ListListSetting getNullInstance() {
|
---|
72 | return new ListListSetting(null);
|
---|
73 | }
|
---|
74 | }
|
---|