com.github.davidmoten.oas3.internal.model.Model Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-to-plantuml Show documentation
Show all versions of openapi-to-plantuml Show documentation
Generates PlantUML file from an OpenAPI 3.0 Definition
package com.github.davidmoten.oas3.internal.model;
import static java.util.stream.Collectors.joining;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import com.github.davidmoten.guavamini.Lists;
public final class Model {
public static final Model EMPTY = new Model(Collections.emptyList(), Collections.emptyList());
private final List classes;
private final List extends Relationship> relationships;
private final Map map;
public Model(List classes, List extends Relationship> relationships) {
this.classes = classes;
this.relationships = relationships;
this.map = classes.stream().collect(Collectors.toMap(c -> c.name(), c -> c, (a, b) -> a));
}
public Model(Class cls, Relationship r) {
this(Collections.singletonList(cls), Collections.singletonList(r));
}
public Model(Class cls) {
this(Collections.singletonList(cls), Collections.emptyList());
}
public Model(Relationship r) {
this(Collections.emptyList(), Collections.singletonList(r));
}
public Model add(Model model) {
List c = Lists.newArrayList(classes);
List r = Lists.newArrayList(relationships);
c.addAll(model.classes);
r.addAll(model.relationships);
return new Model(c, r);
}
public Model add(Relationship r) {
List list = new ArrayList<>(relationships);
list.add(r);
return new Model(classes, list);
}
public List classes() {
return classes;
}
public List extends Relationship> relationships() {
return relationships;
}
public Optional cls(String name) {
return Optional.ofNullable(map.get(name));
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("Model [");
b.append(classes.stream().map(x -> "\n " + x).collect(joining(",")));
b.append(relationships.stream().map(x -> "\n " + x).collect(joining(",")));
b.append("\n]");
return b.toString();
}
}