Mercurial > hg > monetdb-java
comparison src/main/java/nl/cwi/monetdb/mcl/parser/MCLParser.java @ 0:a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
author | Sjoerd Mullender <sjoerd@acm.org> |
---|---|
date | Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21) |
parents | |
children | f1de7262d8d9 b9b35ca2eec2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a5a898f6886c |
---|---|
1 /* | |
2 * This Source Code Form is subject to the terms of the Mozilla Public | |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
5 * | |
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V. | |
7 */ | |
8 | |
9 package nl.cwi.monetdb.mcl.parser; | |
10 | |
11 | |
12 /** | |
13 * Interface for parsers in MCL. The parser family in MCL is set up as | |
14 * a reusable object. This allows the same parser to be used again for | |
15 * the same type of work. While this is a very unnatural solution in | |
16 * the Java language, it prevents many object creations on a low level | |
17 * of the protocol. This favours performance. | |
18 * | |
19 * A typical parser has a method parse() which takes a String, and the | |
20 * methods hasNext() and next() to retrieve the values that were | |
21 * extracted by the parser. Parser specific methods may be available to | |
22 * perform common tasks. | |
23 * | |
24 * @author Fabian Groffen | |
25 */ | |
26 public abstract class MCLParser { | |
27 /** The String values found while parsing. Public, you may touch it. */ | |
28 public final String values[]; | |
29 /** The int values found while parsing. Public, you may touch it. */ | |
30 public final int intValues[]; | |
31 private int colnr; | |
32 | |
33 /** | |
34 * Creates an MCLParser targetted at a given number of field values. | |
35 * The lines parsed by an instance of this MCLParser should have | |
36 * exactly capacity field values. | |
37 * | |
38 * @param capacity the number of field values to expect | |
39 */ | |
40 protected MCLParser(int capacity) { | |
41 values = new String[capacity]; | |
42 intValues = new int[capacity]; | |
43 } | |
44 | |
45 /** | |
46 * Parse the given string, and populate the internal field array | |
47 * to allow for next() and hasNext() calls. | |
48 * | |
49 * @param source the String containing the line to parse | |
50 * @return value | |
51 * @throws MCLParseException if source cannot be (fully) parsed by | |
52 * this parser | |
53 * @see #next() | |
54 * @see #nextInt() | |
55 * @see #hasNext() | |
56 */ | |
57 abstract public int parse(String source) throws MCLParseException; | |
58 | |
59 /** | |
60 * Repositions the internal field offset to the start, such that the | |
61 * next call to next() will return the first field again. | |
62 */ | |
63 final public void reset() { | |
64 colnr = 0; | |
65 } | |
66 | |
67 /** | |
68 * Returns whether the next call to next() or nextInt() succeeds. | |
69 * | |
70 * @return true if the next call to next() or nextInt() is bound to | |
71 * succeed | |
72 * @see #next() | |
73 * @see #nextInt() | |
74 */ | |
75 final public boolean hasNext() { | |
76 return colnr < values.length; | |
77 } | |
78 | |
79 /** | |
80 * Returns the current field value, and advances the field counter | |
81 * to the next value. This method may fail with a RuntimeError if | |
82 * the current field counter is out of bounds. Call hasNext() to | |
83 * determine if the call to next() will succeed. | |
84 * | |
85 * @return the current field value | |
86 * @see #nextInt() | |
87 * @see #hasNext() | |
88 */ | |
89 final public String next() { | |
90 return values[colnr++]; | |
91 } | |
92 | |
93 /** | |
94 * Returns the current field value as integer, and advances the | |
95 * field counter to the next value. This method has the same | |
96 * characteristics as the next() method, apart from returning the | |
97 * field value as an integer. | |
98 * | |
99 * @return the current field value as integer | |
100 * @see #next() | |
101 */ | |
102 final public int nextInt() { | |
103 return intValues[colnr++]; | |
104 } | |
105 } |