View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.gbean;
20  
21  import java.io.Serializable;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.LinkedHashSet;
25  import java.util.Set;
26  
27  /**
28   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
29   */
30  public class ReferencePatterns implements Serializable {
31      private static final long serialVersionUID = 1888371271299507818L;
32  
33      private final Set patterns;
34      private final AbstractName abstractName;
35  
36      public ReferencePatterns(Set patterns) {
37          this.patterns = new LinkedHashSet();
38          for (Iterator iterator = patterns.iterator(); iterator.hasNext();) {
39              Object pattern = iterator.next();
40              if (pattern instanceof AbstractName) {
41                  AbstractName name = (AbstractName) pattern;
42                  this.patterns.add(new AbstractNameQuery(name));
43              } else if (pattern instanceof AbstractNameQuery) {
44                  AbstractNameQuery nameQuery = (AbstractNameQuery) pattern;
45                  this.patterns.add(nameQuery);
46              } else {
47                  throw new IllegalArgumentException("Unknown pattern type: " + pattern);
48              }
49          }
50          this.abstractName = null;
51      }
52  
53      public ReferencePatterns(AbstractNameQuery abstractNameQuery) {
54          this.patterns = Collections.singleton(abstractNameQuery);
55          this.abstractName = null;
56      }
57  
58      public ReferencePatterns(AbstractName abstractName) {
59          if (abstractName == null) {
60              throw new IllegalArgumentException("parameter abstractName is null");
61          }
62          this.abstractName = abstractName;
63          this.patterns = null;
64      }
65  
66      public Set getPatterns() {
67          if (patterns == null) {
68              throw new IllegalStateException("This is resolved to: " + abstractName);
69          }
70          return patterns;
71      }
72  
73      public AbstractName getAbstractName() {
74          if (abstractName == null) {
75              throw new IllegalStateException("This is not resolved with patterns: " + patterns);
76          }
77          return abstractName;
78      }
79  
80      public boolean isResolved() {
81          return abstractName != null;
82      }
83  
84      public String toString() {
85          if (abstractName != null) {
86              return abstractName.toString();
87          } else {
88              return patterns.toString();
89          }
90      }
91  
92      public boolean equals(Object other) {
93          if (other instanceof ReferencePatterns) {
94              ReferencePatterns otherRefPat = (ReferencePatterns) other;
95              if (abstractName != null) {
96                  return abstractName.equals(otherRefPat.abstractName);
97              }
98              return patterns.equals(otherRefPat.patterns);
99          }
100         return false;
101     }
102 
103     public int hashCode() {
104         if (abstractName != null) {
105             return abstractName.hashCode();
106         }
107         return patterns.hashCode();
108     }
109 }