KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > ReferencePatterns


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

17
18 package org.apache.geronimo.gbean;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public class ReferencePatterns implements Serializable JavaDoc {
30     private static final long serialVersionUID = 1888371271299507818L;
31
32     private final Set JavaDoc patterns;
33     private final AbstractName abstractName;
34
35     public ReferencePatterns(Set JavaDoc patterns) {
36         this.patterns = new LinkedHashSet JavaDoc();
37         for (Iterator JavaDoc iterator = patterns.iterator(); iterator.hasNext();) {
38             Object JavaDoc pattern = iterator.next();
39             if (pattern instanceof AbstractName) {
40                 AbstractName name = (AbstractName) pattern;
41                 this.patterns.add(new AbstractNameQuery(name));
42             } else if (pattern instanceof AbstractNameQuery) {
43                 AbstractNameQuery nameQuery = (AbstractNameQuery) pattern;
44                 this.patterns.add(nameQuery);
45             } else {
46                 throw new IllegalArgumentException JavaDoc("Unknown pattern type: " + pattern);
47             }
48         }
49         this.abstractName = null;
50     }
51
52     public ReferencePatterns(AbstractNameQuery abstractNameQuery) {
53         this.patterns = Collections.singleton(abstractNameQuery);
54         this.abstractName = null;
55     }
56
57     public ReferencePatterns(AbstractName abstractName) {
58         if (abstractName == null) {
59             throw new IllegalArgumentException JavaDoc("parameter abstractName is null");
60         }
61         this.abstractName = abstractName;
62         this.patterns = null;
63     }
64
65     public Set JavaDoc getPatterns() {
66         if (patterns == null) {
67             throw new IllegalStateException JavaDoc("This is resolved to: " + abstractName);
68         }
69         return patterns;
70     }
71
72     public AbstractName getAbstractName() {
73         if (abstractName == null) {
74             throw new IllegalStateException JavaDoc("This is not resolved with patterns: " + patterns);
75         }
76         return abstractName;
77     }
78
79     public boolean isResolved() {
80         return abstractName != null;
81     }
82
83     public String JavaDoc toString() {
84         if (abstractName != null) {
85             return abstractName.toString();
86         } else {
87             return patterns.toString();
88         }
89     }
90
91     public boolean equals(Object JavaDoc other) {
92         if (other instanceof ReferencePatterns) {
93             ReferencePatterns otherRefPat = (ReferencePatterns) other;
94             if (abstractName != null) {
95                 return abstractName.equals(otherRefPat.abstractName);
96             }
97             return patterns.equals(otherRefPat.patterns);
98         }
99         return false;
100     }
101
102     public int hashCode() {
103         if (abstractName != null) {
104             return abstractName.hashCode();
105         }
106         return patterns.hashCode();
107     }
108 }
109
Popular Tags