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