001/*
002 * Copyright 2019 Zoran.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package javax.visrec.spi;
017
018import java.util.HashMap;
019import java.util.Iterator;
020import java.util.Map;
021
022/**
023 * Registry for all ML algorithms provided by this implementation.
024 * 
025 * @author Zoran Sevarac
026 */
027public class ServiceRegistry {
028    
029    private final Map<Class<?>, Object> providers = new HashMap();
030
031    public <T> void registerServiceProvider(Class<T> service, T provider) {
032        providers.put(service, provider);
033    }
034
035    public <T> void deregisterServiceProvider(Class<T> service, T provider) {
036        providers.remove(service, provider);
037    }
038
039    public Iterator<?> getAllProviders() {
040        return providers.values().iterator();
041    }
042
043    public <T> T getProvider(Class<T> service) {
044        return service.cast(providers.get(service));
045    }
046}