KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > notify > impl > AdapterFactoryImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: AdapterFactoryImpl.java,v 1.5 2005/06/08 06:19:08 nickb Exp $
16  */

17 package org.eclipse.emf.common.notify.impl;
18
19
20 import java.util.Iterator JavaDoc;
21
22 import org.eclipse.emf.common.notify.Adapter;
23 import org.eclipse.emf.common.notify.AdapterFactory;
24 import org.eclipse.emf.common.notify.Notifier;
25
26
27 /**
28  * An extensible adapter factory implementation.
29  */

30 public class AdapterFactoryImpl implements AdapterFactory
31 {
32   /**
33    * Creates an instance.
34    */

35   public AdapterFactoryImpl()
36   {
37   }
38
39   /**
40    * Returns <code>false</code>.
41    * @param type the key indicating the type of adapter in question.
42    * @return <code>false</code>.
43    */

44   public boolean isFactoryForType(Object JavaDoc type)
45   {
46     return false;
47   }
48
49   /**
50    * Returns either
51    * the result of calling {@link #adapt(Notifier,Object) adapt(Notifier, Object)}
52    * or the result of calling {@link #resolve resolve(Object, Object)},
53    * depending on whether the target is a notifier.
54    * @param target arbitrary object to adapt.
55    * @param type the key indicating the type of adapter required.
56    * @return either an associated adapter or the object itself.
57    * @see #adapt(Notifier,Object)
58    * @see #resolve(Object, Object)
59    */

60   public Object JavaDoc adapt(Object JavaDoc target, Object JavaDoc type)
61   {
62     if (target instanceof Notifier)
63     {
64       return adapt((Notifier)target, type);
65     }
66     else
67     {
68       return resolve(target, type);
69     }
70   }
71
72   /**
73    * Returns the object itself.
74    * This is called by {@link #adapt(Object,Object) adapt(Object, Object)} for objects that aren't notifiers.
75    * @param object arbitrary object to adapt.
76    * @param type the key indicating the type of adapter required.
77    * @return the object itself.
78    * @see #adapt(Object,Object)
79    */

80   protected Object JavaDoc resolve(Object JavaDoc object, Object JavaDoc type)
81   {
82     return object;
83   }
84
85   /*
86    * Javadoc copied from interface.
87    */

88   public Adapter adapt(Notifier target, Object JavaDoc type)
89   {
90     for (Iterator JavaDoc adapters = target.eAdapters().iterator(); adapters.hasNext(); )
91     {
92       Adapter adapter = (Adapter)adapters.next();
93       if (adapter.isAdapterForType(type))
94       {
95         return adapter;
96       }
97     }
98     return adaptNew(target, type);
99   }
100
101   /**
102    * Creates an adapter by calling {@link #createAdapter(Notifier, Object) createAdapter(Notifier, Object)}
103    * and associates it by calling {@link #associate(Adapter, Notifier) associate}.
104    * @param target the notifier to adapt.
105    * @param type the key indicating the type of adapter required.
106    * @return a new associated adapter.
107    * @see #createAdapter(Notifier, Object)
108    * @see #associate(Adapter, Notifier)
109    */

110   public Adapter adaptNew(Notifier target, Object JavaDoc type)
111   {
112     Adapter adapter = createAdapter(target, type);
113     associate(adapter, target);
114     return adapter;
115   }
116
117   /**
118    * Creates an adapter by calling {@link #createAdapter(Notifier) createAdapter(Notifier)}
119    * and associates it by calling {@link #associate(Adapter, Notifier) associate}.
120    * @param target notifier to adapt.
121    * @see #createAdapter(Notifier)
122    */

123   public void adaptAllNew(Notifier target)
124   {
125     Adapter adapter = createAdapter(target);
126     associate(adapter, target);
127   }
128
129   /**
130    * Creates an adapter by calling {@link #createAdapter(Notifier) createAdapter(Notifier)}.
131    * @param target the notifier to adapt.
132    * @param type the key indicating the type of adapter required.
133    * @return a new adapter.
134    * @see #createAdapter(Notifier)
135    */

136   protected Adapter createAdapter(Notifier target, Object JavaDoc type)
137   {
138     return createAdapter(target);
139   }
140
141   /**
142    * Creates an {@link org.eclipse.emf.common.notify.impl.AdapterImpl}.
143    * @param target the notifier to adapt.
144    * @return a new adapter.
145    * @see #createAdapter(Notifier)
146    */

147   protected Adapter createAdapter(Notifier target)
148   {
149     return new AdapterImpl();
150   }
151
152   /**
153    * Associates an adapter with a notifier by adding it to the target's {@link org.eclipse.emf.common.notify.Notifier#eAdapters adapters}.
154    * @param adapter the new adapter to associate.
155    * @param target the notifier being adapted.
156    */

157   protected void associate(Adapter adapter, Notifier target)
158   {
159     if (adapter != null)
160     {
161       target.eAdapters().add(adapter);
162     }
163   }
164 }
165
Popular Tags