Skip to content

Commit

Permalink
Axiom: added metadata to AxiomValue
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Tkacik <tony.tkacik@evolveum.com>
  • Loading branch information
tonydamage committed Jun 17, 2020
1 parent c664133 commit 5862bca
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 14 deletions.
Expand Up @@ -20,9 +20,8 @@ default Optional<AxiomItem<?>> item(AxiomItemDefinition def) {
return (Optional) item(def.name());
}

@SuppressWarnings("unchecked")
default <T> Optional<AxiomItem<T>> item(AxiomName name) {
return Optional.ofNullable((AxiomItem<T>) itemMap().get(name));
default Optional<? extends AxiomItem<?>> item(AxiomName name) {
return Optional.ofNullable(itemMap().get(name));
}

default <T> Optional<AxiomValue<T>> onlyValue(Class<T> type, AxiomItemDefinition... components) {
Expand Down
10 changes: 10 additions & 0 deletions infra/axiom/src/main/java/com/evolveum/axiom/api/AxiomValue.java
Expand Up @@ -10,11 +10,21 @@ public interface AxiomValue<V> extends AxiomInfraValue {

AxiomName TYPE = AxiomName.axiom("type");
AxiomName VALUE = AxiomName.axiom("value");
AxiomName METADATA = AxiomName.axiom("metadata");

Optional<AxiomTypeDefinition> type();

V value();


default Optional<? extends AxiomComplexValue> metadata() {
return infraItem(METADATA).flatMap(v -> v.onlyValue().asComplex());
}

default Optional<? extends AxiomItem<?>> metadata(AxiomName name) {
return metadata().flatMap(m -> m.item(name));
}

default Optional<AxiomComplexValue> asComplex() {
if(this instanceof AxiomComplexValue) {
return Optional.of((AxiomComplexValue) this);
Expand Down
Expand Up @@ -6,6 +6,7 @@

import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.evolveum.axiom.api.schema.AxiomTypeDefinition;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;

public class ComplexValueImpl implements AxiomComplexValue {
Expand Down Expand Up @@ -38,8 +39,12 @@ public Optional<AxiomItem<?>> item(AxiomItemDefinition def) {
}

@Override
public <T> Optional<AxiomItem<T>> item(AxiomName name) {
return Optional.ofNullable((AxiomItem<T>) items.get(name));
public Optional<? extends AxiomItem<?>> item(AxiomName name) {
return Optional.ofNullable(items.get(name));
}

protected AxiomItem<?> requireItem(AxiomName name) {
return item(name).orElseThrow(() -> new IllegalStateException(Strings.lenientFormat("Required item %s not present.", name)));
}

public Collection<AxiomItem<?>> items() {
Expand All @@ -55,4 +60,9 @@ public Map<AxiomName, AxiomItem<?>> itemMap() {
public Map<AxiomName, AxiomItem<?>> infraItems() {
return infraItems;
}

@SuppressWarnings("unchecked")
protected <T> Optional<AxiomItem<T>> as(Class<T> type, Optional<? extends AxiomItem<?>> item) {
return (Optional<AxiomItem<T>>) item;
}
}
Expand Up @@ -41,7 +41,7 @@ public Optional<AxiomItem<?>> item(AxiomItemDefinition def) {
}

@Override
public <T> Optional<AxiomItem<T>> item(AxiomName name) {
public Optional<? extends AxiomItem<?>> item(AxiomName name) {
return delegate().asComplex().get().item(name);
}

Expand Down
Expand Up @@ -21,7 +21,7 @@ public AxiomIdentifierDefinitionImpl(AxiomTypeDefinition axiomItemDefinition, Ma
super(axiomItemDefinition, items, infraItems);

ImmutableList.Builder<AxiomName> components = ImmutableList.builder();
for (AxiomValue<AxiomName> val : this.<AxiomName>item(Item.ID_MEMBER.name()).get().values()) {
for (AxiomValue<AxiomName> val : as(AxiomName.class,item(Item.ID_MEMBER.name())).get().values()) {
components.add(val.value());
}
this.components = components.build();
Expand Down
Expand Up @@ -24,8 +24,8 @@ public AxiomItemDefinitionImpl(AxiomTypeDefinition axiomItemDefinition, Map<Axio
super(axiomItemDefinition, items, infraItems);
this.valueType = require(asComplex().get().onlyValue(AxiomTypeDefinition.class,Item.TYPE_REFERENCE, Item.REF_TARGET));
this.identifierDef = asComplex().get().onlyValue(AxiomIdentifierDefinition.class, Item.IDENTIFIER_DEFINITION).map(v -> AxiomIdentifierDefinitionImpl.from(v));
minOccurs = this.<String>item(Item.MIN_OCCURS.name());
substitutionOf = this.<AxiomName>item(Item.SUBSTITUTION_OF.name()).map(v -> v.onlyValue().value());
minOccurs = as(String.class,item(Item.MIN_OCCURS.name()));
substitutionOf = as(AxiomName.class, item(Item.SUBSTITUTION_OF.name())).map(v -> v.onlyValue().value());
}

@Override
Expand Down
Expand Up @@ -30,16 +30,15 @@ public AxiomTypeDefinitionImpl(AxiomTypeDefinition def, Map<AxiomName, AxiomItem
super(def, keywordMap, infraItems);

ImmutableMap.Builder<AxiomName, AxiomItemDefinition> builder = ImmutableMap.builder();
Optional<AxiomItem<AxiomItemDefinition>> itemDef = item(Item.ITEM_DEFINITION.name());
Optional<AxiomItem<AxiomItemDefinition>> itemDef = as(AxiomItemDefinition.class, item(Item.ITEM_DEFINITION.name()));
if(itemDef.isPresent()) {
supplyAll(name(),builder, itemDef.get().values());
}
itemDefinitions = builder.build();

superType = onlyValue(AxiomTypeDefinition.class,Item.SUPERTYPE_REFERENCE, Item.REF_TARGET).map(v -> from(v.asComplex().get()));


argument = this.<AxiomName>item(Item.ARGUMENT.name()).flatMap(v -> itemDefinition(v.onlyValue().value()));
argument = as(AxiomName.class,item(Item.ARGUMENT.name())).flatMap(v -> itemDefinition(v.onlyValue().value()));
identifiers = Collections2.transform((this.item(Item.IDENTIFIER_DEFINITION).map(v -> v.values()).orElse(Collections.emptyList())),
AxiomIdentifierDefinitionImpl::from);
}
Expand All @@ -52,7 +51,7 @@ public static AxiomTypeDefinition from(AxiomComplexValue value) {
}

@Override
public <V> Optional<AxiomItem<V>> item(AxiomName name) {
public Optional<? extends AxiomItem<?>> item(AxiomName name) {
return super.item(name);
}

Expand Down
Expand Up @@ -87,7 +87,7 @@ public void axiomTestLanguageExtension() throws IOException, AxiomSyntaxExceptio
Optional<AxiomTypeDefinition> personDef = schemaContext.getType(PERSON);
assertTrue(personDef.isPresent());

AxiomItem<Object> extension = personDef.get().asComplex().get().item(STORAGE).get();
AxiomItem<?> extension = personDef.get().asComplex().get().item(STORAGE).get();

assertFalse(extension.values().isEmpty(), "Extension statements should be available.");
assertEquals(2, personDef.get().itemDefinitions().entrySet().size());
Expand Down

0 comments on commit 5862bca

Please sign in to comment.