View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
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   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util.component;
20  
21  import java.util.Collection;
22  
23  public interface Container
24  {
25      /* ------------------------------------------------------------ */
26      /**
27       * Add a bean.  If the bean is-a {@link Listener}, then also do an implicit {@link #addEventListener(Listener)}.
28       * @param o the bean object to add
29       * @return true if the bean was added, false if it was already present
30       */
31      public boolean addBean(Object o);
32  
33      /**
34       * @return the list of beans known to this aggregate
35       * @see #getBean(Class)
36       */
37      public Collection<Object> getBeans();
38  
39      /**
40       * @param clazz the class of the beans
41       * @return the list of beans of the given class (or subclass)
42       * @see #getBeans()
43       */
44      public <T> Collection<T> getBeans(Class<T> clazz);
45  
46      /**
47       * @param clazz the class of the bean
48       * @return the first bean of a specific class (or subclass), or null if no such bean exist
49       */
50      public <T> T getBean(Class<T> clazz);
51  
52      /**
53       * Removes the given bean.
54       * If the bean is-a {@link Listener}, then also do an implicit {@link #removeEventListener(Listener)}.
55       * @return whether the bean was removed
56       * @see #removeBeans()
57       */
58      public boolean removeBean(Object o);
59      
60      /**
61       * Add an event listener. 
62       * @see Container#addBean(Object), which also adds listeners if the bean is-a Listener
63       * @param listener
64       */
65      public void addEventListener(Listener listener);
66      
67      /**
68       * Remove an event listener. 
69       * @see Container#removeBean(Object), which also adds listeners if the bean is-a Listener
70       * @param listener
71       */
72      public void removeEventListener(Listener listener);
73  
74      /**
75       * A listener for Container events.
76       * If an added bean implements this interface it will receive the events
77       * for this container.
78       */
79      public interface Listener
80      {
81          void beanAdded(Container parent,Object child);
82          void beanRemoved(Container parent,Object child);
83      }
84      
85      /**
86       * Inherited Listener.
87       * If an added bean implements this interface, then it will 
88       * be added to all contained beans that are themselves Containers
89       */
90      public interface InheritedListener extends Listener
91      {
92      }
93  }