com.celum.dbtool.step.RegularVersionFactory Maven / Gradle / Ivy
/*****************************************************************************
* Copyright 2012 celum Slovakia s r.o.
*
* 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.celum.dbtool.step;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Factory class parse the string to {@link RegularVersion}.
*
* @author Zdenko Vrabel ([email protected])
*/
public class RegularVersionFactory implements VersionFactory
{
private final static Pattern p1 = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
private final static Pattern p2 = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
private final static Pattern p3 = Pattern.compile("(\\d+)\\.(\\d+)");
private final static Pattern p4 = Pattern.compile("(\\d+)_(\\d+)_(\\d+)_(\\d+)");
private final static Pattern p5 = Pattern.compile("(\\d+)_(\\d+)_(\\d+)");
private final static Pattern p6 = Pattern.compile("(\\d+)_(\\d+)");
private final static Pattern p7 = Pattern.compile("(\\d+)");
/**
* {@inheritDoc}
*/
@Override
public Version parse(String text)
{
Matcher m = null;
m = p1.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)), Long.parseLong(m.group(2)), Long.parseLong(m.group(3)), Long.parseLong(m.group(4)));
}
m = p2.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)), Long.parseLong(m.group(2)), Long.parseLong(m.group(3)));
}
m = p3.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)), Long.parseLong(m.group(2)));
}
m = p4.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)), Long.parseLong(m.group(2)), Long.parseLong(m.group(3)), Long.parseLong(m.group(4)));
}
m = p5.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)), Long.parseLong(m.group(2)), Long.parseLong(m.group(3)));
}
m = p6.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)), Long.parseLong(m.group(2)));
}
m = p7.matcher(text);
if (m.find()) {
return new RegularVersion(Long.parseLong(m.group(1)));
}
throw new VersionException("cannot parse RegularVersion from '" + text + "'");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy