(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / DefaultProvidedInterface.java
1 package org.wamblee.system;
2
3 import java.util.Arrays;
4
5 /**
6  * Default implementation of a service descriptor.
7  *
8  * @author Erik Brakkee
9  */
10 public class DefaultProvidedInterface implements ProvidedInterface {
11         
12         private String _name; 
13         private Class[] _interfaces;
14         private Object _implementation; 
15         
16         /**
17          * Constructs the descriptor. 
18          * @param aInterface Type of service. 
19          */
20         public DefaultProvidedInterface(String aName, Class aInterface) {
21                 _name = aName; 
22                 _interfaces = new Class[] { aInterface };  
23         }
24         
25         public DefaultProvidedInterface(String aName, Class[] aInterfaces) {
26                 _name = aName; 
27                 _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length);  
28         }
29
30         @Override
31         public String getName() {
32                 return _name;
33         }
34         
35         @Override
36         public Class[] getInterfaceTypes() {
37                 return _interfaces;
38         }
39         
40         @Override
41         public void publish(Object aImplementation) {
42                 _implementation = aImplementation;      
43         }
44         
45         @Override
46         public Object getImplementation() {
47                 return _implementation; 
48         }
49         
50         @Override
51         public boolean equals(Object obj) {
52                 if ( !(obj instanceof DefaultProvidedInterface)) { 
53                         return false; 
54                 }
55                 DefaultProvidedInterface descr = (DefaultProvidedInterface)obj;
56                 if ( _interfaces.length != descr._interfaces.length ) { 
57                         return false; 
58                 }
59                 String[] interfaces1 = new String[_interfaces.length];
60                 String[] interfaces2 = new String[_interfaces.length];
61                 for (int i = 0; i < _interfaces.length; i++) {  
62                         interfaces1[i] = _interfaces[i].getName();
63                         interfaces2[i] = descr._interfaces[i].getName();
64                 }
65                 Arrays.sort(interfaces1);
66                 Arrays.sort(interfaces2);
67                 return Arrays.equals(interfaces1, interfaces2);
68         }
69
70         @Override
71         public int hashCode() {
72                 return _interfaces.hashCode(); 
73         }
74         
75         @Override
76         public String toString() {
77                 StringBuffer buf = new StringBuffer();
78                 for (Class intf: _interfaces) { 
79                         buf.append("." + intf.getName());
80                 }
81                 return buf.toString();
82         }
83 }