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.LinkedHashSet;
023    import java.util.Set;
024    
025    /**
026     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
027     */
028    public class ReferencePatterns implements Serializable {
029        private static final long serialVersionUID = 1888371271299507818L;
030    
031        private final Set<AbstractNameQuery> patterns;
032        private final AbstractName abstractName;
033    
034        public ReferencePatterns(Set<? extends Object> patterns) {
035            this.patterns = new LinkedHashSet<AbstractNameQuery>();
036            for (Object pattern : patterns) {
037                if (pattern instanceof AbstractName) {
038                    AbstractName name = (AbstractName) pattern;
039                    this.patterns.add(new AbstractNameQuery(name));
040                } else if (pattern instanceof AbstractNameQuery) {
041                    AbstractNameQuery nameQuery = (AbstractNameQuery) pattern;
042                    this.patterns.add(nameQuery);
043                } else {
044                    throw new IllegalArgumentException("Unknown pattern type: " + pattern);
045                }
046            }
047            this.abstractName = null;
048        }
049    
050        public ReferencePatterns(AbstractNameQuery abstractNameQuery) {
051            this.patterns = Collections.singleton(abstractNameQuery);
052            this.abstractName = null;
053        }
054    
055        public ReferencePatterns(AbstractName abstractName) {
056            if (abstractName == null) {
057                throw new IllegalArgumentException("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: " + abstractName);
066            }
067            return patterns;
068        }
069    
070        public AbstractName getAbstractName() {
071            if (abstractName == null) {
072                throw new IllegalStateException("This is not resolved with patterns: " + patterns);
073            }
074            return abstractName;
075        }
076    
077        public boolean isResolved() {
078            return abstractName != null;
079        }
080    
081        public String toString() {
082            if (abstractName != null) {
083                return abstractName.toString();
084            } else {
085                return patterns.toString();
086            }
087        }
088    
089        public boolean equals(Object other) {
090            if (other instanceof ReferencePatterns) {
091                ReferencePatterns otherRefPat = (ReferencePatterns) other;
092                if (abstractName != null) {
093                    return abstractName.equals(otherRefPat.abstractName);
094                }
095                return patterns.equals(otherRefPat.patterns);
096            }
097            return false;
098        }
099    
100        public int hashCode() {
101            if (abstractName != null) {
102                return abstractName.hashCode();
103            }
104            return patterns.hashCode();
105        }
106    }