BrightSide Workbench Full Report + Source Code
PhaseDefinition.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.phase;
20 
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import org.turro.entities.IElephantEntity;
28 import org.turro.security.ConceptPermission;
29 import org.turro.security.ConceptPermissionMap;
30 import org.turro.util.CompareUtil;
31 
36 public class PhaseDefinition implements Comparable<PhaseDefinition> {
37 
38  private final int index;
39  private final String name;
40  private final Set<PhaseType> types;
41  private final Map<String, Integer> mapping = new HashMap<>();
42  private final Set<String> documentation = new HashSet<>();
43 
44  private final ConceptPermissionMap permissions = new ConceptPermissionMap();
45 
46  private String icon, description;
47 
48  public PhaseDefinition icon(String icon) {
49  this.icon = icon;
50  return this;
51  }
52 
53  public PhaseDefinition described(String description) {
54  this.description = description;
55  return this;
56  }
57 
58  public PhaseDefinition addMapping(String targetEntity, Integer targetPhase) {
59  this.mapping.put(targetEntity, targetPhase);
60  return this;
61  }
62 
63  public PhaseDefinition addDocumentation(String documentation) {
64  this.documentation.add(documentation);
65  return this;
66  }
67 
69  permissions.put(permission);
70  return this;
71  }
72 
73  public int getIndex() {
74  return index;
75  }
76 
77  public String getName() {
78  return name;
79  }
80 
81  public Set<PhaseType> getTypes() {
82  return types;
83  }
84 
85  public boolean anyStringMatch(Collection<String> types) {
86  return anyTypeMatch(types.stream().map(t -> PhaseType.valueOf(t)).toList());
87  }
88 
89  public boolean anyTypeMatch(Collection<PhaseType> types) {
90  return this.types.stream().anyMatch(types::contains);
91  }
92 
93  public String getIconName() {
94  return icon;
95  }
96 
97  public String getDescription() {
98  return description;
99  }
100 
101  public Map<String, Integer> getMapping() {
102  return mapping;
103  }
104 
105  public Set<String> getDocumentation() {
106  return documentation;
107  }
108 
110  return Optional.ofNullable(permissions.get(name, iee)).orElse(null);
111  }
112 
113  public Collection<String> getConcepts() {
114  return permissions.concepts();
115  }
116 
117  /* Compare */
118 
119  @Override
120  public int compareTo(PhaseDefinition o) {
121  int result = Integer.compare(index, o.index);
122  if(result == 0) {
123  result = CompareUtil.compare(name, o.name);
124  }
125  return result;
126  }
127 
128  /* Factory */
129 
130  public static PhaseDefinition create(int index, String name, Set<PhaseType> types) {
131  return new PhaseDefinition(index, name, types);
132  }
133 
134  private PhaseDefinition(int index, String name, Set<PhaseType> types) {
135  this.index = index;
136  this.name = name;
137  this.types = types;
138  }
139 
140 }
Map< String, Integer > getMapping()
int compareTo(PhaseDefinition o)
static PhaseDefinition create(int index, String name, Set< PhaseType > types)
PhaseDefinition icon(String icon)
ConceptPermission getPermission(String name, IElephantEntity iee)
PhaseDefinition addMapping(String targetEntity, Integer targetPhase)
PhaseDefinition addDocumentation(String documentation)
Collection< String > getConcepts()
PhaseDefinition addPermission(ConceptPermission permission)
PhaseDefinition described(String description)
boolean anyStringMatch(Collection< String > types)
boolean anyTypeMatch(Collection< PhaseType > types)
ConceptPermission put(String key, ConceptPermission value)