Created
September 18, 2017 21:37
-
-
Save kleczkowski/172c6397c31111d9e21a9355b8526a3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MIT License | |
* | |
* Copyright (c) 2017 Konrad Kleczkowski | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
package com.github.repaj.libjson.io; | |
/** | |
* Template class to implement JSON tokenizer. | |
* | |
* @author Konrad Kleczkowski | |
*/ | |
@SuppressWarnings("WeakerAccess") | |
public abstract class AbstractJsonTokenizer implements JsonTokenizer { | |
private JsonToken current; | |
/** | |
* If token type is the same as pending token, returns that token | |
* and removes it from input. | |
* | |
* @param tokenType expected type of pending token | |
* @return pending token it token type is the same as specified. | |
* @throws JsonParseException if expected and actual token types are mismatched | |
* @throws JsonIOException if I/O error occurs | |
* @throws NullPointerException if <code>tokenType</code> is null | |
*/ | |
@Override | |
public final String next(JsonTokenType tokenType) throws JsonParseException { | |
if (!hasNext(tokenType)) { | |
throw new JsonParseException("Expected " + tokenType + " token" | |
+ (getLine() != -1 ? " at line " + getLine() : "")); | |
} | |
String match = current.getMatch(); | |
current = null; | |
return match; | |
} | |
/** | |
* Returns <code>true</code> if pending token is of specified type. | |
* | |
* @param tokenType expected type of pending token | |
* @return <code>true</code> if pending token is of specified type, otherwise <code>false</code> | |
* @throws JsonIOException if I/O error occurs | |
* @throws NullPointerException if <code>tokenType</code> is null | |
*/ | |
@Override | |
public final boolean hasNext(JsonTokenType tokenType) { | |
if (current == null) { | |
current = next(); | |
} | |
return tokenType == current.getTokenType(); | |
} | |
/** | |
* Removes token from a queue. | |
* | |
* @return token removed from a queue (must be non-null) | |
* @throws JsonIOException if I/O error occurs | |
* @throws JsonParseException if end-of-file occurs | |
*/ | |
protected abstract JsonToken next(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment