"schema": {"type": "string"},
"in": "query",
"name": "compatible_with_version",
- "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around.",
+ "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching types will be returned sorted in ascending order.",
"required": false
}
],
in: query
description: Select types that are compatible with the given version. This
parameter is only applicable in conjunction with type_name. As an example
- version 1.9.1 is compatible with 1.0.0 but not the other way around.
+ version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching
+ types will be returned sorted in ascending order.
required: false
style: form
explode: true
@Parameter(name = Consts.COMPATIBLE_WITH_VERSION_PARAM, required = false, //
description = "Select types that are compatible with the given version. This parameter is only applicable in conjunction with "
+ Consts.TYPE_NAME_PARAM
- + ". As an example version 1.9.1 is compatible with 1.0.0 but not the other way around.") //
+ + ". As an example version 1.9.1 is compatible with 1.0.0 but not the other way around."
+ + " Matching types will be returned sorted in ascending order.") //
@RequestParam(name = Consts.COMPATIBLE_WITH_VERSION_PARAM, required = false) String compatibleWithVersion
) throws ServiceException {
throw new ServiceException("Syntax error in " + version, HttpStatus.BAD_REQUEST);
}
}
+
+ public int compareTo(Version other) {
+ if (major != other.major)
+ return major - other.major;
+ if (minor != other.minor)
+ return minor - other.minor;
+ return patch - other.patch;
+ }
+
+ public boolean isCompatibleWith(Version other) {
+ return (major == other.major && minor >= other.minor);
+ }
}
@Getter
return TypeId.ofString(getId());
}
+ public Version getVersion() {
+ try {
+ return Version.ofString(getTypeId().getVersion());
+ } catch (ServiceException e) {
+ return new Version(0, 0, 0);
+ }
+ }
+
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Vector;
* @param types the types to select from
* @param typeName select types with given type name
* @param compatibleWithVersion select types that are compatible with given
- * version string (major.minor.patch)
+ * version string (major.minor.patch).
+ * Matching types will be sorted in ascending
+ * order.
* @return the types that matches given criterias
* @throws ServiceException if there are errors in the given input
*/
return result;
}
- private static boolean isTypeCompatibleWithVersion(PolicyType type, PolicyType.Version version) {
- try {
- PolicyType.TypeId typeId = type.getTypeId();
- PolicyType.Version typeVersion = PolicyType.Version.ofString(typeId.getVersion());
- return (typeVersion.major == version.major && typeVersion.minor >= version.minor);
- } catch (Exception e) {
- logger.warn("Ignoring type with syntactically incorrect type ID: {}", type.getId());
- return false;
- }
- }
-
private static Collection<PolicyType> filterCompatibleWithVersion(Collection<PolicyType> types, String versionStr)
throws ServiceException {
- Collection<PolicyType> result = new ArrayList<>();
- PolicyType.Version otherVersion = PolicyType.Version.ofString(versionStr);
+ List<PolicyType> result = new ArrayList<>();
+ PolicyType.Version requestedVersion = PolicyType.Version.ofString(versionStr);
for (PolicyType type : types) {
- if (isTypeCompatibleWithVersion(type, otherVersion)) {
+ if (type.getVersion().isCompatibleWith(requestedVersion)) {
result.add(type);
}
}
+ result.sort((left, right) -> left.getVersion().compareTo(right.getVersion()));
return result;
}
"schema": {"type": "string"},
"in": "query",
"name": "compatible_with_version",
- "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around.",
+ "description": "Select types that are compatible with the given version. This parameter is only applicable in conjunction with type_name. As an example version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching types will be returned sorted in ascending order.",
"required": false
}
],
in: query
description: Select types that are compatible with the given version. This
parameter is only applicable in conjunction with type_name. As an example
- version 1.9.1 is compatible with 1.0.0 but not the other way around.
+ version 1.9.1 is compatible with 1.0.0 but not the other way around. Matching
+ types will be returned sorted in ascending order.
required: false
style: form
explode: true