Debian Codegraph @

net/sf/saxon/query/Annotation.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.query;
9
10 import net.sf.saxon.lib.NamespaceConstant;
11 import net.sf.saxon.om.StructuredQName;
12 import net.sf.saxon.value.AtomicValue;
13 import net.sf.saxon.value.NumericValue;
14 import net.sf.saxon.value.StringValue;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 /**
20 * This class represents an annotation that appears in a function or variable declarations
21 */
22 public class Annotation {
23
24 public static final StructuredQName UPDATING = new StructuredQName("", NamespaceConstant.XQUERY, "updating");
25 public static final StructuredQName SIMPLE = new StructuredQName("", NamespaceConstant.XQUERY, "simple");
26 public static final StructuredQName PRIVATE = new StructuredQName("", NamespaceConstant.XQUERY, "private");
27 public static final StructuredQName PUBLIC = new StructuredQName("", NamespaceConstant.XQUERY, "public");
28
29
30 // The name of the annotation
31 private StructuredQName qName = null;
32
33 // The list of parameters (all strings or numbers) associated with the annotation
34 private List<AtomicValue> annotationParameters = null;
35
36 /**
37 * Create an annotation
38 *
39 * @param name the annotation name (a QName)
40 */
41
42 public Annotation(StructuredQName name) {
43 this.qName = name;
44 }
45
46 /**
47 * Get the name of the annotation (a QName)
48 *
49 * @return the annotation name
50 */
51
52 public StructuredQName getAnnotationQName() {
53 return qName;
54 }
55
56 /**
57 * Add a value to the list of annotation parameters
58 *
59 * @param value the value to be added. This will always be a string or number,
60 * but Saxon enforces this only at the level of the query parser
61 */
62
63 public void addAnnotationParameter(AtomicValue value) {
64 if (annotationParameters == null) {
65 annotationParameters = new ArrayList<>();
66 }
67 annotationParameters.add(value);
68 }
69
70 /**
71 * Get the list of annotation parameters
72 *
73 * @return the list of parameters
74 */
75
76 public List<AtomicValue> getAnnotationParameters() {
77 if (annotationParameters == null) {
78 annotationParameters = new ArrayList<>();
79 }
80 return annotationParameters;
81 }
82
83 public boolean equals(Object other) {
84 if (!(other instanceof Annotation &&
85 qName.equals(((Annotation) other).qName) &&
86 getAnnotationParameters().size() == ((Annotation) other).getAnnotationParameters().size())) {
87 return false;
88 }
89 for (int i = 0; i < annotationParameters.size(); i++) {
90 if (!annotationParamEqual(annotationParameters.get(i), ((Annotation) other).annotationParameters.get(i))) {
91 return false;
92 }
93 }
94 return true;
95 }
96
97 private static boolean annotationParamEqual(AtomicValue a, AtomicValue b) {
98 if (a instanceof StringValue && b instanceof StringValue) {
99 return a.getStringValue().equals(b.getStringValue());
100 } else if (a instanceof NumericValue && b instanceof NumericValue) {
101 return ((NumericValue) a).getDoubleValue() == ((NumericValue) b).getDoubleValue();
102 } else {
103 return false;
104 }
105 }
106
107
108 public int hashCode() {
109 return qName.hashCode() ^ annotationParameters.hashCode();
110 }
111
112
113 }
114