DATACMNS-719 - Version now parses strings with non-numeric suffixes correctly.
Extended Version value object to correctly dump a non-numeric last segment of a version string to parse. Incorrect source version strings now raise an IllegalArgumentException better explaining the cause. Related tickets: DATACMNS-496.
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
*
|
||||
* 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.springframework.data.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -13,6 +28,8 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class Version implements Comparable<Version> {
|
||||
|
||||
private static final String VERSION_PARSE_ERROR = "Invalid version string! Could not parse segment %s within %s.";
|
||||
|
||||
private final int major;
|
||||
private final int minor;
|
||||
private final int bugfix;
|
||||
@@ -53,7 +70,16 @@ public class Version implements Comparable<Version> {
|
||||
int[] intParts = new int[parts.length];
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
intParts[i] = Integer.parseInt(parts[i].replaceAll("\\D.*", ""));
|
||||
|
||||
String input = i == parts.length - 1 ? parts[i].replaceAll("\\D.*", "") : parts[i];
|
||||
|
||||
if (StringUtils.hasText(input)) {
|
||||
try {
|
||||
intParts[i] = Integer.parseInt(input);
|
||||
} catch (IllegalArgumentException o_O) {
|
||||
throw new IllegalArgumentException(String.format(VERSION_PARSE_ERROR, input, version), o_O);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Version(intParts);
|
||||
|
||||
Reference in New Issue
Block a user