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.

PluginDependency.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import java.util.Objects;
  18. /**
  19. * A plugin dependency is a dependency that the plugin has on another plugin.
  20. * <p>
  21. * The dependency is defined by the plugin id and the version of the plugin that is required.
  22. * <p>
  23. * A dependency is considered as optional, if the plugin id ends with a question mark.
  24. * For example, the plugin id "my-plugin?" is considered as optional.
  25. * <p>
  26. * The plugin id and the version are separated by the '@' character.
  27. * For example, the dependency "my-plugin@1.0.0" means that the plugin "my-plugin" with version "1.0.0" is required.
  28. * If the version is not specified, then the plugin is required with any version.
  29. * For example, the dependency "my-plugin" means that the plugin "my-plugin" with any version is required.
  30. *
  31. * @see VersionManager
  32. * @author Decebal Suiu
  33. */
  34. public class PluginDependency {
  35. private String pluginId;
  36. private String pluginVersionSupport = "*";
  37. private final boolean optional;
  38. public PluginDependency(String dependency) {
  39. int index = dependency.indexOf('@');
  40. if (index == -1) {
  41. this.pluginId = dependency;
  42. } else {
  43. this.pluginId = dependency.substring(0, index);
  44. if (dependency.length() > index + 1) {
  45. this.pluginVersionSupport = dependency.substring(index + 1);
  46. }
  47. }
  48. // A dependency is considered as optional, if the plugin id ends with a question mark.
  49. this.optional = this.pluginId.endsWith("?");
  50. if (this.optional) {
  51. this.pluginId = this.pluginId.substring(0, this.pluginId.length() - 1);
  52. }
  53. }
  54. /**
  55. * Returns the unique identifier of the plugin.
  56. *
  57. * @return the plugin id
  58. */
  59. public String getPluginId() {
  60. return pluginId;
  61. }
  62. /**
  63. * Returns the version of the plugin that is required.
  64. *
  65. * @return the version of the plugin that is required
  66. */
  67. public String getPluginVersionSupport() {
  68. return pluginVersionSupport;
  69. }
  70. /**
  71. * Returns {@code true} if the dependency is optional, {@code false} otherwise.
  72. *
  73. * @return {@code true} if the dependency is optional, {@code false} otherwise
  74. */
  75. public boolean isOptional() {
  76. return optional;
  77. }
  78. @Override
  79. public String toString() {
  80. return "PluginDependency [pluginId=" + pluginId + ", pluginVersionSupport="
  81. + pluginVersionSupport + ", optional="
  82. + optional + "]";
  83. }
  84. @Override
  85. public boolean equals(Object o) {
  86. if (this == o) return true;
  87. if (!(o instanceof PluginDependency)) return false;
  88. PluginDependency that = (PluginDependency) o;
  89. return optional == that.optional &&
  90. pluginId.equals(that.pluginId) &&
  91. pluginVersionSupport.equals(that.pluginVersionSupport);
  92. }
  93. @Override
  94. public int hashCode() {
  95. return Objects.hash(pluginId, pluginVersionSupport, optional);
  96. }
  97. }