KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > tbutils > res > CompositeResourceProvider


1 package com.tonbeller.tbutils.res;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Set JavaDoc;
9
10 /**
11  * Composite ResourceProvider.
12  *
13  * Contains an ordered list of {@link ResourceProvider}'s. For a given key
14  * it iterates the list and returns the value of the first ResourceProvider
15  * that knows about the key.
16  * <p />
17  */

18
19 public class CompositeResourceProvider implements ResourceProvider {
20   List JavaDoc list = new ArrayList JavaDoc();
21
22   public String JavaDoc getString(String JavaDoc key) {
23     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
24       ResourceProvider r = (ResourceProvider) it.next();
25       String JavaDoc s = r.getString(key);
26       if (s != null)
27         return s;
28     }
29     return null;
30   }
31
32   public void add(ResourceProvider r) {
33     list.add(r);
34   }
35
36   public void add(int index, ResourceProvider r) {
37     list.add(index, r);
38   }
39
40   public List JavaDoc getProviders() {
41     return list;
42   }
43
44   public Collection JavaDoc keySet() {
45     Set JavaDoc set = new HashSet JavaDoc();
46     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
47       ResourceProvider r = (ResourceProvider) it.next();
48       set.addAll(r.keySet());
49     }
50     return set;
51   }
52
53   public void close() {
54     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
55       ResourceProvider r = (ResourceProvider) it.next();
56       r.close();
57     }
58   }
59
60   public void dump(Dumper d) {
61     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
62       ResourceProvider r = (ResourceProvider) it.next();
63       r.dump(d);
64     }
65   }
66
67   public String JavaDoc getName() {
68     return "CompositeResourceProvider";
69   }
70 }
Popular Tags