comparison src/main/java/org/monetdb/mcl/parser/MCLParser.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/mcl/parser/MCLParser.java@54137aeb1f92
children bf9f6b6ecf40
comparison
equal deleted inserted replaced
390:6199e0be3c6e 391:f523727db392
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 - 2020 MonetDB B.V.
7 */
8
9 package org.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 protected int colnr = 0;
30
31 /**
32 * Creates an MCLParser targetted at a given number of field values.
33 * The lines parsed by an instance of this MCLParser should have
34 * exactly capacity field values.
35 *
36 * @param capacity the number of field values to expect
37 */
38 protected MCLParser(final int capacity) {
39 values = new String[capacity];
40 }
41
42 /**
43 * Parse the given string, and populate the internal field array
44 * to allow for next() and hasNext() calls.
45 *
46 * @param source the String containing the line to parse
47 * @return value
48 * @throws MCLParseException if source cannot be (fully) parsed by
49 * this parser
50 * @see #next()
51 * @see #hasNext()
52 */
53 abstract public int parse(final String source) throws MCLParseException;
54
55 /**
56 * Repositions the internal field offset to the start, such that the
57 * next call to next() will return the first field again.
58 */
59 final public void reset() {
60 colnr = 0;
61 }
62
63 /**
64 * Returns whether the next call to next() or nextInt() succeeds.
65 *
66 * @return true if the next call to next() or nextInt() is bound to
67 * succeed
68 * @see #next()
69 */
70 final public boolean hasNext() {
71 return colnr < values.length;
72 }
73
74 /**
75 * Returns the current field value, and advances the field counter
76 * to the next value. This method may fail with a RuntimeError if
77 * the current field counter is out of bounds. Call hasNext() to
78 * determine if the call to next() will succeed.
79 *
80 * @return the current field value
81 * @see #hasNext()
82 */
83 final public String next() {
84 return values[colnr++];
85 }
86 }