/*
 * Copyright 2015 Danilo Reinert
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.reinert.requestor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.google.web.bindery.event.shared.HandlerRegistration;

/**
 * Manager of instance providers.
 *
 * @author Danilo Reinert
 */
class ProviderManagerImpl implements ProviderManager {

    private final Map<String, Provider<?>> providers = new HashMap<String, Provider<?>>();

    public ProviderManagerImpl() {
        registerCollectionProviders();
    }

    @Override
    public <T> HandlerRegistration register(Class<T> type, Provider<T> provider) {
        final String typeName = type.getName();
        providers.put(typeName, provider);

        return new HandlerRegistration() {
            @Override
            public void removeHandler() {
                providers.remove(typeName);
            }
        };
    }

    @Override
    public <T> HandlerRegistration register(TypeProvider<T> provider) {
        return register(provider.getType(), provider);
    }

    /**
     * Given a class, return its {@link Provider}.
     *
     * @param <T> the class of T
     */
    @SuppressWarnings("unchecked")
    public <T> Provider<T> get(Class<T> type) {
        // TODO: throw exception if not exists?
        return (Provider<T>) providers.get(type.getName());
    }

    private void registerCollectionProviders() {
        final Provider<ArrayList> arrayListProvider = new Provider<ArrayList>() {
            @Override
            public ArrayList getInstance() {
                return new ArrayList();
            }
        };
        providers.put(Collection.class.getName(), arrayListProvider);
        providers.put(List.class.getName(), arrayListProvider);
        providers.put(ArrayList.class.getName(), arrayListProvider);
        providers.put(LinkedList.class.getName(), new Provider<LinkedList>() {
            @Override
            public LinkedList getInstance() {
                return new LinkedList();
            }
        });
        final Provider<HashSet> hashSetProvider = new Provider<HashSet>() {
            @Override
            public HashSet getInstance() {
                return new HashSet();
            }
        };
        providers.put(Set.class.getName(), hashSetProvider);
        providers.put(HashSet.class.getName(), hashSetProvider);
        providers.put(TreeSet.class.getName(), new Provider<TreeSet>() {
            @Override
            public TreeSet getInstance() {
                return new TreeSet();
            }
        });
    }
}

@@ -1360,16 +1360,25 @@
 .get
+Canonical
 Name();%0A
     
@@ -1369,24 +1369,24 @@
 icalName();%0A
-
         prov
@@ -2033,24 +2033,33 @@
 get(type.get
+Canonical
 Name());%0A   
@@ -2359,32 +2359,41 @@
 ection.class.get
+Canonical
 Name(), arrayLis
@@ -2432,32 +2432,41 @@
 t(List.class.get
+Canonical
 Name(), arrayLis
@@ -2510,32 +2510,41 @@
 ayList.class.get
+Canonical
 Name(), arrayLis
@@ -2589,32 +2589,41 @@
 edList.class.get
+Canonical
 Name(), new Prov
@@ -3009,32 +3009,41 @@
 ut(Set.class.get
+Canonical
 Name(), hashSetP
@@ -3083,32 +3083,41 @@
 ashSet.class.get
+Canonical
 Name(), hashSetP
@@ -3165,16 +3165,25 @@
 lass.get
+Canonical
 Name(), 

Replace name by canonicalName in ProviderManagerImpl indexing