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

cdc.applic.dictionaries.edit.EnRegistry Maven / Gradle / Ivy

There is a newer version: 0.13.3
Show newest version
package cdc.applic.dictionaries.edit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cdc.applic.expressions.literals.SName;
import cdc.util.lang.Introspection;

public final class EnRegistry extends EnDictionary {
    public static final String KIND = "Registry";
    private SName prefix;
    private final List>> parentRefs = new ArrayList<>();

    protected EnRegistry(Builder builder) {
        super(builder);
        this.prefix = builder.prefix;
        for (final String parentId : builder.parentIds) {
            this.parentRefs.add(EnRef.of(this, Introspection.uncheckedCast(EnDictionary.class), parentId));
        }
        addToOwner();
    }

    @Override
    public String getKind() {
        return KIND;
    }

    @Override
    public EnRef getRef() {
        return getRef(EnRegistry.class);
    }

    @Override
    public EnRegistry getRegistry() {
        return this;
    }

    @Override
    public List> getParents(boolean fail) {
        return EnRef.resolve(getParentRefs(), fail);
    }

    public final void setPrefix(SName prefix) {
        this.prefix = prefix;

        fireSemanticChange(EnNames.PREFIX);
    }

    public final void setPrefix(String prefix) {
        setPrefix(SName.of(prefix));
    }

    public final SName getPrefix() {
        return prefix;
    }

    public void setParents(List> parents) {
        this.parentRefs.clear();
        for (final EnDictionary parent : parents) {
            this.parentRefs.add(EnRef.of(this, Introspection.uncheckedCast(EnDictionary.class), parent));
        }

        fireSemanticChange(EnNames.PARENTS);
    }

    public void setParents(EnDictionary... parents) {
        setParents(List.of(parents));
    }

    public List>> getParentRefs() {
        return Collections.unmodifiableList(parentRefs);
    }

    public List getTypes() {
        return getChildren(EnType.class);
    }

    public List getAllTypes(boolean fail) {
        final List list = new ArrayList<>();
        for (final EnDictionary dictionary : getAllAncestors(fail)) {
            if (dictionary instanceof EnRegistry) {
                list.addAll(((EnRegistry) dictionary).getTypes());
            }
        }
        return list;
    }

    public boolean isKnownType(EnType type) {
        // FIXME O(n) complexity
        final List types = getAllTypes(false);
        return types.contains(type);
    }

    public List getNamingConventions() {
        return getChildren(EnNamingConvention.class);
    }

    public EnNamingConvention.Builder namingConvention() {
        return EnNamingConvention.builder(this);
    }

    public List getNamedDItems() {
        return getChildren(EnNamedDItem.class);
    }

    public List getAllNamedDItems(boolean fail) {
        final List list = new ArrayList<>();
        for (final EnDictionary dictionary : getAllAncestors(fail)) {
            if (dictionary instanceof EnRegistry) {
                list.addAll(((EnRegistry) dictionary).getNamedDItems());
            }
        }
        return list;
    }

    public boolean isKnownItem(EnNamedDItem item) {
        // FIXME O(n) complexity
        final List items = getAllNamedDItems(false);
        return items.contains(item);
    }

    public List getProperties() {
        return getChildren(EnProperty.class);
    }

    public List getAliases() {
        return getChildren(EnAlias.class);
    }

    public EnBooleanType.Builder booleanType() {
        return EnBooleanType.builder(this);
    }

    public EnIntegerType.Builder integerType() {
        return EnIntegerType.builder(this);
    }

    public EnRealType.Builder realType() {
        return EnRealType.builder(this);
    }

    public EnEnumeratedType.Builder enumeratedType() {
        return EnEnumeratedType.builder(this);
    }

    public EnPatternType.Builder patternType() {
        return EnPatternType.builder(this);
    }

    public EnProperty.Builder property() {
        return EnProperty.builder(this);
    }

    public EnAlias.Builder alias() {
        return EnAlias.builder(this);
    }

    static Builder builder(EnRepository owner) {
        return new Builder(owner);
    }

    public static final class Builder
            extends EnDictionary.Builder {
        private SName prefix;
        private final List parentIds = new ArrayList<>();

        protected Builder(EnRepository owner) {
            super(owner);
        }

        @Override
        public Class getBuiltClass() {
            return EnRegistry.class;
        }

        public Builder prefix(SName prefix) {
            this.prefix = prefix;
            return self();
        }

        public Builder prefix(String prefix) {
            return prefix(SName.of(prefix));
        }

        public Builder parentIds(List parentIds) {
            this.parentIds.clear();
            this.parentIds.addAll(parentIds);
            return self();
        }

        public Builder parentIds(String... parentIds) {
            return parentIds(List.of(parentIds));
        }

        public Builder parents(List> parents) {
            this.parentIds.clear();
            for (final EnDictionary parent : parents) {
                this.parentIds.add(parent.getId());
            }
            return self();
        }

        public Builder parents(EnDictionary... parents) {
            return parents(List.of(parents));
        }

        @Override
        public EnRegistry build() {
            return wrap(new EnRegistry(this));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy