Skip to content

Commit e814937

Browse files
Adds perspectives to archetypes.
1 parent 1d23a0d commit e814937

25 files changed

+161
-27
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.structurizr;
2+
3+
import com.structurizr.model.Perspective;
4+
import com.structurizr.util.StringUtils;
5+
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.TreeSet;
9+
10+
public interface PerspectivesHolder {
11+
12+
/**
13+
* Gets the set of perspectives associated with this object.
14+
*
15+
* @return a Set of Perspective objects (empty if there are none)
16+
*/
17+
Set<Perspective> getPerspectives();
18+
19+
/**
20+
* Adds a perspective to this object.
21+
*
22+
* @param name the name of the perspective (e.g. "Security", must be unique)
23+
* @param description the description of the perspective
24+
* @return a Perspective object
25+
* @throws IllegalArgumentException if perspective details are not specified, or the named perspective exists already
26+
*/
27+
Perspective addPerspective(String name, String description);
28+
29+
/**
30+
* Adds a perspective to this object.
31+
*
32+
* @param name the name of the perspective (e.g. "Technical Debt", must be unique)
33+
* @param description the description of the perspective (e.g. "High")
34+
* @param value the value of the perspective
35+
* @return a Perspective object
36+
* @throws IllegalArgumentException if perspective details are not specified, or the named perspective exists already
37+
*/
38+
Perspective addPerspective(String name, String description, String value);
39+
40+
}

