001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean;
017:
018: import java.io.Serializable;
019: import java.util.Collections;
020: import java.util.LinkedHashSet;
021: import java.util.Set;
022:
023: /**
024: * @version $Rev: 556119 $ $Date: 2007-07-13 12:34:02 -0700 (Fri, 13 Jul 2007) $
025: */
026: public class ReferencePatterns implements Serializable {
027: private static final long serialVersionUID = 1888371271299507818L;
028:
029: private final Set<AbstractNameQuery> patterns;
030: private final AbstractName abstractName;
031:
032: public ReferencePatterns(Set<? extends Object> patterns) {
033: this .patterns = new LinkedHashSet<AbstractNameQuery>();
034: for (Object pattern : patterns) {
035: if (pattern instanceof AbstractName) {
036: AbstractName name = (AbstractName) pattern;
037: this .patterns.add(new AbstractNameQuery(name));
038: } else if (pattern instanceof AbstractNameQuery) {
039: AbstractNameQuery nameQuery = (AbstractNameQuery) pattern;
040: this .patterns.add(nameQuery);
041: } else {
042: throw new IllegalArgumentException(
043: "Unknown pattern type: " + pattern);
044: }
045: }
046: this .abstractName = null;
047: }
048:
049: public ReferencePatterns(AbstractNameQuery abstractNameQuery) {
050: this .patterns = Collections.singleton(abstractNameQuery);
051: this .abstractName = null;
052: }
053:
054: public ReferencePatterns(AbstractName abstractName) {
055: if (abstractName == null) {
056: throw new IllegalArgumentException(
057: "parameter abstractName is null");
058: }
059: this .abstractName = abstractName;
060: this .patterns = null;
061: }
062:
063: public Set<AbstractNameQuery> getPatterns() {
064: if (patterns == null) {
065: throw new IllegalStateException("This is resolved to: "
066: + abstractName);
067: }
068: return patterns;
069: }
070:
071: public AbstractName getAbstractName() {
072: if (abstractName == null) {
073: throw new IllegalStateException(
074: "This is not resolved with patterns: " + patterns);
075: }
076: return abstractName;
077: }
078:
079: public boolean isResolved() {
080: return abstractName != null;
081: }
082:
083: public String toString() {
084: if (abstractName != null) {
085: return abstractName.toString();
086: } else {
087: return patterns.toString();
088: }
089: }
090:
091: public boolean equals(Object other) {
092: if (other instanceof ReferencePatterns) {
093: ReferencePatterns otherRefPat = (ReferencePatterns) other;
094: if (abstractName != null) {
095: return abstractName.equals(otherRefPat.abstractName);
096: }
097: return patterns.equals(otherRefPat.patterns);
098: }
099: return false;
100: }
101:
102: public int hashCode() {
103: if (abstractName != null) {
104: return abstractName.hashCode();
105: }
106: return patterns.hashCode();
107: }
108: }
|