com.rapiddweller.common.math.IntervalParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rd-lib-common Show documentation
Show all versions of rd-lib-common Show documentation
'rapiddweller Common' is an open source Java library
forked from Databene Commons by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
/*
* 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 com.rapiddweller.common.math;
import com.rapiddweller.common.ParseException;
import com.rapiddweller.common.Parser;
import java.text.ParsePosition;
import java.util.Comparator;
import static com.rapiddweller.common.ParseUtil.skipWhiteSpace;
/**
* 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
*
* @param the type of the bounds that define the interval
* @author Volker Bergmann
* @since 0.5.8
*/
public class IntervalParser extends Parser> {
private final Parser endpointParser;
private final Comparator endpointComparator;
/**
* Parse interval.
*
* @param the type parameter
* @param text the text
* @param endpointParser the endpoint parser
* @param endpointComparator the endpoint comparator
* @return the interval
*/
public static Interval parse(String text, Parser endpointParser, Comparator endpointComparator) {
return new IntervalParser(endpointParser, endpointComparator).parseObject(text, new ParsePosition(0));
}
/**
* Instantiates a new Interval parser.
*
* @param endPointParser the end point parser
* @param endpointComparator the endpoint comparator
*/
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);
}
}