Package rename org.wamblee.system to org.wamblee.system.core
[utils] / system / general / src / main / java / org / wamblee / system / core / DefaultProvidedInterface.java
diff --git a/system/general/src/main/java/org/wamblee/system/core/DefaultProvidedInterface.java b/system/general/src/main/java/org/wamblee/system/core/DefaultProvidedInterface.java
new file mode 100644 (file)
index 0000000..b751a67
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2007 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+package org.wamblee.system.core;
+
+import java.util.Arrays;
+
+/**
+ * Default implementation of a service descriptor.
+ *
+ * @author Erik Brakkee
+ */
+public class DefaultProvidedInterface implements ProvidedInterface {
+       
+       private String _name; 
+       private Class[] _interfaces;
+       private Object _implementation; 
+       
+       /**
+        * Constructs the descriptor. 
+        * @param aInterface Type of service. 
+        */
+       public DefaultProvidedInterface(String aName, Class aInterface) {
+               _name = aName; 
+               _interfaces = new Class[] { aInterface };  
+       }
+       
+       public DefaultProvidedInterface(String aName, Class[] aInterfaces) {
+               _name = aName; 
+               _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length);  
+       }
+
+       @Override
+       public String getName() {
+               return _name;
+       }
+       
+       @Override
+       public Class[] getInterfaceTypes() {
+               return _interfaces;
+       }
+       
+       @Override
+       public void publish(Object aImplementation) {
+               _implementation = aImplementation;      
+       }
+       
+       @Override
+       public Object getImplementation() {
+               return _implementation; 
+       }
+       
+       @Override
+       public boolean equals(Object obj) {
+               if ( !(obj instanceof DefaultProvidedInterface)) { 
+                       return false; 
+               }
+               DefaultProvidedInterface descr = (DefaultProvidedInterface)obj;
+               if ( _interfaces.length != descr._interfaces.length ) { 
+                       return false; 
+               }
+               String[] interfaces1 = new String[_interfaces.length];
+               String[] interfaces2 = new String[_interfaces.length];
+               for (int i = 0; i < _interfaces.length; i++) {  
+                       interfaces1[i] = _interfaces[i].getName();
+                       interfaces2[i] = descr._interfaces[i].getName();
+               }
+               Arrays.sort(interfaces1);
+               Arrays.sort(interfaces2);
+               return Arrays.equals(interfaces1, interfaces2);
+       }
+
+       @Override
+       public int hashCode() {
+               return _interfaces.hashCode(); 
+       }
+       
+       @Override
+       public String toString() {
+               StringBuffer buf = new StringBuffer();
+               for (Class intf: _interfaces) { 
+                       buf.append("." + intf.getName());
+               }
+               return buf.toString();
+       }
+}