structurizr-core/src/main/java/com/structurizr/PropertyHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
public interface PropertyHolder {
66

77
/**
8-
* Gets the collection of name-value property pairs associated with this workspace, as a Map.
8+
* Gets the collection of name-value property pairs associated with this object, as a Map.
99
*
1010
* @return a Map (String, String) (empty if there are no properties)
1111
*/
1212
public Map<String, String> getProperties();
1313

1414
/**
15-
* Adds a name-value pair property to this workspace.
15+
* Adds a name-value pair property to this object.
1616
*
1717
* @param name the name of the property
1818
* @param value the value of the property

structurizr-core/src/main/java/com/structurizr/model/ModelItem.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.structurizr.model;
22

33
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.structurizr.PerspectivesHolder;
45
import com.structurizr.PropertyHolder;
56
import com.structurizr.util.StringUtils;
67
import com.structurizr.util.TagUtils;
@@ -11,7 +12,7 @@
1112
/**
1213
* The base class for elements and relationships.
1314
*/
14-
public abstract class ModelItem implements PropertyHolder, Comparable<ModelItem> {
15+
public abstract class ModelItem implements PropertyHolder, PerspectivesHolder, Comparable<ModelItem> {
1516

1617
private String id = "";
1718
private final Set<String> tags = new LinkedHashSet<>();
@@ -249,6 +250,17 @@ public Perspective addPerspective(String name, String description, String value)
249250
return perspective;
250251
}
251252

253+
/**
254+
* Adds a collection of name-value pair properties to this model item.
255+
*
256+
* @param perspectives Set of Perspective objects
257+
*/
258+
public void addPerspectives(Set<Perspective> perspectives) {
259+
for (Perspective perspective : perspectives) {
260+
addPerspective(perspective.getName(), perspective.getDescription(), perspective.getValue());
261+
}
262+
}
263+
252264
@Override
253265
public int compareTo(ModelItem modelItem) {
254266
try {

structurizr-core/src/main/java/com/structurizr/model/Perspective.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class Perspective implements Comparable<Perspective> {
1313
Perspective() {
1414
}
1515

16-
Perspective(String name, String description, String value) {
16+
public Perspective(String name, String description, String value) {
1717
this.name = name;
1818
this.description = description;
1919
this.value = value;

structurizr-dsl/src/main/java/com/structurizr/dsl/Archetype.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.structurizr.dsl;
22

3+
import com.structurizr.PerspectivesHolder;
34
import com.structurizr.PropertyHolder;
45
import com.structurizr.model.Perspective;
56
import com.structurizr.util.StringUtils;
67

78
import java.util.*;
89

9-
final class Archetype implements PropertyHolder {
10+
final class Archetype implements PropertyHolder, PerspectivesHolder {
1011

1112
private final String name;
1213
private final String type;
1314
private String description = "";
1415
private String technology = "";
1516
private final Set<String> tags = new LinkedHashSet<>();
1617

17-
private Map<String, String> properties = new HashMap<>();
18+
private final Map<String, String> properties = new HashMap<>();
1819
private final Set<Perspective> perspectives = new TreeSet<>();
1920

2021
Archetype(String name, String type) {
@@ -67,7 +68,7 @@ Set<String> getTags() {
6768
}
6869

6970
/**
70-
* Gets the collection of name-value property pairs associated with this model item, as a Map.
71+
* Gets the collection of name-value property pairs associated with this archetype, as a Map.
7172
*
7273
* @return a Map (String, String) (empty if there are no properties)
7374
*/
@@ -76,7 +77,7 @@ public Map<String, String> getProperties() {
7677
}
7778

7879
/**
79-
* Adds a name-value pair property to this model item.
80+
* Adds a name-value pair property to this archetype.
8081
*
8182
* @param name the name of the property
8283
* @param value the value of the property
@@ -93,4 +94,53 @@ public void addProperty(String name, String value) {
9394
properties.put(name, value);
9495
}
9596

97+
/**
98+
* Gets the set of perspectives associated with this archetype.
99+
*
100+
* @return a Set of Perspective objects (empty if there are none)
101+
*/
102+
public Set<Perspective> getPerspectives() {
103+
return new TreeSet<>(perspectives);
104+
}
105+
106+
/**
107+
* Adds a perspective to this archetype.
108+
*
109+
* @param name the name of the perspective (e.g. "Security", must be unique)
110+
* @param description the description of the perspective
111+
* @return a Perspective object
112+
* @throws IllegalArgumentException if perspective details are not specified, or the named perspective exists already
113+
*/
114+
public Perspective addPerspective(String name, String description) {
115+
return addPerspective(name, description, "");
116+
}
117+
118+
/**
119+
* Adds a perspective to this archetype.
120+
*
121+
* @param name the name of the perspective (e.g. "Technical Debt", must be unique)
122+
* @param description the description of the perspective (e.g. "High")
123+
* @param value the value of the perspective
124+
* @return a Perspective object
125+
* @throws IllegalArgumentException if perspective details are not specified, or the named perspective exists already
126+
*/
127+
public Perspective addPerspective(String name, String description, String value) {
128+
if (StringUtils.isNullOrEmpty(name)) {
129+
throw new IllegalArgumentException("A name must be specified.");
130+
}
131+
132+
if (StringUtils.isNullOrEmpty(description)) {
133+
throw new IllegalArgumentException("A description must be specified.");
134+
}
135+
136+
if (perspectives.stream().anyMatch(p -> p.getName().equals(name))) {
137+
throw new IllegalArgumentException("A perspective named \"" + name + "\" already exists.");
138+
}
139+
140+
Perspective perspective = new Perspective(name, description, value);
141+
perspectives.add(perspective);
142+
143+
return perspective;
144+
}
145+
96146
}

structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentArchetypeDslContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ protected String[] getPermittedTokens() {
1313
StructurizrDslTokens.TECHNOLOGY_TOKEN,
1414
StructurizrDslTokens.TAG_TOKEN,
1515
StructurizrDslTokens.TAGS_TOKEN,
16-
StructurizrDslTokens.PROPERTIES_TOKEN
16+
StructurizrDslTokens.PROPERTIES_TOKEN,
17+
StructurizrDslTokens.PERSPECTIVES_TOKEN
1718
};
1819
}
1920

structurizr-dsl/src/main/java/com/structurizr/dsl/ComponentParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Component parse(ContainerDslContext context, Tokens tokens, Archetype archetype)
5454
component.addTags(tags);
5555

5656
component.addProperties(archetype.getProperties());
57+
component.addPerspectives(archetype.getPerspectives());
5758

5859
if (context.hasGroup()) {
5960
component.setGroup(context.getGroup().getName());

structurizr-dsl/src/main/java/com/structurizr/dsl/ContainerArchetypeDslContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ protected String[] getPermittedTokens() {
1313
StructurizrDslTokens.TECHNOLOGY_TOKEN,
1414
StructurizrDslTokens.TAG_TOKEN,
1515
StructurizrDslTokens.TAGS_TOKEN,
16-
StructurizrDslTokens.PROPERTIES_TOKEN
16+
StructurizrDslTokens.PROPERTIES_TOKEN,
17+
StructurizrDslTokens.PERSPECTIVES_TOKEN
1718
};
1819
}
1920

structurizr-dsl/src/main/java/com/structurizr/dsl/ContainerParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Container parse(SoftwareSystemDslContext context, Tokens tokens, Archetype arche
5454
container.addTags(tags);
5555

5656
container.addProperties(archetype.getProperties());
57+
container.addPerspectives(archetype.getPerspectives());
5758

5859
if (context.hasGroup()) {
5960
container.setGroup(context.getGroup().getName());

structurizr-dsl/src/main/java/com/structurizr/dsl/DeploymentNodeArchetypeDslContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ protected String[] getPermittedTokens() {
1313
StructurizrDslTokens.TECHNOLOGY_TOKEN,
1414
StructurizrDslTokens.TAG_TOKEN,
1515
StructurizrDslTokens.TAGS_TOKEN,
16-
StructurizrDslTokens.PROPERTIES_TOKEN
16+
StructurizrDslTokens.PROPERTIES_TOKEN,
17+
StructurizrDslTokens.PERSPECTIVES_TOKEN
1718
};
1819
}
1920

0 commit comments

Comments
 (0)