View Javadoc

1   /*
2    * Copyright 2006-2008 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.Assert;
21  import org.springframework.util.ObjectUtils;
22  
23  /**
24   * Default, immutable implementation for {@link OsgiServiceDependency}.
25   * 
26   * @author Costin Leau
27   * 
28   */
29  public class DefaultOsgiServiceDependency implements OsgiServiceDependency {
30  
31  	private final String beanName;
32  	private final Filter filter;
33  	private final boolean mandatoryService;
34  	private final String toString;
35  	private final int hashCode;
36  
37  
38  	/**
39  	 * Constructs a new <code>DefaultOsgiServiceDependency</code> instance.
40  	 * 
41  	 * @param beanName dependency bean name (can be null)
42  	 * @param filter dependency OSGi filter (can be null)
43  	 * @param mandatoryService flag indicating whether the dependency is
44  	 * mandatory or not
45  	 */
46  	public DefaultOsgiServiceDependency(String beanName, Filter filter, boolean mandatoryService) {
47  		this.beanName = beanName;
48  		this.filter = filter;
49  		this.mandatoryService = mandatoryService;
50  
51  		// calculate internal fields
52  		toString = "DependencyService[Name=" + (beanName != null ? beanName : "null") + "][Filter=" + filter
53  				+ "][Mandatory=" + mandatoryService + "]";
54  
55  		int result = 17;
56  		result = 37 * result + DefaultOsgiServiceDependency.class.hashCode();
57  		result = 37 * result + (filter == null ? 0 : filter.hashCode());
58  		result = 37 * result + (beanName == null ? 0 : beanName.hashCode());
59  		result = 37 * result + (mandatoryService ? 0 : 1);
60  		hashCode = result;
61  	}
62  
63  	public String getBeanName() {
64  		return beanName;
65  	}
66  
67  	public Filter getServiceFilter() {
68  		return filter;
69  	}
70  
71  	public boolean isMandatory() {
72  		return mandatoryService;
73  	}
74  
75  	public String toString() {
76  		return toString;
77  	}
78  
79  	public boolean equals(Object obj) {
80  		if (obj instanceof OsgiServiceDependency) {
81  			OsgiServiceDependency other = (OsgiServiceDependency) obj;
82  			return (other.isMandatory() == mandatoryService && filter.equals(other.getServiceFilter()) && ObjectUtils.nullSafeEquals(
83  				beanName, other.getBeanName()));
84  		}
85  		return false;
86  	}
87  
88  	public int hashCode() {
89  		return hashCode;
90  	}
91  }