View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.metapath.function;
28  
29  import gov.nist.secauto.metaschema.core.metapath.IExpression;
30  import gov.nist.secauto.metaschema.core.metapath.item.IItem;
31  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
32  
33  import java.util.Objects;
34  
35  import edu.umd.cs.findbugs.annotations.NonNull;
36  
37  public interface IArgument {
38    @NonNull
39    String getName();
40  
41    @NonNull
42    ISequenceType getSequenceType();
43  
44    boolean isSupported(IExpression expression);
45  
46    @NonNull
47    String toSignature();
48  
49    @NonNull
50    static Builder newBuilder() {
51      return new Builder();
52    }
53  
54    class Builder {
55      private String name;
56      private Class<? extends IItem> type = IItem.class;
57      private Occurrence occurrence = Occurrence.ONE;
58  
59      public Builder() {
60        // construct a new non-initialized builder
61      }
62  
63      public Builder(@NonNull String name) {
64        this.name = name;
65      }
66  
67      @NonNull
68      public Builder name(@NonNull String name) {
69        Objects.requireNonNull(name, "name");
70        if (name.isBlank()) {
71          throw new IllegalArgumentException("the name must be non-blank");
72        }
73        this.name = name.trim();
74        return this;
75      }
76  
77      @NonNull
78      public Builder type(@NonNull Class<? extends IItem> type) {
79        Objects.requireNonNull(type, "type");
80        this.type = type;
81        return this;
82      }
83  
84      @NonNull
85      public Builder zeroOrOne() {
86        return occurrence(Occurrence.ZERO_OR_ONE);
87      }
88  
89      @NonNull
90      public Builder one() {
91        return occurrence(Occurrence.ONE);
92      }
93  
94      @NonNull
95      public Builder zeroOrMore() {
96        return occurrence(Occurrence.ZERO_OR_MORE);
97      }
98  
99      @NonNull
100     public Builder oneOrMore() {
101       return occurrence(Occurrence.ONE_OR_MORE);
102     }
103 
104     @NonNull
105     public Builder occurrence(@NonNull Occurrence occurrence) {
106       Objects.requireNonNull(occurrence, "occurrence");
107       this.occurrence = occurrence;
108       return this;
109     }
110 
111     @NonNull
112     public IArgument build() {
113       return new ArgumentImpl(
114           ObjectUtils.requireNonNull(name, "the name must not be null"),
115           new SequenceTypeImpl(type, occurrence));
116     }
117 
118   }
119 
120 }