Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javax/ide/model/spi/ModelAdapterFactory.java


1   package javax.ide.model.spi;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import java.util.Collections;
7   import java.util.Iterator;
8   import java.util.List;
9   import javax.ide.model.Element;
10  import javax.ide.spi.LookupProvider;
11  import javax.ide.spi.ProviderNotFoundException;
12  
13  /**
14   * A model apapter factory is responsible for creating the best possible 
15   * adapter for a given element. The adapter is responsible for managing the 
16   * communication between JSR model objects and IDE specific model objects.
17   */
18  public abstract class ModelAdapterFactory
19  {
20    private static ModelAdapterFactory _impl;
21  
22    /**
23     * Creates an instance of an adapter that will delegate requests to IDE 
24     * specific model objects.
25     * 
26     * @param element the element that the newly created adapter adapts.
27     * @return an adapter instance.
28     */
29    public abstract ElementImpl getImpl( Element element );
30  
31    /**
32     * Get the ModelAdpaterFactory implementation for this IDE.
33     * 
34     * @return the DocumentFactory implementation for this IDE.
35     */
36    public static ModelAdapterFactory getModelAdapterFactory()
37    {
38      try
39      {
40        if ( _impl == null )
41        {
42          Collection impls = LookupProvider.lookupAll(
43            Thread.currentThread().getContextClassLoader(), 
44            ModelAdapterFactory.class
45          );
46          if ( impls.size() == 1 )
47          {
48            _impl = (ModelAdapterFactory) impls.iterator().next();
49          }
50          else
51          {
52            List copy = new ArrayList( impls );
53            Collections.reverse( copy );
54            _impl = new ChainedFactories( copy );
55          }
56        }
57      }
58      catch ( ProviderNotFoundException pnfe )
59      {
60        throw new IllegalStateException( "No model adapter factory." );
61      }
62      
63      return _impl;
64    }
65    
66    
67    private static final class ChainedFactories extends ModelAdapterFactory
68    {
69      private final List _factories;
70      
71      ChainedFactories( List factories )
72      {
73        _factories = factories;
74      }
75    
76      public ElementImpl getImpl( Element element )
77      {
78        for ( Iterator i = _factories.iterator(); i.hasNext(); )
79        {
80          ModelAdapterFactory f = (ModelAdapterFactory) i.next();
81          ElementImpl result = f.getImpl( element );
82          if ( result != null )
83          {
84            return result;
85          }
86        }
87        return null;
88      }
89    }
90    
91  }