org.databene.commons.ParseUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databene-commons Show documentation
Show all versions of databene-commons Show documentation
'databene commons' is an open source Java library by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
The newest version!
/*
* Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.databene.commons;
import java.io.PushbackReader;
import java.io.IOException;
import java.io.Reader;
import java.io.BufferedReader;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.List;
import java.util.ArrayList;
import java.math.BigInteger;
/**
* Provides methods for parsing PushbackReaders and Strings.
* Created: 20.03.2005 16:32:00
* @author Volker Bergmann
*/
public final class ParseUtil {
public static double parseDecimal(PushbackReader reader) throws IOException, ParseException {
double result = parseInteger(reader);
double postfix = parseOptionalPostfix(reader);
if (result >= 0)
result += postfix;
else
result -= postfix;
return result;
}
public static double parseOptionalPostfix(PushbackReader reader) throws IOException {
int c = reader.read();
if (c != '.') {
if (c != -1)
reader.unread(c);
return 0.;
}
double result = 0;
double base = 0.1;
while ((c = reader.read()) != -1 && Character.isDigit((char)c)) {
result += (c - '0') * base;
base *= 0.1;
}
if (c != -1)
reader.unread(c);
return result;
}
public static long parseInteger(PushbackReader reader) throws IOException, ParseException {
boolean negative = parseOptionalSign(reader);
return parseNonNegativeInteger(reader) * (negative ? -1 : 1);
}
public static long parseNonNegativeInteger(String source, ParsePosition pos) throws ParseException {
int digit;
if (pos.getIndex() > source.length() || !Character.isDigit(digit = source.charAt(pos.getIndex())))
throw new ParseException("Number expected", 0);
pos.setIndex(pos.getIndex() + 1);
long result = digit - '0';
while(pos.getIndex() < source.length() && Character.isDigit(digit = source.charAt(pos.getIndex()))) {
result = result * 10 + digit - '0';
pos.setIndex(pos.getIndex() + 1);
}
return result;
}
public static long parseNonNegativeInteger(PushbackReader reader) throws IOException, ParseException {
int digit;
if ((digit = reader.read()) == -1 || !Character.isDigit((char)digit))
throw new ParseException("Long expected", 0);
long result = digit - '0';
while((digit = reader.read()) != -1 && Character.isDigit((char)digit))
result = result * 10 + digit - '0';
if (digit != -1)
reader.unread(digit);
return result;
}
public static boolean parseOptionalSign(PushbackReader reader) throws IOException {
skipWhitespace(reader);
int optionalSign = reader.read();
if (optionalSign == '-')
return true;
if (optionalSign != -1)
reader.unread(optionalSign);
return false;
}
public static void skipWhitespace(PushbackReader reader) throws IOException {
int c;
do {
c = reader.read();
} while (c != -1 && Character.isWhitespace((char)c));
if (c != -1)
reader.unread(c);
}
public static String parseUnit(PushbackReader reader) throws IOException {
StringBuilder result = new StringBuilder();
int c;
while ((c = reader.read()) != -1 && Character.isUpperCase((char)c))
result.append((char)c);
if (c != -1)
reader.unread(c);
return (result.length() > 0 ? result.toString() : null);
}
public static boolean parseEstimated(PushbackReader reader) throws IOException {
int c = reader.read();
if (c == 'e')
return true;
if (c != -1)
reader.unread(c);
return false;
}
public static boolean isEmpty(Object object) {
return (object == null || StringUtil.isEmpty((String) object));
}
public static int nextNonWhitespaceIndex(String source, int startIndex) {
int i;
for (i = startIndex; i < source.length() && Character.isWhitespace(source.charAt(i)); i++) {
}
if (i >= source.length())
i = -1;
return i;
}
public static String[][] parseEmptyLineSeparatedFile(Reader src) throws IOException {
BufferedReader reader = null;
List> sections = new ArrayList>();
List lines = null;
try {
reader = new BufferedReader(src);
String line;
while ((line = reader.readLine()) != null) {
if (line.length() > 0) {
if (lines == null) {
// start a new section
lines = new ArrayList();
sections.add(lines);
}
lines.add(line);
} else {
// end of the section
if (lines != null) {
lines = null;
} else {
// add empty section
sections.add(new ArrayList());
}
}
}
return StringUtil.toArrayArray(sections);
} finally {
IOUtil.close(reader);
}
}
public static Object[] splitNumbers(String text) {
List