1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.tagging.ac;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Objects;
|
---|
6 | import java.util.Optional;
|
---|
7 | import java.util.TreeSet;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A sorted set of {@link AutoCompletionItem}s.
|
---|
12 | *
|
---|
13 | * Items are sorted with higher priority first, then according to lexicographic order
|
---|
14 | * on the value of the {@code AutoCompletionListItem}.
|
---|
15 | *
|
---|
16 | * @since 12859 (extracted from {@code gui.tagging.ac.AutoCompletionList})
|
---|
17 | */
|
---|
18 | public class AutoCompletionSet extends TreeSet<AutoCompletionItem> {
|
---|
19 |
|
---|
20 | @Override
|
---|
21 | public boolean add(AutoCompletionItem e) {
|
---|
22 | // Is there already an item for the value?
|
---|
23 | Optional<AutoCompletionItem> result = stream().filter(i -> i.getValue().equals(e.getValue())).findFirst();
|
---|
24 | if (result.isPresent()) {
|
---|
25 | AutoCompletionItem item = result.get();
|
---|
26 | // yes: merge priorities
|
---|
27 | AutoCompletionPriority newPriority = item.getPriority().mergeWith(e.getPriority());
|
---|
28 | // if needed, remove/re-add the updated item to maintain set ordering
|
---|
29 | if (!item.getPriority().equals(newPriority)) {
|
---|
30 | remove(item);
|
---|
31 | item.setPriority(newPriority);
|
---|
32 | return add(item);
|
---|
33 | } else {
|
---|
34 | return false;
|
---|
35 | }
|
---|
36 | } else {
|
---|
37 | return super.add(e);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Adds a list of strings to this list. Only strings which
|
---|
43 | * are not null and which do not exist yet in the list are added.
|
---|
44 | *
|
---|
45 | * @param values a list of strings to add
|
---|
46 | * @param priority the priority to use
|
---|
47 | * @return {@code true} if this set changed as a result of the call
|
---|
48 | */
|
---|
49 | public boolean addAll(Collection<String> values, AutoCompletionPriority priority) {
|
---|
50 | return addAll(values.stream().filter(Objects::nonNull).map(v -> new AutoCompletionItem(v, priority)).collect(Collectors.toList()));
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Adds values that have been entered by the user.
|
---|
55 | * @param values values that have been entered by the user
|
---|
56 | * @return {@code true} if this set changed as a result of the call
|
---|
57 | */
|
---|
58 | public boolean addUserInput(Collection<String> values) {
|
---|
59 | int i = 0;
|
---|
60 | boolean modified = false;
|
---|
61 | for (String value : values) {
|
---|
62 | if (value != null && add(new AutoCompletionItem(value, new AutoCompletionPriority(false, false, false, i++)))) {
|
---|
63 | modified = true;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return modified;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Checks whether an item with the given value is already in the list. Ignores priority of the items.
|
---|
71 | *
|
---|
72 | * @param value the value of an auto completion item
|
---|
73 | * @return true, if value is in the list; false, otherwise
|
---|
74 | */
|
---|
75 | public boolean contains(String value) {
|
---|
76 | return stream().anyMatch(i -> i.getValue().equals(value));
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Removes the auto completion item with key <code>key</code>
|
---|
81 | * @param key the key
|
---|
82 | * @return {@code true} if an element was removed
|
---|
83 | */
|
---|
84 | public boolean remove(String key) {
|
---|
85 | return removeIf(i -> i.getValue().equals(key));
|
---|
86 | }
|
---|
87 | }
|
---|