View Javadoc

1   //  jGABL2 - The Java Graph Algorithm Base Library
2   //  Copyright (C) 2000-2006  Alexander Schwartz
3   //
4   //  This library is free software; you can redistribute it and/or
5   //  modify it under the terms of the GNU Lesser General Public
6   //  License as published by the Free Software Foundation; either
7   //  version 2.1 of the License, or (at your option) any later version.
8   //
9   //  This library is distributed in the hope that it will be useful,
10  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  //  Lesser General Public License for more details.
13  
14  package net.sf.jgabl2.adapt.impl;
15  
16  import net.sf.jgabl2.adapt.IGenericAdaptable;
17  import net.sf.jgabl2.adapt.IGenericAdapterManager;
18  import net.sf.jgabl2.adapt.UnavailableAdapterException;
19  
20  /**
21   * A first implementation of an {@link IGenericAdapterManager}.
22   *
23   * @author Alexander Schwartz
24   * @since 0.5.0
25   */
26  public final class GenericAdapterManagerImpl
27          implements IGenericAdapterManager {
28      /** {@inheritDoc} */
29      @SuppressWarnings("unchecked")
30      public <AdapterType> AdapterType getAdapter(
31                                          final Object object,
32                                          final Class<?> type)
33              throws UnavailableAdapterException {
34          if (type.isInstance(object)) {
35              return (AdapterType) object;
36          }
37  
38          if (object instanceof IGenericAdaptable) {
39              final IGenericAdaptable adaptable = (IGenericAdaptable) object;
40  
41              return adaptable.<AdapterType>getAdapter(type);
42          }
43  
44          throw new UnavailableAdapterException("No adapter of type '" + type
45              + "' available for '" //$NON-NLS-1$
46              + object + "'"); //$NON-NLS-1$
47      }
48  }