removed the unique id from the provided interface. Now using object
[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.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.UUID;
23
24
25 /**
26  * Default implementation of a service descriptor.
27  *
28  * @author Erik Brakkee
29  */
30 public class DefaultProvidedInterface implements ProvidedInterface {
31         
32         private String _name; 
33         private Class[] _interfaces;
34         
35         /**
36          * Constructs the descriptor. 
37          * @param aInterface Type of service. 
38          */
39         public DefaultProvidedInterface(String aName, Class aInterface) {
40                 this(aName, new Class[] { aInterface }); 
41         }
42         
43         public DefaultProvidedInterface(String aName, Class[] aInterfaces) { 
44                 _name = aName; 
45                 _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length); 
46         }
47
48         @Override
49         public String getName() {
50                 return _name;
51         }
52         
53         @Override
54         public Class[] getInterfaceTypes() {
55                 return _interfaces;
56         }
57         
58         @Override
59         public void publish(Object aImplementation, Scope aScope) {
60                 aScope.publishInterface(this, aImplementation);
61         }
62         
63         @Override
64         public String toString() {
65                 StringBuffer buf = new StringBuffer();
66                 buf.append(getName());
67                 buf.append(":");
68                 for (Class intf: _interfaces) { 
69                         buf.append(" " + intf.getName());
70                 }
71                 return buf.toString();
72         }
73         
74         @Override
75         public boolean equals(Object aObj) {
76             return this == aObj;
77             /*
78             if ( !(aObj instanceof DefaultProvidedInterface)) { 
79                 return false; 
80             }
81             DefaultProvidedInterface provided = (DefaultProvidedInterface)aObj; 
82             return getEqualsRepresentation().equals(provided.getEqualsRepresentation());
83             */
84         }
85         
86         @Override
87         public int hashCode() {
88             return getEqualsRepresentation().hashCode();
89         }
90         
91         @Override
92         public boolean covers(ProvidedInterface aInterface) {
93             // TODO do more than just equals. 
94             if ( !(aInterface instanceof DefaultProvidedInterface)) { 
95                 return false; 
96             }
97             return getEqualsRepresentation().equals(((DefaultProvidedInterface)aInterface).getEqualsRepresentation());
98         }
99         
100         
101         private String getEqualsRepresentation() { 
102             List<String> result = new ArrayList<String>(); 
103             for (Class cls: _interfaces) { 
104                 result.add(cls.getName());
105             }
106             Collections.sort(result); 
107             String value = ""; 
108             for (String str: result) { 
109                 value += ":" + str; 
110             }
111             return value; 
112         }
113 }