BrightSide Workbench Full Report + Source Code
TagSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.tags;
20 
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.stream.Collectors;
27 import org.turro.marker.MarkerHelper;
28 
33 public class TagSet extends TreeSet<TagItem> {
34 
35  public TagSet(Collection<? extends TagItem> c) {
36  super(c);
37  }
38 
39  public void select(String tagName) {
40  for(TagItem t : this) {
41  if(t.getTagName().equals(tagName)) {
42  t.setSelected(true);
43  }
44  }
45  }
46 
47  public void unselect(String tagName) {
48  for(TagItem t : this) {
49  if(t.getTagName().equals(tagName)) {
50  t.setSelected(false);
51  }
52  }
53  }
54 
55  public void toggle(String tagName) {
56  for(TagItem t : this) {
57  if(t.getTagName().equals(tagName)) {
58  if(t.isSelected()) {
59  t.setSelected(false);
60  } else if(t.isSibling()) {
61  t.setSelected(true);
62  } else {
63  unselectAll();
64  t.setSelected(true);
65  }
66  }
67  }
68  }
69 
70  public void selectAll() {
71  for(TagItem t : this) {
72  t.setSelected(true);
73  }
74  }
75 
76  public void unselectAll() {
77  for(TagItem t : this) {
78  t.setSelected(false);
79  }
80  }
81 
82  public void toggle() {
83  for(TagItem t : this) {
84  t.setSelected(!t.isSelected());
85  }
86  }
87 
88  public String tagUrl(TagItem tag) {
89  return MarkerHelper.setObfuscatedRightNowPars("tag=" + tag.getTagName());
90  }
91 
92  public TreeSet<String> getTagNames() {
93  return new TreeSet(stream().map(t -> t.getTagName()).collect(Collectors.toSet()));
94  }
95 
96  public TreeSet<TagItem> getSelected() {
97  return new TreeSet(stream().filter(t -> t.isSelected()).collect(Collectors.toSet()));
98  }
99 
100  public TreeSet<TagItem> getUnselected() {
101  return new TreeSet(stream().filter(t -> !t.isSelected()).collect(Collectors.toSet()));
102  }
103 
104  public TreeSet<String> getSelectedTagNames() {
105  return new TreeSet(stream().filter(t -> t.isSelected()).map(t -> t.getTagName()).collect(Collectors.toSet()));
106  }
107 
108  public TreeSet<String> getUnselectedTagNames() {
109  return new TreeSet(stream().filter(t -> !t.isSelected()).map(t -> t.getTagName()).collect(Collectors.toSet()));
110  }
111 
112  public long getMaxUsage() {
113  return stream().mapToLong(t -> t.getUsage()).max().orElse(0);
114  }
115 
116  public List<String> getIdentifiers(String root) {
117  Set<TagItem> set = getSelected();
118  if(!(set == null && set.isEmpty())) {
119  return Tags.getIdentifiers(root, set);
120  }
121  return Collections.EMPTY_LIST;
122  }
123 
124  public void markSiblingsFromSelection(String root) {
125  TagSet siblings = Tags.getSiblings(root, getSelectedTagNames());
126  for(TagItem tag : getUnselected()) {
127  tag.setSibling(siblings.contains(tag));
128  }
129  }
130 
131  public static TagSet empty() {
132  return new TagSet(Collections.EMPTY_SET);
133  }
134 
135 }
static String setObfuscatedRightNowPars(String parameters)
void setSelected(boolean selected)
Definition: TagItem.java:58
boolean isSelected()
Definition: TagItem.java:54
TreeSet< TagItem > getUnselected()
Definition: TagSet.java:100
static TagSet empty()
Definition: TagSet.java:131
void markSiblingsFromSelection(String root)
Definition: TagSet.java:124
void toggle(String tagName)
Definition: TagSet.java:55
void unselect(String tagName)
Definition: TagSet.java:47
TreeSet< TagItem > getSelected()
Definition: TagSet.java:96
String tagUrl(TagItem tag)
Definition: TagSet.java:88
List< String > getIdentifiers(String root)
Definition: TagSet.java:116
TagSet(Collection<? extends TagItem > c)
Definition: TagSet.java:35
TreeSet< String > getUnselectedTagNames()
Definition: TagSet.java:108
TreeSet< String > getTagNames()
Definition: TagSet.java:92
TreeSet< String > getSelectedTagNames()
Definition: TagSet.java:104
void select(String tagName)
Definition: TagSet.java:39
static List< String > getIdentifiers(String root, Set< TagItem > set)
Definition: Tags.java:234
static TagSet getSiblings(Set< String > tagNames)
Definition: Tags.java:82