001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015-2016 ForgeRock AS. 015 */ 016package org.forgerock.audit; 017 018import java.util.HashMap; 019import java.util.Map; 020 021/** 022 * Base DependencyProvider that has provides no dependencies. 023 */ 024public class DependencyProviderBase implements DependencyProvider { 025 026 private final Map<Class<?>, Object> dependencies = new HashMap<>(); 027 028 /** 029 * {@inheritDoc} 030 */ 031 @SuppressWarnings("unchecked") 032 @Override 033 public <T> T getDependency(Class<T> clazz) throws ClassNotFoundException { 034 T dependency = (T) dependencies.get(clazz); 035 if (dependency == null) { 036 throw new ClassNotFoundException("No instance registered for class: " + clazz.getName()); 037 } else { 038 return dependency; 039 } 040 } 041 042 /** 043 * Register a new provided dependency. 044 * @param <T> The type of the dependency. 045 * @param clazz the class to register 046 * @param obj the instance to provide 047 * @return the previous values registered for {@literal clazz} 048 */ 049 public <T> T register(Class<T> clazz, T obj) { 050 return (T) dependencies.put(clazz, obj); 051 } 052}