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