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