PageRenderTime 17ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 25package org.hudsonci.utils.marshal.xref;
 26
 27import org.hudsonci.utils.marshal.Marshaller;
 28
 29import static com.google.common.base.Preconditions.checkNotNull;
 30
 31/**
 32 * Reference to an externally serialized entity.
 33 *
 34 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 35 * @since 2.1.0
 36 */
 37public abstract class XReference<T>
 38{
 39    protected transient Holder<T> holder;
 40
 41    public XReference(final T value) {
 42        set(value);
 43    }
 44
 45    public XReference() {
 46        // empty
 47    }
 48
 49    public void set(final T value) {
 50        if (value != null) {
 51            holder = new InstanceHolder<T>(value);
 52        }
 53    }
 54    
 55    public T get() {
 56        if (holder != null) {
 57            return holder.get();
 58        }
 59        return null;
 60    }
 61
 62    /**
 63     * Defines the path of the external reference.
 64     */
 65    public abstract String getPath();
 66
 67    /**
 68     * Override to provide alternative marshalling.
 69     */
 70    public Marshaller getMarshaller() {
 71        return null;
 72    }
 73
 74    @Override
 75    public String toString() {
 76        return getClass().getName() + "{" +
 77            "holder=" + holder +
 78        '}';
 79    }
 80
 81    /**
 82     * Provides delegation for instance access.
 83     */
 84    public static interface Holder<T>
 85    {
 86        T get();
 87    }
 88
 89    /**
 90     * Holds on to a specific instance.
 91     */
 92    public static class InstanceHolder<T>
 93        implements Holder<T>
 94    {
 95        protected final T instance;
 96
 97        protected InstanceHolder(final T instance) {
 98            this.instance = checkNotNull(instance);
 99        }
100
101        public T get() {
102            return instance;
103        }
104
105        @Override
106        public String toString() {
107            return "InstanceHolder{" +
108                "instance=" + instance +
109                '}';
110        }
111    }
112}