All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyapi.plugin.model.generation.Context Maven / Gradle / Ivy

There is a newer version: 0.15.3
Show newest version
package io.polyapi.plugin.model.generation;

import static io.polyapi.plugin.utils.StringUtils.toPascalCase;
import static java.lang.String.format;
import static java.util.function.Predicate.isEqual;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import io.polyapi.plugin.model.specification.Specification;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Context {
    private String name;
    private Context parent;
    private List subcontexts = new ArrayList<>();
    private Set specifications = new HashSet<>();

    public Context(Context parent, String name) {
        this.parent = parent;
        this.name = name;
    }

    public String getPackageName() {
        return Optional.ofNullable(parent).map(parentContext -> format("%s.context.%s", parentContext.getPackageName(), parentContext.getName().toLowerCase())).orElse("io.polyapi");
    }

    public String getClassName() {
        return toPascalCase(name);
    }

    public Context put(Context context) {
        return subcontexts.stream()
                .filter(isEqual(context))
                .findAny()
                .orElseGet(() -> {
                    subcontexts.add(context);
                    return context;
                });
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Context that)) return false;
        return name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy