001package org.clafer.ast;
002
003import org.clafer.common.Check;
004
005/**
006 * A concrete Clafer.
007 *
008 * @author jimmy
009 */
010public class AstConcreteClafer extends AstClafer {
011
012    private final AstClafer parent;
013    private Card card = new Card();
014
015    AstConcreteClafer(String name, AstAbstractClafer claferClafer) {
016        super(name, claferClafer);
017        this.parent = null;
018    }
019
020    AstConcreteClafer(String name, AstClafer parent, AstAbstractClafer claferClafer) {
021        super(name, claferClafer);
022        this.parent = Check.notNull(parent);
023    }
024
025    /**
026     * Checks if this Clafer has a parent. Every Clafer has a parent except for
027     * the special Clafer at the root of the hierarchy.
028     *
029     * @return {@code true} if and only if this Clafer has a parent,
030     * {@code false} otherwise
031     */
032    public boolean hasParent() {
033        return parent != null;
034    }
035
036    /**
037     * Returns this Clafer's parent.
038     *
039     * @return this Clafer's parent
040     */
041    public AstClafer getParent() {
042        return parent;
043    }
044
045    /**
046     * Returns this Clafer's cardinality.
047     *
048     * @return this Clafer's cardinality
049     */
050    public Card getCard() {
051        return card;
052    }
053
054    /**
055     * Set this Clafer's cardinality.
056     *
057     * @param card the cardinality
058     * @return this Clafer
059     */
060    public AstConcreteClafer withCard(Card card) {
061        this.card = Check.notNull(card);
062        return this;
063    }
064
065    /**
066     * Set this Clafer's low cardinality and set the high cardinality to
067     * unbounded.
068     *
069     * @param low the low group cardinality
070     * @return this Clafer
071     */
072    public AstConcreteClafer withCard(int low) {
073        return withCard(new Card(low));
074    }
075
076    /**
077     * Set this Clafer's cardinality.
078     *
079     * @param low the low cardinality
080     * @param high the high cardinality
081     * @return this Clafer
082     */
083    public AstConcreteClafer withCard(int low, int high) {
084        return withCard(new Card(low, high));
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public AstConcreteClafer withGroupCard(Card groupCard) {
092        super.withGroupCard(groupCard);
093        return this;
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public AstConcreteClafer withGroupCard(int low) {
101        return withGroupCard(new Card(low));
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public AstConcreteClafer withGroupCard(int low, int high) {
109        return withGroupCard(new Card(low, high));
110    }
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    public AstConcreteClafer extending(AstAbstractClafer superClafer) {
117        super.extending(superClafer);
118        return this;
119    }
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public AstConcreteClafer refTo(AstClafer targetType) {
126        super.refTo(targetType);
127        return this;
128    }
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public AstConcreteClafer refToUnique(AstClafer targetType) {
135        super.refToUnique(targetType);
136        return this;
137    }
138}