You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Semver4jVersionManager.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package org.pf4j;
  2. import org.pf4j.util.StringUtils;
  3. import com.vdurmont.semver4j.Semver;
  4. import com.vdurmont.semver4j.Semver.SemverType;
  5. /**
  6. * Implementation for {@link VersionManager}.
  7. * This implementation uses semver4j (a Java implementation of the SemVer Specification). Before using it you have to include the optional
  8. * dependency semver4j
  9. *
  10. * @author Wolfram Haussig
  11. */
  12. public class Semver4jVersionManager implements VersionManager {
  13. /**
  14. * the parser type of the version - see https://github.com/vdurmont/semver4j#the-semver-object for details
  15. */
  16. private SemverType type;
  17. /**
  18. * creates a version manager with the given parser type
  19. * @param type
  20. */
  21. public Semver4jVersionManager(SemverType type) {
  22. this.type = type;
  23. }
  24. /**
  25. * creates a version manager with the NPM parser type which supports ranges
  26. */
  27. public Semver4jVersionManager() {
  28. this(SemverType.NPM);
  29. }
  30. /**
  31. * parses a version with the current parser type
  32. * @param version
  33. */
  34. protected Semver parseVersion(String version) {
  35. if (version == null)
  36. return new Semver("", type);
  37. return new Semver(version, type);
  38. }
  39. /**
  40. * Checks if a version satisfies the specified SemVer {@link Expression} string.
  41. * If the constraint is empty or null then the method returns true.
  42. * Constraint examples: {@code >2.0.0} (simple), {@code "1.1.1 || 1.2.3 - 2.0.0"} (range).
  43. * See https://github.com/vdurmont/semver4j#requirements for more info.
  44. *
  45. * @param version
  46. * @param constraint
  47. * @return
  48. */
  49. @Override
  50. public boolean checkVersionConstraint(String version, String constraint) {
  51. return StringUtils.isNullOrEmpty(constraint) || "*".equals(constraint) || parseVersion(version).satisfies(constraint);
  52. }
  53. @Override
  54. public int compareVersions(String v1, String v2) {
  55. return parseVersion(v1).compareTo(parseVersion(v2));
  56. }
  57. @Override
  58. public boolean isStable(String version) {
  59. return parseVersion(version).isStable();
  60. }
  61. }