(no commit message)
[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 String toString() {
60                 StringBuffer buf = new StringBuffer();
61                 buf.append(getName());
62                 buf.append(":");
63                 for (Class intf: _interfaces) { 
64                         buf.append(" " + intf.getName());
65                 }
66                 return buf.toString();
67         }
68         
69         @Override
70         public boolean equals(Object aObj) {
71             return this == aObj;
72             /*
73             if ( !(aObj instanceof DefaultProvidedInterface)) { 
74                 return false; 
75             }
76             DefaultProvidedInterface provided = (DefaultProvidedInterface)aObj; 
77             return getEqualsRepresentation().equals(provided.getEqualsRepresentation());
78             */
79         }
80         
81         @Override
82         public int hashCode() {
83             return getEqualsRepresentation().hashCode();
84         }
85         
86         @Override
87         public boolean covers(ProvidedInterface aInterface) {
88             // TODO do more than just equals. 
89             if ( !(aInterface instanceof DefaultProvidedInterface)) { 
90                 return false; 
91             }
92             return getEqualsRepresentation().equals(((DefaultProvidedInterface)aInterface).getEqualsRepresentation());
93         }
94         
95         
96         private String getEqualsRepresentation() { 
97             List<String> result = new ArrayList<String>(); 
98             for (Class cls: _interfaces) { 
99                 result.add(cls.getName());
100             }
101             Collections.sort(result); 
102             String value = ""; 
103             for (String str: result) { 
104                 value += ":" + str; 
105             }
106             return value; 
107         }
108 }