/*
 * 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.spi.reflect;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

import javax.inject.Provider;
import javax.inject.Singleton;

import org.grouplens.grapht.spi.CachePolicy;
import org.grouplens.grapht.spi.Desire;
import org.grouplens.grapht.spi.ProviderSource;
import org.grouplens.grapht.spi.Satisfaction;
import org.grouplens.grapht.util.Preconditions;
import org.grouplens.grapht.util.Types;


/**
 * Satisfaction implementation wrapping an existing Provider instance. It has no
 * dependencies and it always returns the same Provider when
 * {@link #makeProvider(ProviderSource)} is invoked.
 * 
 * @author Michael Ludwig <mludwig@cs.umn.edu>
 */
public class ProviderInstanceSatisfaction implements Satisfaction, Externalizable {
    // "final"
    private Provider<?> provider;

    /**
     * Create a new satisfaction that wraps the given Provider instance.
     * 
     * @param provider The provider
     * @throws NullPointerException if provider is null
     */
    public ProviderInstanceSatisfaction(Provider<?> provider) {
        Preconditions.notNull("provider", provider);
        this.provider = provider;
    }
    
    /**
     * Constructor required by {@link Externalizable}.
     */
    public ProviderInstanceSatisfaction() { }
    
    @Override
    public CachePolicy getDefaultCachePolicy() {
        return (getErasedType().getAnnotation(Singleton.class) != null ? CachePolicy.MEMOIZE : CachePolicy.NO_PREFERENCE);
    }
    
    /**
     * @return The provider instance returned by {@link #makeProvider(ProviderSource)}
     */
    public Provider<?> getProvider() {
        return provider;
    }
    
    @Override
    public List<? extends Desire> getDependencies() {
        return Collections.emptyList();
    }

    @Override
    public Type getType() {
        return Types.getProvidedType(provider);
    }

    @Override
    public Class<?> getErasedType() {
        return Types.erase(getType());
    }

    @Override
    public boolean hasInstance() {
        return false;
    }

    @Override
    public Provider<?> makeProvider(ProviderSource dependencies) {
        return provider;
    }
    
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ProviderInstanceSatisfaction)) {
            return false;
        }
        return ((ProviderInstanceSatisfaction) o).provider.equals(provider);
    }
    
    @Override
    public int hashCode() {
        return provider.hashCode();
    }
    
    @Override
    public String toString() {
        return "ProviderInstance(" + provider + ")";
    }
    
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        provider = (Provider<?>) in.readObject();
    }
    
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(provider);
    }
}

@@ -917,107 +917,9 @@
 .io.
-Externalizable;%0Aimport java.io.IOException;%0Aimport java.io.ObjectInput;%0Aimport java.io.ObjectOutput
+*
 ;%0Aim
@@ -1653,22 +1653,20 @@
 action, 
-Extern
+Seri
 alizable
@@ -1676,18 +1676,56 @@
     
-// %22final%22
+private static final long serialVersionUID = 1L;
 %0A   
@@ -1733,16 +1733,22 @@
 private 
+final 
 Provider
@@ -2117,130 +2117,8 @@
    %0A
-    /**%0A     * Constructor required by %7B@link Externalizable%7D.%0A     */%0A    public ProviderInstanceSatisfaction() %7B %7D%0A    %0A
     
@@ -3057,13 +3057,15 @@
     
-if (!
+return 
 (o i
@@ -3103,19 +3103,16 @@
 faction)
-) %7B
 %0A       
@@ -3120,46 +3120,13 @@
     
-return false;%0A        %7D%0A        return
+   &&
  ((P
@@ -3392,300 +3392,6 @@
   %7D%0A
-    %0A    @Override%0A    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException %7B%0A        provider = (Provider%3C?%3E) in.readObject();%0A    %7D%0A    %0A    @Override%0A    public void writeExternal(ObjectOutput out) throws IOException %7B%0A        out.writeObject(provider);%0A    %7D%0A
 %7D%0A

Clean up `ProviderInstanceSatisfaction`