-
Notifications
You must be signed in to change notification settings - Fork 188
/
AxiomTypeDefinition.java
86 lines (67 loc) · 2.82 KB
/
AxiomTypeDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.axiom.api.schema;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.api.AxiomValue;
import com.evolveum.axiom.api.meta.Inheritance;
import com.evolveum.axiom.lang.api.IdentifierSpaceKey;
import com.google.common.collect.ImmutableMap;
public interface AxiomTypeDefinition extends AxiomNamedDefinition, AxiomValue<AxiomTypeDefinition> {
public final AxiomIdentifier IDENTIFIER_MEMBER = AxiomIdentifier.axiom("name");
public final AxiomIdentifier SPACE = AxiomIdentifier.axiom("AxiomTypeDefinition");
@Override
default AxiomTypeDefinition get() {
return this;
}
@Override
default Optional<AxiomTypeDefinition> type() {
return Optional.empty();
}
Optional<AxiomItemDefinition> argument();
Optional<AxiomTypeDefinition> superType();
Map<AxiomIdentifier, AxiomItemDefinition> itemDefinitions();
Collection<AxiomIdentifierDefinition> identifierDefinitions();
default Optional<AxiomItemDefinition> itemDefinition(AxiomIdentifier child) {
AxiomItemDefinition maybe = itemDefinitions().get(child);
if(maybe == null) {
maybe = itemDefinitions().get(Inheritance.adapt(name(), child));
}
if(maybe == null && child.namespace().isEmpty()) {
maybe = itemDefinitions().get(name().localName(child.localName()));
}
return Optional.ofNullable(maybe).or(() -> superType().flatMap(s -> s.itemDefinition(child)));
}
static IdentifierSpaceKey identifier(AxiomIdentifier name) {
return IdentifierSpaceKey.from(ImmutableMap.of(IDENTIFIER_MEMBER, name));
}
default Collection<AxiomItemDefinition> requiredItems() {
return itemDefinitions().values().stream().filter(AxiomItemDefinition::required).collect(Collectors.toList());
}
default Optional<AxiomItemDefinition> itemDefinition(AxiomIdentifier parentItem, AxiomIdentifier name) {
return itemDefinition(Inheritance.adapt(parentItem, name));
}
default boolean isSubtypeOf(AxiomTypeDefinition type) {
return isSubtypeOf(type.name());
}
default boolean isSupertypeOf(AxiomTypeDefinition other) {
return other.isSubtypeOf(this);
}
default boolean isSubtypeOf(AxiomIdentifier other) {
Optional<AxiomTypeDefinition> current = Optional.of(this);
while(current.isPresent()) {
if(current.get().name().equals(other)) {
return true;
}
current = current.get().superType();
}
return false;
}
}