org.databene.commons.math.IntervalParser 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.
/*
* Copyright (C) 2004-2014 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.math;
import java.text.ParsePosition;
import java.util.Comparator;
import org.databene.commons.ParseException;
import static org.databene.commons.ParseUtil.skipWhiteSpace;
import org.databene.commons.Parser;
/**
* Parses an {@link Interval} generically using an endpoint parser and an endpoint comparator.
* The endpoint parser has to be able to parse the interval endpoint values.
* Created: 10.03.2011 15:33:01
* @since 0.5.8
* @author Volker Bergmann
*/
public class IntervalParser extends Parser> {
private Parser endpointParser;
private Comparator endpointComparator;
public static Interval parse(String text, Parser endpointParser, Comparator endpointComparator) {
return new IntervalParser(endpointParser, endpointComparator).parseObject(text, new ParsePosition(0));
}
public IntervalParser(Parser endPointParser, Comparator endpointComparator) {
this.endpointParser = endPointParser;
this.endpointComparator = endpointComparator;
}
@Override
public Interval parseObject(String text, ParsePosition pos) throws ParseException {
skipWhiteSpace(text, pos);
// parse left bracket
boolean minInclusive;
char c = text.charAt(pos.getIndex());
switch (c) {
case '[' : minInclusive = true; break;
case ']' : minInclusive = false; break;
default : throw new ParseException("Expected '[' or ']', found: " + c, text);
}
pos.setIndex(pos.getIndex() + 1);
skipWhiteSpace(text, pos);
// parse lower endpoint
E min = endpointParser.parseObject(text, pos);
skipWhiteSpace(text, pos);
// parse comma
c = text.charAt(pos.getIndex());
if (c != ',')
throw new ParseException("Expected ',', found '" + c + "'", text);
pos.setIndex(pos.getIndex() + 1);
skipWhiteSpace(text, pos);
// parse upper endpoint
E max = endpointParser.parseObject(text, pos);
skipWhiteSpace(text, pos);
// parse right bracket
boolean maxInclusive;
c = text.charAt(pos.getIndex());
switch (c) {
case ']' : maxInclusive = true; break;
case '[' : maxInclusive = false; break;
default : throw new ParseException("Expected '[' or ']', found: " + c, text);
}
pos.setIndex(pos.getIndex() + 1);
skipWhiteSpace(text, pos);
return new Interval(min, minInclusive, max, maxInclusive, endpointComparator);
}
}