ZorbaXQSequenceType.java
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2012 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.zorbaxquery.api.xqj;
17 
18 import javax.xml.xquery.XQItemType;
19 
20  /**
21  * The ZorbaXQSequenceType interface represents a sequence type as XQuery 1.0: An XML Query language. The ZorbaXQSequenceType is the base interface for the XQItemType interface and contains an occurence indicator.
22  *
23  */
24 public class ZorbaXQSequenceType implements javax.xml.xquery.XQSequenceType {
25 
26  XQItemType type;
27  int occurence;
28 
29  public ZorbaXQSequenceType(XQItemType type, int occurence) {
30  this.type = type;
31  this.occurence = occurence;
32  }
33 
34  /** \brief Returns the type of the item in the sequence type.
35  *
36  * @return XQItemType representing the item type in the sequence. null is returned in case of an empty sequence.
37  */
38  @Override
39  public XQItemType getItemType() {
40  return type;
41  }
42 
43  /** \brief Returns the occurrence indicator for the sequence type.
44  *
45  * One of:
46  *
47  * Description Value
48  * Zero or one OCC_ZERO_OR_ONE
49  * Exactly one OCC_EXACTLY_ONE
50  * Zero or more OCC_ZERO_OR_MORE
51  * One or more OCC_ONE_OR_MORE
52  * Empty OCC_EMPTY
53  *
54  * @return int indicating the occurrence indicator
55  */
56  @Override
57  public int getItemOccurrence() {
58  return occurence;
59  }
60 
61  /** \brief Returns a human-readable implementation-defined string representation of the sequence type.
62  *
63  * @return a String representation of the sequence type
64  */
65  @Override
66  public String toString() {
67  StringBuffer result = new StringBuffer();
68 
69  try {
70 
71  result.append(type.toString());
72 
73  for (java.lang.reflect.Field field: ZorbaXQSequenceType.class.getFields()) {
74  if (field.getName().startsWith("OCC_")) {
75  if (field.getInt(this)==occurence) {
76  result.append("Ocurrence: ").append(field.getName().substring(4));
77  }
78  }
79  }
80  } catch (Exception e){
81  result.append("Sequence Type error: ").append(e.getLocalizedMessage());
82  }
83 
84  return result.toString();
85  }
86 
87  /** \brief Compares the specified object with this sequence type for equality.
88  *
89  * The result is true only if the argument is a sequence type object which represents the same XQuery sequence type.
90  *
91  * In order to comply with the general contract of equals and hashCode across different implementations the following algorithm must be used. Return true if and only if both objects are XQsequenceType and:
92  * - getItemOccurrence() is equal
93  * - if not OCC_EMPTY, getItemType() is equal
94  *
95  * @param o - an XQItemType object representing an XQuery sequence type
96  * @return true if the input item type object represents the same XQuery sequence type, false otherwise
97  */
98  @Override
99  public boolean equals(Object o) {
100  boolean result = false;
101  if (o instanceof ZorbaXQSequenceType) {
102  result = (occurence != ZorbaXQSequenceType.OCC_EMPTY) &&
103  (type.equals(((ZorbaXQSequenceType)o).getItemType())) &&
104  (occurence == ((ZorbaXQSequenceType)o).getItemOccurrence());
105  }
106  return result;
107  }
108 
109  /** \brief Returns a hash code consistent with the definition of the equals method.
110  *
111  * In order to comply with the general contract of equals and hashCode across different implementations the following algorithm must be used:
112  * \code{.java}
113  * int hashCode;
114  * if (getItemOccurrence() == ZorbaXQSequenceType.OCC_EMPTY)
115  * {
116  * hashCode = 1;
117  * }
118  * else
119  * {
120  * hashCode = getItemOccurrence()*31 + getItemType().hashCode();
121  * }
122  * \endcode
123  *
124  * @return hash code for this item type
125  */
126  @Override
127  public int hashCode() {
128  int hashCode;
129  if (occurence == ZorbaXQSequenceType.OCC_EMPTY) {
130  hashCode = 1;
131  } else {
132  hashCode = occurence * 31 + type.hashCode();
133  }
134  return hashCode;
135  }
136 
137 }
boolean equals(Object o)
Compares the specified object with this sequence type for equality.
String toString()
Returns a human-readable implementation-defined string representation of the sequence type...
The ZorbaXQSequenceType interface represents a sequence type as XQuery 1.0: An XML Query language...
int hashCode()
Returns a hash code consistent with the definition of the equals method.
int getItemOccurrence()
Returns the occurrence indicator for the sequence type.
XQItemType getItemType()
Returns the type of the item in the sequence type.
ZorbaXQSequenceType(XQItemType type, int occurence)