BrightSide Workbench Full Report + Source Code
NotificationConstraintSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.action.queue;
20 
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
25 
30 public class NotificationConstraintSet extends HashSet<NotificationConstraint> {
31 
32  public NotificationConstraintSet(Collection<? extends NotificationConstraint> c) {
33  super(c == null ? Collections.EMPTY_LIST : c);
34  }
35 
36  public boolean strongBond(String... key) {
37  return meetRequired(List.of(key));
38  }
39 
40  public boolean wideBond(String... key) {
41  return meetRequired(List.of(key)) && meetOptionals(List.of(key));
42  }
43 
44  public boolean strongBond(ConstraintKeys keys) {
45  return meetRequired(keys.get());
46  }
47 
48  public boolean wideBond(ConstraintKeys keys) {
49  return meetRequired(keys.get()) && meetOptionals(keys.get());
50  }
51 
52  public boolean meetRequired(List<String> keys) {
53  return hasRequired() ? keys.containsAll(required()) : true;
54  }
55 
56  public boolean meetOptionals(List<String> keys) {
57  return hasOptional() ? optional().stream().anyMatch(keys::contains) : true;
58  }
59 
60  public boolean isRestricted() {
61  return hasRequired();
62  }
63 
64  /* Utils */
65 
66  private boolean hasRequired() {
67  return stream().anyMatch(nc -> !nc.isOptional());
68  }
69 
70  private List<String> required() {
71  return stream().filter(nc -> !nc.isOptional())
72  .map(nc -> nc.getKeyId())
73  .toList();
74  }
75 
76  private boolean hasOptional() {
77  return stream().anyMatch(nc -> nc.isOptional());
78  }
79 
80  private List<String> optional() {
81  return stream().filter(nc -> nc.isOptional())
82  .map(nc -> nc.getKeyId())
83  .toList();
84  }
85 
86 }
NotificationConstraintSet(Collection<? extends NotificationConstraint > c)