HibernateUserAdministrationTest now based on the component mechanism.
[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         private String _uniqueId; 
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                 _uniqueId = null; 
47         }
48
49         @Override
50         public String getName() {
51                 return _name;
52         }
53         
54         @Override
55         public Class[] getInterfaceTypes() {
56                 return _interfaces;
57         }
58
59         @Override
60         public void setUniqueId(String aId) {
61                 _uniqueId = aId;        
62         }
63         
64         @Override
65         public String getUniqueId() {
66                 return _uniqueId;
67         }
68         
69         @Override
70         public void publish(Object aImplementation, Scope aScope) {
71                 aScope.publishInterface(this, aImplementation);
72         }
73         
74         @Override
75         public String toString() {
76                 StringBuffer buf = new StringBuffer();
77                 buf.append(getName());
78                 buf.append(":");
79                 for (Class intf: _interfaces) { 
80                         buf.append(" " + intf.getName());
81                 }
82                 return buf.toString();
83         }
84         
85         @Override
86         public boolean equals(Object aObj) {
87             if ( !(aObj instanceof DefaultProvidedInterface)) { 
88                 return false; 
89             }
90             DefaultProvidedInterface provided = (DefaultProvidedInterface)aObj; 
91             return getEqualsRepresentation().equals(provided.getEqualsRepresentation());
92         }
93         
94         @Override
95         public int hashCode() {
96             return getEqualsRepresentation().hashCode();
97         }
98         
99         
100         private String getEqualsRepresentation() { 
101             List<String> result = new ArrayList<String>(); 
102             for (Class cls: _interfaces) { 
103                 result.add(cls.getName());
104             }
105             Collections.sort(result); 
106             String value = ""; 
107             for (String str: result) { 
108                 value += ":" + str; 
109             }
110             return value; 
111         }
112 }