001    package com.thaiopensource.relaxng.impl;
002    
003    class ChoicePattern extends BinaryPattern {
004      ChoicePattern(Pattern p1, Pattern p2) {
005        super(p1.isNullable() || p2.isNullable(),
006              combineHashCode(CHOICE_HASH_CODE, p1.hashCode(), p2.hashCode()),
007              p1,
008              p2);
009      }
010      Pattern expand(SchemaPatternBuilder b) {
011        Pattern ep1 = p1.expand(b);
012        Pattern ep2 = p2.expand(b);
013        if (ep1 != p1 || ep2 != p2)
014          return b.makeChoice(ep1, ep2);
015        else
016          return this;
017      }
018    
019      boolean containsChoice(Pattern p) {
020        return p1.containsChoice(p) || p2.containsChoice(p);
021      }
022    
023      void accept(PatternVisitor visitor) {
024        visitor.visitChoice(p1, p2);
025      }
026    
027      Object apply(PatternFunction f) {
028        return f.caseChoice(this);
029      }
030    
031      void checkRestrictions(int context, DuplicateAttributeDetector dad, Alphabet alpha)
032        throws RestrictionViolationException {
033        if (dad != null)
034          dad.startChoice();
035        p1.checkRestrictions(context, dad, alpha);
036        if (dad != null)
037          dad.alternative();
038        p2.checkRestrictions(context, dad, alpha);
039        if (dad != null)
040          dad.endChoice();
041      }
042    
043    }
044