Skip to content

Commit

Permalink
WIP
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 8, 2020
1 parent 30904b5 commit 3f4cd87
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 136 deletions.
Expand Up @@ -20,10 +20,10 @@ STRING_DOUBLEQUOTE: DQOUTE ((ESC DQOUTE) | ~[\n"])* DQOUTE;
//statement : SEP* identifier SEP* (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (statement)* SEP* RIGHT_BRACE SEP*) SEP*;
itemBody: identifier SEP* value;
item : SEP* itemBody;
value: (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (item | metadata)* SEP* RIGHT_BRACE SEP*) SEP*;
metadata : SEP* '@' itemBody;
item: identifier SEP* value;
dataItem : SEP* item;
infraItem : SEP* '@' item;
value: (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (dataItem | infraItem)* SEP* RIGHT_BRACE SEP*) SEP*;
identifier : (prefix COLON)? localIdentifier;
prefix : IDENTIFIER;
Expand Down
Expand Up @@ -4,7 +4,7 @@

import com.evolveum.axiom.api.schema.AxiomItemDefinition;

public abstract class AbstractAxiomItem<V> implements AxiomItem<V> {
public abstract class AbstractAxiomItem<T extends AxiomValue<?>> implements AxiomItem<T> {


private final AxiomItemDefinition definition;
Expand Down
Expand Up @@ -21,7 +21,7 @@ default Optional<AxiomItem<?>> item(AxiomItemDefinition def) {
}

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

Expand Down
13 changes: 7 additions & 6 deletions infra/axiom/src/main/java/com/evolveum/axiom/api/AxiomItem.java
Expand Up @@ -7,26 +7,27 @@
import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.google.common.collect.Iterables;

public interface AxiomItem<V> {
public interface AxiomItem<T extends AxiomValue<?>> {

AxiomName name();
Optional<AxiomItemDefinition> definition();

Collection<AxiomValue<V>> values();
Collection<T> values();

default AxiomValue<V> onlyValue() {
default T onlyValue() {
return Iterables.getOnlyElement(values());
}

/*
static <V> AxiomItem<V> of(AxiomItemDefinition def, V value) {
return CompactAxiomItem.of(def, value);
}
}*/

static <V> AxiomItem<V> from(AxiomItemDefinition def, Collection<? extends AxiomValue<V>> values) {
static <T extends AxiomValue<?>> AxiomItem<T> from(AxiomItemDefinition def, Collection<? extends T> values) {
return AxiomItemImpl.from(def, values);
}

static <V> AxiomItem<V> from(AxiomItemDefinition def, AxiomValue<V> value) {
static <T extends AxiomValue<?>> AxiomItem<T> from(AxiomItemDefinition def, T value) {
return AxiomItemImpl.from(def, Collections.singleton(value));
}

Expand Down
Expand Up @@ -8,9 +8,9 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;

public class AxiomItemBuilder<V> implements Supplier<AxiomItem<V>> {
public class AxiomItemBuilder<V extends AxiomValue<?>> implements Supplier<AxiomItem<V>> {

Collection<Supplier<? extends AxiomValue<V>>> values = new ArrayList<>();
Collection<Supplier<? extends V>> values = new ArrayList<>();
private AxiomItemDefinition definition;

public AxiomItemBuilder(AxiomItemDefinition definition) {
Expand All @@ -21,18 +21,17 @@ public AxiomItemDefinition definition() {
return definition;
}

public void addValue(Supplier<? extends AxiomValue<V>> value) {
public void addValue(Supplier<? extends V> value) {
values.add(value);
}

@Override
public AxiomItem<V> get() {
Builder<AxiomValue<V>> result = ImmutableList.builder();
for(Supplier<? extends AxiomValue<V>> value : values) {
Builder<V> result = ImmutableList.builder();
for(Supplier<? extends V> value : values) {
result.add(value.get());
}
return AxiomItem.from(definition, result.build());
}


}
Expand Up @@ -4,8 +4,8 @@

import com.evolveum.axiom.api.schema.AxiomItemDefinition;

public interface AxiomItemFactory<V> {
public interface AxiomItemFactory<V extends AxiomValue<?>> {

AxiomItem<V> create(AxiomItemDefinition def, Collection<? extends AxiomValue<?>> axiomItem);
AxiomItem<V> create(AxiomItemDefinition def, Collection<? extends V> axiomItem);

}
Expand Up @@ -5,22 +5,22 @@
import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.google.common.collect.ImmutableList;

class AxiomItemImpl<V> extends AbstractAxiomItem<V> {
class AxiomItemImpl<T extends AxiomValue<?>> extends AbstractAxiomItem<T> {

Collection<AxiomValue<V>> values;
Collection<T> values;


private AxiomItemImpl(AxiomItemDefinition definition, Collection<? extends AxiomValue<V>> val) {
private AxiomItemImpl(AxiomItemDefinition definition, Collection<? extends T> val) {
super(definition);
this.values = ImmutableList.copyOf(val);
}

static <V> AxiomItem<V> from(AxiomItemDefinition definition, Collection<? extends AxiomValue<V>> values) {
static <T extends AxiomValue<?>> AxiomItem<T> from(AxiomItemDefinition definition, Collection<? extends T> values) {
return new AxiomItemImpl<>(definition, values);
}

@Override
public Collection<AxiomValue<V>> values() {
public Collection<T> values() {
return values;
}

Expand Down
34 changes: 27 additions & 7 deletions infra/axiom/src/main/java/com/evolveum/axiom/api/AxiomValue.java
@@ -1,28 +1,48 @@
package com.evolveum.axiom.api;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

import com.evolveum.axiom.api.schema.AxiomTypeDefinition;


public interface AxiomValue<V> {

Optional<AxiomTypeDefinition> type();

default Collection<AxiomItem<?>> metadata() {
return Collections.emptyList();
}
AxiomName TYPE = AxiomName.axiom("type");
AxiomName VALUE = AxiomName.axiom("value");

Optional<AxiomTypeDefinition> type();

V value();

default <C> C value(Class<C> type) {
V value = value();
if(type.isInstance(value)) {
return type.cast(value);
}
return null;
}

default Optional<AxiomComplexValue> asComplex() {
if(this instanceof AxiomComplexValue) {
return Optional.of((AxiomComplexValue) this);
}
return Optional.empty();
}

default Optional<AxiomItem<?>> infraItem(AxiomName name) {
return Optional.empty();
}

interface InfraFactory<V, T extends AxiomValue<V>> {

default T create(Map<AxiomName, AxiomItem<?>> infraItems) {
AxiomTypeDefinition type = infraItems.get(TYPE).onlyValue().value(AxiomTypeDefinition.class);
V value = (V) infraItems.get(VALUE).onlyValue().value();
return create(type, value, infraItems);
}

T create(AxiomTypeDefinition valueType, V value, Map<AxiomName,AxiomItem<?>> infraItems);
}

}
Expand Up @@ -15,23 +15,30 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public class AxiomValueBuilder<V,T extends AxiomValue<V>> implements Lazy.Supplier<T> {
public class AxiomValueBuilder<V,T extends AxiomValue<V>> extends AxiomValueInfraBuilder<V, T> implements Lazy.Supplier<T> {

private final AxiomTypeDefinition type;

private AxiomValueFactory<V,T> factory;
private Map<AxiomName, Supplier<? extends AxiomItem<?>>> children = new LinkedHashMap<>();
private Map<AxiomName, Supplier<? extends AxiomItem<?>>> dataItems = new LinkedHashMap<>();
private V value;

public AxiomValueBuilder(AxiomTypeDefinition type, AxiomValueFactory<V,T> factory) {
super(null);
this.type = type;
this.factory = factory;
}

public static <V,T extends AxiomValue<V>> AxiomValueBuilder<V, T> from(AxiomTypeDefinition type, AxiomValue.InfraFactory<V, T> factory) {
return new AxiomValueBuilder(type, type.isComplex() ? ItemValueImpl.factory() : SimpleValue.factory());
}


public static <V> AxiomValueBuilder<V, AxiomValue<V>> from(AxiomTypeDefinition type) {
return new AxiomValueBuilder(type, type.isComplex() ? ItemValueImpl.factory() : SimpleValue.factory());
}


public V getValue() {
return value;
}
Expand All @@ -41,28 +48,28 @@ public void setValue(V value) {
}

public void add(AxiomName name, Supplier<? extends AxiomItem<?>> child) {
children.put(name, child);
dataItems.put(name, child);
}

public Supplier<? extends AxiomItem<?>> get(AxiomName name) {
return children.get(name);
return dataItems.get(name);
}

public Supplier<? extends AxiomItem<?>> get(AxiomName name, Function<AxiomName, ? extends Supplier<? extends AxiomItem<?>>> child) {
return children.computeIfAbsent(name, child);
return dataItems.computeIfAbsent(name, child);
}

@Override
public T get() {
if(type.isComplex()) {
Builder<AxiomName, AxiomItem<?>> builder = ImmutableMap.builder();
for(Entry<AxiomName, Supplier<? extends AxiomItem<?>>> entry : children.entrySet()) {
for(Entry<AxiomName, Supplier<? extends AxiomItem<?>>> entry : dataItems.entrySet()) {
AxiomItem<?> item = entry.getValue().get();
builder.put(entry.getKey(), entry.getValue().get());
}
return factory.create(type, null, builder.build());
}
Preconditions.checkState(children.isEmpty(), "%s does not have items. Items found %s", type.name(), children.keySet());
Preconditions.checkState(dataItems.isEmpty(), "%s does not have items. Items found %s", type.name(), dataItems.keySet());
return factory.create(type, value, Collections.emptyMap());
}

Expand Down
@@ -0,0 +1,49 @@
package com.evolveum.axiom.api;

import com.evolveum.axiom.concepts.Lazy;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.function.Supplier;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public class AxiomValueInfraBuilder<V,T extends AxiomValue<V>> implements Lazy.Supplier<T> {

private AxiomValue.InfraFactory<V,T> factory;
private Map<AxiomName, Supplier<? extends AxiomItem<?>>> children = new LinkedHashMap<>();

protected AxiomValueInfraBuilder(AxiomValue.InfraFactory<V,T> factory) {
this.factory = factory;
}

public static <V,T extends AxiomValue<V>> AxiomValueInfraBuilder<V,T> from(AxiomValue.InfraFactory<V,T> type) {
return new AxiomValueInfraBuilder<>(type);
}

public void addInfra(AxiomName name, Supplier<? extends AxiomItem<?>> child) {
children.put(name, child);
}

public Supplier<? extends AxiomItem<?>> getInfra(AxiomName name) {
return children.get(name);
}

public Supplier<? extends AxiomItem<?>> getInfra(AxiomName name, Function<AxiomName, ? extends Supplier<? extends AxiomItem<?>>> child) {
return children.computeIfAbsent(name, child);
}

@Override
public T get() {
Builder<AxiomName, AxiomItem<?>> builder = ImmutableMap.builder();
for(Entry<AxiomName, Supplier<? extends AxiomItem<?>>> entry : children.entrySet()) {
AxiomItem<?> item = entry.getValue().get();
builder.put(entry.getKey(), entry.getValue().get());
}
return factory.create(builder.build());
}

}

This file was deleted.

0 comments on commit 3f4cd87

Please sign in to comment.