Package rename org.wamblee.system to org.wamblee.system.core
[utils] / system / general / src / main / java / org / wamblee / system / core / DefaultProvidedInterface.java
1 /*
2  * Copyright 2007 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 package org.wamblee.system.core;
17
18 import java.util.Arrays;
19
20 /**
21  * Default implementation of a service descriptor.
22  *
23  * @author Erik Brakkee
24  */
25 public class DefaultProvidedInterface implements ProvidedInterface {
26         
27         private String _name; 
28         private Class[] _interfaces;
29         private Object _implementation; 
30         
31         /**
32          * Constructs the descriptor. 
33          * @param aInterface Type of service. 
34          */
35         public DefaultProvidedInterface(String aName, Class aInterface) {
36                 _name = aName; 
37                 _interfaces = new Class[] { aInterface };  
38         }
39         
40         public DefaultProvidedInterface(String aName, Class[] aInterfaces) {
41                 _name = aName; 
42                 _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length);  
43         }
44
45         @Override
46         public String getName() {
47                 return _name;
48         }
49         
50         @Override
51         public Class[] getInterfaceTypes() {
52                 return _interfaces;
53         }
54         
55         @Override
56         public void publish(Object aImplementation) {
57                 _implementation = aImplementation;      
58         }
59         
60         @Override
61         public Object getImplementation() {
62                 return _implementation; 
63         }
64         
65         @Override
66         public boolean equals(Object obj) {
67                 if ( !(obj instanceof DefaultProvidedInterface)) { 
68                         return false; 
69                 }
70                 DefaultProvidedInterface descr = (DefaultProvidedInterface)obj;
71                 if ( _interfaces.length != descr._interfaces.length ) { 
72                         return false; 
73                 }
74                 String[] interfaces1 = new String[_interfaces.length];
75                 String[] interfaces2 = new String[_interfaces.length];
76                 for (int i = 0; i < _interfaces.length; i++) {  
77                         interfaces1[i] = _interfaces[i].getName();
78                         interfaces2[i] = descr._interfaces[i].getName();
79                 }
80                 Arrays.sort(interfaces1);
81                 Arrays.sort(interfaces2);
82                 return Arrays.equals(interfaces1, interfaces2);
83         }
84
85         @Override
86         public int hashCode() {
87                 return _interfaces.hashCode(); 
88         }
89         
90         @Override
91         public String toString() {
92                 StringBuffer buf = new StringBuffer();
93                 for (Class intf: _interfaces) { 
94                         buf.append("." + intf.getName());
95                 }
96                 return buf.toString();
97         }
98 }