All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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 org.jboss.weld.bootstrap.api.helpers;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.jboss.weld.bootstrap.api.BootstrapService;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.bootstrap.api.ServiceRegistry;

/**
 * A registry for services
 *
 * @author Pete Muir
 *
 */
public class SimpleServiceRegistry implements ServiceRegistry {

    private final Map, Service> services;
    private final Set bootstrapServices;

    public SimpleServiceRegistry() {
        this.services = new HashMap, Service>();
        this.bootstrapServices = new HashSet();
    }

    public  void add(java.lang.Class type, S service) {
        services.put(type, service);
        if (service instanceof BootstrapService) {
            bootstrapServices.add((BootstrapService) service);
        }
    }

    @SuppressWarnings("unchecked")
    public void addAll(Collection, Service>> services) {
        for (Entry, Service> entry : services) {
            add((Class) entry.getKey(), entry.getValue());
        }
    }

    public Set, Service>> entrySet() {
        return services.entrySet();
    }

    protected Map, Service> get() {
        return services;
    }

    @SuppressWarnings("unchecked")
    public  S get(Class type) {
        return (S) services.get(type);
    }

    public  boolean contains(Class type) {
        return services.containsKey(type);
    }

    public void cleanup() {
        for (Service service : services.values()) {
            service.cleanup();
        }
    }

    @Override
    public void cleanupAfterBoot() {
        for (BootstrapService service : bootstrapServices) {
            service.cleanupAfterBoot();
        }
    }

    @Override
    public String toString() {
        return services.toString();
    }

    @Override
    public int hashCode() {
        return services.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Map) {
            return services.equals(obj);
        } else {
            return false;
        }
    }

    public Iterator iterator() {
        return new ValueIterator, Service>() {

            @Override
            protected Iterator, Service>> delegate() {
                return services.entrySet().iterator();
            }

        };
    }

    private abstract static class ValueIterator implements Iterator {

        protected abstract Iterator> delegate();

        public boolean hasNext() {
            return delegate().hasNext();
        }

        public V next() {
            return delegate().next().getValue();
        }

        public void remove() {
            delegate().remove();
        }

    }

}