/*
 * Grapht, an open source dependency injector.
 * Copyright 2010-2012 Regents of the University of Minnesota and contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.grapht.util;

/**
 * InstanceProvider is a simple Provider that always provides the same instance.
 * 
 * @author Michael Ludwig <mludwig@cs.umn.edu>
 * @param <T>
 */
public class InstanceProvider<T> implements TypedProvider<T> {
    private final Class<?> providedType;
    private final T instance;

    /**
     * Construct a new instance provider.
     * @param instance The instance.
     * @deprecated Use {@link Providers#of(Object)} instead.
     */
    @Deprecated
    public InstanceProvider(T instance) {
        this(instance, instance == null ? Object.class : instance.getClass());
    }

    InstanceProvider(T instance, Class<?> type) {
        if (instance != null && !type.isInstance(instance)) {
            throw new IllegalArgumentException("instance not of specified type");
        }
        this.instance = instance;
        providedType = type;
    }

    @Override
    public Class<?> getProvidedType() {
        return providedType;
    }

    @Override
    public T get() {
        return instance;
    }
}

@@ -891,16 +891,123 @@
 .util;%0A%0A
+import java.io.InvalidObjectException;%0Aimport java.io.ObjectStreamException;%0Aimport java.io.Serializable;%0A%0A
 /**%0A * I
@@ -1212,18 +1212,87 @@
 vider%3CT%3E
- %7B
+, Serializable %7B%0A    private static final long serialVersionUID = -1L;%0A
 %0A    pri
@@ -2051,24 +2051,24 @@
 c T get() %7B%0A
-
         retu
@@ -2085,11 +2085,1214 @@
 e;%0A    %7D
+%0A%0A    private Object writeReplace() %7B%0A        return new SerialProxy(providedType,  instance);%0A    %7D%0A%0A    private Object readObject() throws ObjectStreamException %7B%0A        throw new InvalidObjectException(%22must use serialization proxy%22);%0A    %7D%0A%0A    private static class SerialProxy implements Serializable %7B%0A        private static final long serialVersionUID = 1L;%0A        private ClassProxy type;%0A        private Object instance;%0A%0A        public SerialProxy(Class%3C?%3E t, Object i) %7B%0A            type = ClassProxy.of(t);%0A            instance = i;%0A        %7D%0A%0A        @SuppressWarnings(%22unchecked%22)%0A        private Object readResolve() throws ObjectStreamException %7B%0A            Class%3C?%3E cls = null;%0A            try %7B%0A                cls = type.resolve();%0A            %7D catch (ClassNotFoundException e) %7B%0A                InvalidObjectException ex = new InvalidObjectException(%22class not found%22);%0A                ex.initCause(e);%0A                throw ex;%0A            %7D%0A            if (instance != null && !cls.isInstance(instance)) %7B%0A                throw new InvalidObjectException(%22instance is not of expected type%22);%0A            %7D%0A            return new InstanceProvider(instance, cls);%0A        %7D%0A    %7D
 %0A%7D%0A

Make instance providers serializable