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