source code formatting.
[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      * DOCUMENT ME!
32      */
33     private String name;
34
35     /**
36      * DOCUMENT ME!
37      */
38     private Class[] interfaces;
39
40 /**
41          * Constructs the descriptor. 
42          * @param aInterface Type of service. 
43          */
44     public DefaultProvidedInterface(String aName, Class aInterface) {
45         this(aName, new Class[] { aInterface });
46     }
47
48 /**
49      * Creates a new DefaultProvidedInterface object.
50      *
51      * @param aName DOCUMENT ME!
52      * @param aInterfaces DOCUMENT ME!
53      */
54     public DefaultProvidedInterface(String aName, Class[] aInterfaces) {
55         name           = aName;
56         interfaces     = Arrays.copyOf(aInterfaces, aInterfaces.length);
57     }
58
59     /**
60      * DOCUMENT ME!
61      *
62      * @return DOCUMENT ME!
63      */
64     @Override
65     public String getName() {
66         return name;
67     }
68
69     /**
70      * DOCUMENT ME!
71      *
72      * @return DOCUMENT ME!
73      */
74     @Override
75     public Class[] getInterfaceTypes() {
76         return interfaces;
77     }
78
79     /**
80      * DOCUMENT ME!
81      *
82      * @return DOCUMENT ME!
83      */
84     @Override
85     public String toString() {
86         StringBuffer buf = new StringBuffer();
87         buf.append(getName());
88         buf.append(":");
89
90         for (Class intf : interfaces) {
91             buf.append(" " + intf.getName());
92         }
93
94         return buf.toString();
95     }
96
97     /**
98      * DOCUMENT ME!
99      *
100      * @param aObj DOCUMENT ME!
101      *
102      * @return DOCUMENT ME!
103      */
104     @Override
105     public boolean equals(Object aObj) {
106         return this == aObj;
107
108         /*
109         if ( !(aObj instanceof DefaultProvidedInterface)) {
110             return false;
111         }
112         DefaultProvidedInterface provided = (DefaultProvidedInterface)aObj;
113         return getEqualsRepresentation().equals(provided.getEqualsRepresentation());
114         */
115     }
116
117     /**
118      * DOCUMENT ME!
119      *
120      * @return DOCUMENT ME!
121      */
122     @Override
123     public int hashCode() {
124         return getEqualsRepresentation().hashCode();
125     }
126
127     /**
128      * DOCUMENT ME!
129      *
130      * @param aInterface DOCUMENT ME!
131      *
132      * @return DOCUMENT ME!
133      */
134     @Override
135     public boolean covers(ProvidedInterface aInterface) {
136         // TODO do more than just equals. 
137         if (!(aInterface instanceof DefaultProvidedInterface)) {
138             return false;
139         }
140
141         return getEqualsRepresentation()
142         .equals(((DefaultProvidedInterface) aInterface).getEqualsRepresentation());
143     }
144
145     /**
146      * DOCUMENT ME!
147      *
148      * @return DOCUMENT ME!
149      */
150     private String getEqualsRepresentation() {
151         List<String> result = new ArrayList<String>();
152
153         for (Class cls : interfaces) {
154             result.add(cls.getName());
155         }
156
157         Collections.sort(result);
158
159         String value = "";
160
161         for (String str : result) {
162             value += (":" + str);
163         }
164
165         return value;
166     }
167 }