Debian Codegraph @

net/sf/saxon/value/NestedIntegerValue.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.value;
9
10 import net.sf.saxon.expr.sort.AtomicMatchKey;
11 import net.sf.saxon.lib.StringCollator;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.tree.util.FastStringBuffer;
14 import net.sf.saxon.type.AtomicType;
15 import net.sf.saxon.type.BuiltInAtomicType;
16
17 import java.util.Arrays;
18 import java.util.StringTokenizer;
19
20 /**
21 * This class represents a dot-separated sequence of numbers such as 1.12.5, typically
22 * used as a software version number.
23 * <p>Although the class is implemented as an atomic type derived from xs:string, it
24 * has not been integrated into the schema type hierarchy and cannot be used directly
25 * in schema validation.</p>
26 * <p>The class provides "smart" ordering, for example 1 &lt; 1.2 &lt; 1.12 &lt; 1.12.6</p>
27 */
28
29
30 public class NestedIntegerValue extends AtomicValue implements Comparable, AtomicMatchKey {
31
32 public static NestedIntegerValue ONE = new NestedIntegerValue(new int[]{1});
33 public static NestedIntegerValue TWO = new NestedIntegerValue(new int[]{2});
34
35 int[] value;
36
37 public NestedIntegerValue(String v) throws XPathException {
38 typeLabel = BuiltInAtomicType.STRING;
39 parse(v);
40 }
41
42 public NestedIntegerValue(int[] val) {
43 typeLabel = BuiltInAtomicType.STRING;
44 value = val;
45 }
46
47 public static NestedIntegerValue parse(String v) throws XPathException {
48 StringTokenizer st = new StringTokenizer(v, ".");
49 int[] valuei = new int[st.countTokens()];
50 try {
51 for (int i = 0; st.hasMoreTokens(); i++) {
52 valuei[i] = Integer.parseInt(st.nextToken());
53 }
54 } catch (NumberFormatException exc) {
55 throw new XPathException("Nested integer value has incorrect format: " + v);
56 }
57 return new NestedIntegerValue(valuei);
58 }
59
60 public NestedIntegerValue append(int leaf) {
61 int[] v = new int[value.length + 1];
62 System.arraycopy(value, 0, v, 0, value.length);
63 v[value.length] = leaf;
64 return new NestedIntegerValue(v);
65 }
66
67 public NestedIntegerValue getStem() {
68 if (value.length == 0) {
69 return null;
70 } else {
71 int[] v = new int[value.length - 1];
72 System.arraycopy(value, 0, v, 0, v.length);
73 return new NestedIntegerValue(v);
74 }
75 }
76
77 public int getDepth() {
78 return value.length;
79 }
80
81 public int getLeaf() {
82 if (value.length == 0) {
83 return -1;
84 } else {
85 return value[value.length-1];
86 }
87 }
88
89
90 @Override
91 public Comparable getSchemaComparable() {
92 return this;
93 }
94
95 @Override
96 public AtomicMatchKey getXPathComparable(boolean ordered, StringCollator collator, int implicitTimezone) {
97 return this;
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 return (o instanceof NestedIntegerValue) && Arrays.equals(value, ((NestedIntegerValue) o).value);
103 }
104
105 @Override
106 public BuiltInAtomicType getPrimitiveType() {
107 return BuiltInAtomicType.STRING;
108 }
109
110 @Override
111 public AtomicValue copyAsSubType(AtomicType typeLabel) {
112 NestedIntegerValue v = new NestedIntegerValue(value);
113 v.typeLabel = typeLabel;
114 return v;
115 }
116
117 @Override
118 protected CharSequence getPrimitiveStringValue() {
119 FastStringBuffer buffer = new FastStringBuffer(value.length * 2);
120 for (int i = 0; i < value.length - 1; i++) {
121 buffer.append(value[i] + ".");
122 }
123 buffer.append(value[value.length - 1] + "");
124 return buffer;
125 }
126
127
128 public int compareTo(Object other) {
129 if (!(other instanceof NestedIntegerValue)) {
130 throw new ClassCastException("NestedIntegerValue is not comparable to " + other.getClass());
131 } else {
132 NestedIntegerValue v2 = (NestedIntegerValue) other;
133 for (int i = 0; i < value.length && i < v2.value.length; i++) {
134 if (value[i] != v2.value[i]) {
135 if (value[i] < v2.value[i]) {
136 return -1;
137 } else {
138 return 1;
139 }
140 }
141 }
142 return Integer.signum(value.length - v2.value.length);
143 }
144 }
145
146 }