View Javadoc

1   /*
2    * Copyright 2006-2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.service.importer;
18  
19  import org.osgi.framework.Filter;
20  import org.springframework.util.ObjectUtils;
21  
22  /**
23   * Default, immutable implementation for {@link OsgiServiceDependency}.
24   * 
25   * @author Costin Leau
26   * 
27   */
28  public class DefaultOsgiServiceDependency implements OsgiServiceDependency {
29  
30  	private final String beanName;
31  	private final Filter filter;
32  	private final boolean mandatoryService;
33  	private final String toString;
34  	private final int hashCode;
35  
36  
37  	/**
38  	 * Constructs a new <code>DefaultOsgiServiceDependency</code> instance.
39  	 * 
40  	 * @param beanName dependency bean name (can be null)
41  	 * @param filter dependency OSGi filter (can be null)
42  	 * @param mandatoryService flag indicating whether the dependency is
43  	 * mandatory or not
44  	 */
45  	public DefaultOsgiServiceDependency(String beanName, Filter filter, boolean mandatoryService) {
46  		this.beanName = beanName;
47  		this.filter = filter;
48  		this.mandatoryService = mandatoryService;
49  
50  		// calculate internal fields
51  		toString = "DependencyService[Name=" + (beanName != null ? beanName : "null") + "][Filter=" + filter
52  				+ "][Mandatory=" + mandatoryService + "]";
53  
54  		int result = 17;
55  		result = 37 * result + DefaultOsgiServiceDependency.class.hashCode();
56  		result = 37 * result + (filter == null ? 0 : filter.hashCode());
57  		result = 37 * result + (beanName == null ? 0 : beanName.hashCode());
58  		result = 37 * result + (mandatoryService ? 0 : 1);
59  		hashCode = result;
60  	}
61  
62  	public String getBeanName() {
63  		return beanName;
64  	}
65  
66  	public Filter getServiceFilter() {
67  		return filter;
68  	}
69  
70  	public boolean isMandatory() {
71  		return mandatoryService;
72  	}
73  
74  	public String toString() {
75  		return toString;
76  	}
77  
78  	public boolean equals(Object obj) {
79  		if (obj instanceof OsgiServiceDependency) {
80  			OsgiServiceDependency other = (OsgiServiceDependency) obj;
81  			return (other.isMandatory() == mandatoryService && filter.equals(other.getServiceFilter()) && ObjectUtils.nullSafeEquals(
82  				beanName, other.getBeanName()));
83  		}
84  		return false;
85  	}
86  
87  	public int hashCode() {
88  		return hashCode;
89  	}
90  }