BrightSide Workbench Full Report + Source Code
Interests.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2024 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.interest;
20 
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import org.turro.participation.ParticipationReason;
25 import org.turro.plugin.contacts.IContact;
26 import org.turro.reflection.Instances;
27 import org.turro.util.Arrays;
28 
33 public class Interests {
34 
35  public void collect(Set<InterestCategory> categories) {
36  ParticipationReason[] normal = categories.stream()
37  .filter(c -> !c.isReversed())
38  .map(c -> c.getReason())
39  .toArray(ParticipationReason[]::new);
40  if(!Arrays.isEmpty(normal)) collect(normal);
41  ParticipationReason[] reversed = categories.stream()
42  .filter(c -> c.isReversed())
43  .map(c -> c.getReason())
44  .toArray(ParticipationReason[]::new);
45  if(!Arrays.isEmpty(reversed)) reversed(normal);
46  }
47 
48  public Set<Interest> collect(ParticipationReason... reasons) {
49  for(InterestCollector collector : Instances.fresh().byInterface(InterestCollector.class, InterestCollector.class)) {
50  interests.addAll(collector.collect(contact, reasons));
51  }
52  return interests;
53  }
54 
55  public Set<Interest> reversed(ParticipationReason... reasons) {
56  for(InterestCollector collector : Instances.fresh().byInterface(InterestCollector.class, InterestCollector.class)) {
57  interests.addAll(collector.reversed(contact, reasons));
58  }
59  return interests;
60  }
61 
62  public List<String> labels() {
63  return interests.stream().map(i -> i.getLabel()).distinct().sorted().toList();
64  }
65 
66  public Set<Interest> get() {
67  return interests;
68  }
69 
70  public void clear() {
71  interests.clear();
72  }
73 
74  public boolean isEmpty() {
75  return interests.isEmpty();
76  }
77 
78  /* Factory */
79 
80  public static Interests from(IContact contact) {
81  return new Interests(contact);
82  }
83 
84  private final IContact contact;
85  private final Set<Interest> interests;
86 
87  private Interests(IContact contact) {
88  this.contact = contact;
89  this.interests = new HashSet<>();
90  }
91 
92 }
void collect(Set< InterestCategory > categories)
Definition: Interests.java:35
Set< Interest > collect(ParticipationReason... reasons)
Definition: Interests.java:48
static Interests from(IContact contact)
Definition: Interests.java:80
List< String > labels()
Definition: Interests.java:62
Set< Interest > reversed(ParticipationReason... reasons)
Definition: Interests.java:55