KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > util > resource > OrResourceLoader


1 package jfun.yan.util.resource;
2
3 import java.io.InputStream JavaDoc;
4 import java.net.URL JavaDoc;
5
6
7 /**
8  * This class represents a ResourceLoader that
9  * loads resource using a primary ResourceLoader object,
10  * and tries an alternative ResourceLoader if the primary doesn't
11  * find a resource.
12  * <p>
13  * @author Ben Yu
14  * Jan 16, 2006 4:08:55 PM
15  */

16 public class OrResourceLoader implements ResourceLoader {
17   private final ResourceLoader primary;
18   private final ResourceLoader alt;
19   public URL JavaDoc getResource(String JavaDoc path) {
20     URL JavaDoc ret = primary.getResource(path);
21     if(ret==null){
22       ret = alt.getResource(path);
23     }
24     return ret;
25   }
26
27   public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
28     InputStream JavaDoc ret = primary.getResourceAsStream(path);
29     if(ret==null){
30       ret = alt.getResourceAsStream(path);
31     }
32     return ret;
33   }
34
35   /**
36    * Create an OrResourceLoader object.
37    * @param primary the primary ResourceLoader.
38    * @param alt the alternative ResourceLoader.
39    */

40   public OrResourceLoader(ResourceLoader primary, ResourceLoader alt) {
41     this.alt = alt;
42     this.primary = primary;
43   }
44   /**
45    * Get the primary resource.
46    */

47   public ResourceLoader getPrimary(){
48     return primary;
49   }
50   /**
51    * Get the alternative resource.
52    */

53   public ResourceLoader getAlternative(){
54     return alt;
55   }
56
57   public boolean equals(Object JavaDoc obj) {
58     if(obj instanceof OrResourceLoader){
59       final OrResourceLoader other = (OrResourceLoader)obj;
60       return primary.equals(other.primary) && alt.equals(other.alt);
61     }
62     else return false;
63   }
64
65   public int hashCode() {
66     return primary.hashCode()*31+alt.hashCode();
67   }
68
69   public String JavaDoc toString() {
70     return primary.toString()+" | " + alt;
71   }
72 }
73
Popular Tags