/hudson-utils/src/main/java/org/hudsonci/utils/marshal/xref/XReference.java

http://github.com/hudson/hudson · Java · 112 lines · 54 code · 16 blank · 42 comment · 4 complexity · 875d6d115fbe8d2df87e050f11709fa9 MD5 · raw file

  1. /**
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.hudsonci.utils.marshal.xref;
  25. import org.hudsonci.utils.marshal.Marshaller;
  26. import static com.google.common.base.Preconditions.checkNotNull;
  27. /**
  28. * Reference to an externally serialized entity.
  29. *
  30. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  31. * @since 2.1.0
  32. */
  33. public abstract class XReference<T>
  34. {
  35. protected transient Holder<T> holder;
  36. public XReference(final T value) {
  37. set(value);
  38. }
  39. public XReference() {
  40. // empty
  41. }
  42. public void set(final T value) {
  43. if (value != null) {
  44. holder = new InstanceHolder<T>(value);
  45. }
  46. }
  47. public T get() {
  48. if (holder != null) {
  49. return holder.get();
  50. }
  51. return null;
  52. }
  53. /**
  54. * Defines the path of the external reference.
  55. */
  56. public abstract String getPath();
  57. /**
  58. * Override to provide alternative marshalling.
  59. */
  60. public Marshaller getMarshaller() {
  61. return null;
  62. }
  63. @Override
  64. public String toString() {
  65. return getClass().getName() + "{" +
  66. "holder=" + holder +
  67. '}';
  68. }
  69. /**
  70. * Provides delegation for instance access.
  71. */
  72. public static interface Holder<T>
  73. {
  74. T get();
  75. }
  76. /**
  77. * Holds on to a specific instance.
  78. */
  79. public static class InstanceHolder<T>
  80. implements Holder<T>
  81. {
  82. protected final T instance;
  83. protected InstanceHolder(final T instance) {
  84. this.instance = checkNotNull(instance);
  85. }
  86. public T get() {
  87. return instance;
  88. }
  89. @Override
  90. public String toString() {
  91. return "InstanceHolder{" +
  92. "instance=" + instance +
  93. '}';
  94. }
  95. }
  96. }