KickJava   Java API By Example, From Geeks To Geeks.

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


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 bundle IDs.
22  * With this class bundles can be associated with
23  * IDs. Different parts of an application can use
24  * different IDs (installer, plugins, ...)
25  *
26  * @author Stephan J. Schmidt
27  * @version $id$
28  * @since 1.0
29  */

30 public class IdContext implements Context {
31
32     private String JavaDoc id;
33
34     /**
35      * Factory method to construct IdContext from id string
36      *
37      * @param id id to construct IdContext from
38      * @return new IdContext
39      */

40     public static Context id(String JavaDoc id) {
41         return new IdContext(id);
42     }
43
44     /**
45      * Construct IdContext from id string
46      *
47      * @param id id of the id context
48      */

49     public IdContext(String JavaDoc id) {
50         this.id = id;
51     }
52
53     /**
54      * Ask the context if it matches another context.
55      *
56      * @param otherContext context to check against
57      * @return true if the context matches the other context
58      */

59     public boolean matches(Context otherContext) {
60         if (!(otherContext instanceof IdContext)) return false;
61         return id.equals(((IdContext) otherContext).id);
62     }
63
64     /**
65      * Implementation of the Comparator interface.
66      * There are no IdContexts which are more specific
67      * so this
68      *
69      * @param o other context
70      * @return according to the comparator interface
71      */

72     public int compareTo(Object JavaDoc o) {
73         return 0;
74     }
75
76     public boolean equals(Object JavaDoc o) {
77         if (this == o) return true;
78         if (!(o instanceof IdContext)) return false;
79
80         final IdContext idContext = (IdContext) o;
81
82         if (id != null ? !id.equals(idContext.id) : idContext.id != null) return false;
83
84         return true;
85     }
86
87     public int hashCode() {
88         return id != null ? id.hashCode() : 0;
89     }
90
91     public String JavaDoc toString() {
92         return "<id:" + id + ">";
93     }
94 }
95
Popular Tags