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.

ExtensionWrapper.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2014 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j;
  14. /**
  15. * A wrapper over extension instance.
  16. *
  17. * @author Decebal Suiu
  18. */
  19. public class ExtensionWrapper<T> implements Comparable<ExtensionWrapper<T>> {
  20. ExtensionDescriptor descriptor;
  21. ExtensionFactory extensionFactory;
  22. T extension; // cache
  23. public ExtensionWrapper(ExtensionDescriptor descriptor) {
  24. this.descriptor = descriptor;
  25. }
  26. public T getExtension() {
  27. if (extension == null) {
  28. extension = (T) extensionFactory.create(descriptor.getExtensionClass());
  29. }
  30. return extension;
  31. }
  32. public ExtensionDescriptor getDescriptor() {
  33. return descriptor;
  34. }
  35. public int getOrdinal() {
  36. return descriptor.getOrdinal();
  37. }
  38. @Override
  39. public int compareTo(ExtensionWrapper<T> o) {
  40. return (getOrdinal() - o.getOrdinal());
  41. }
  42. void setExtensionFactory(ExtensionFactory extensionFactory) {
  43. this.extensionFactory = extensionFactory;
  44. }
  45. }