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     */
017    
018    package org.apache.geronimo.gbean;
019    
020    import java.io.Serializable;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.LinkedHashSet;
024    import java.util.Set;
025    
026    /**
027     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
028     */
029    public class ReferencePatterns implements Serializable {
030        private static final long serialVersionUID = 1888371271299507818L;
031    
032        private final Set patterns;
033        private final AbstractName abstractName;
034    
035        public ReferencePatterns(Set patterns) {
036            this.patterns = new LinkedHashSet();
037            for (Iterator iterator = patterns.iterator(); iterator.hasNext();) {
038                Object pattern = iterator.next();
039                if (pattern instanceof AbstractName) {
040                    AbstractName name = (AbstractName) pattern;
041                    this.patterns.add(new AbstractNameQuery(name));
042                } else if (pattern instanceof AbstractNameQuery) {
043                    AbstractNameQuery nameQuery = (AbstractNameQuery) pattern;
044                    this.patterns.add(nameQuery);
045                } else {
046                    throw new IllegalArgumentException("Unknown pattern type: " + pattern);
047                }
048            }
049            this.abstractName = null;
050        }
051    
052        public ReferencePatterns(AbstractNameQuery abstractNameQuery) {
053            this.patterns = Collections.singleton(abstractNameQuery);
054            this.abstractName = null;
055        }
056    
057        public ReferencePatterns(AbstractName abstractName) {
058            if (abstractName == null) {
059                throw new IllegalArgumentException("parameter abstractName is null");
060            }
061            this.abstractName = abstractName;
062            this.patterns = null;
063        }
064    
065        public Set getPatterns() {
066            if (patterns == null) {
067                throw new IllegalStateException("This is resolved to: " + abstractName);
068            }
069            return patterns;
070        }
071    
072        public AbstractName getAbstractName() {
073            if (abstractName == null) {
074                throw new IllegalStateException("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    }