KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > context > PackageContext


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package cintoo.messages.context;
17
18 import api.cintoo.messages.context.Context;
19
20 /**
21  * Context implementation with a scope of Java packages.
22  * With this class bundles can be associated with
23  * packages.
24  *
25  * @author Stephan J. Schmidt
26  * @version $id$
27  * @since 1.0
28  */

29 public class PackageContext implements Context {
30   private String JavaDoc packageName;
31
32   /**
33    * Factory method to create a PackageContext from a string
34    *
35    * @param packageName name of the package which defines the scope
36    * @return PackageContext with a scope of the package
37    */

38   public static PackageContext string(String JavaDoc packageName) {
39     PackageContext context = new PackageContext();
40     context.setPackage(packageName);
41     return context;
42   }
43
44   /**
45    * Construct a PackageContext with a global scope
46    * for all packages
47    */

48   public PackageContext() {
49     packageName = "";
50   }
51
52   /**
53    * Construct a PackageContext specific to an object
54    *
55    * @param object object from which to use the package information
56    */

57   public PackageContext(Object JavaDoc object) {
58     packageName = getContext(object);
59   }
60
61   /**
62    * Set the pattern of package names for the context
63    * like "only.this.package."
64    *
65    * @param packageName pattern to use for scope matching
66    */

67   private void setPackage(String JavaDoc packageName) {
68     this.packageName = packageName;
69   }
70
71   /**
72    * Get the package context for an object
73    *
74    * @param context object which defines the scope
75    * @return string with the package of the object
76    */

77   private String JavaDoc getContext(Object JavaDoc context) {
78     if (null == context || context.getClass().getPackage() == null) {
79       return "";
80     } else {
81       if (context instanceof Class JavaDoc) {
82         return ((Class JavaDoc) context).getPackage().getName();
83       } else {
84         return context.getClass().getPackage().getName();
85       }
86     }
87   }
88
89   /**
90    * Ask the context if it matches another context.
91    *
92    * @param otherContext context to check against
93    * @return true if the context matches the other context
94    */

95   public boolean matches(Context otherContext) {
96     if (!(otherContext instanceof PackageContext)) return false;
97     return ((PackageContext) otherContext).packageName.startsWith(packageName);
98   }
99
100   /**
101    * Returns the package name of the context
102    *
103    * @return package name of the context
104    */

105   public String JavaDoc getName() {
106     return packageName;
107   }
108
109   public boolean equals(Object JavaDoc o) {
110     if (this == o) return true;
111     if (!(o instanceof PackageContext)) return false;
112
113     final PackageContext packageContext = (PackageContext) o;
114
115     if (packageName != null ? !packageName.equals(packageContext.packageName) : packageContext.packageName != null)
116       return false;
117
118     return true;
119   }
120
121   public int hashCode() {
122     return packageName != null ? packageName.hashCode() : 0;
123   }
124
125   /**
126    * Implementation of the Comparator interface.
127    * PackageContexts are more or less specifica about their scope.
128    * "my.package. is more specific than "my.". As we need
129    * to find the most specific / narrow scope the contexts
130    * need to be comparable. The package context with the
131    * longest pattern is the most specific.
132    *
133    * @param o other context
134    * @return according to the comparator interfaace
135    */

136   public int compareTo(Object JavaDoc o) {
137     if (!(o instanceof PackageContext)) return 0;
138
139     String JavaDoc otherPackageName = ((PackageContext) o).packageName;
140
141     int l1 = packageName.length();
142     int l2 = otherPackageName.length();
143
144     // only return 0 if both are identical
145
if (l1 == l2) return packageName.compareTo(otherPackageName);
146     return l1 > l2 ? -1 : 1;
147   }
148
149   public String JavaDoc toString() {
150     return "<" + packageName + ">";
151   }
152 }
153
Popular Tags