2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.core;
18 import java.util.Arrays;
20 public class DefaultRequiredInterface implements RequiredInterface {
23 private boolean _optional;
24 private Class[] _required;
25 private ProvidedInterface _provider;
27 public DefaultRequiredInterface(String aName, Class aInterface) {
28 this(aName, new Class[] { aInterface });
31 public DefaultRequiredInterface(String aName, Class[] aInterfaces) {
32 this(aName, aInterfaces, false);
35 public DefaultRequiredInterface(String aName, Class aInterface, boolean aIsOptional) {
36 this(aName, new Class[] { aInterface }, aIsOptional );
40 public DefaultRequiredInterface(String aName, Class[] aInterfaces, boolean aIsOptional) {
42 _optional = aIsOptional;
43 _required = aInterfaces;
47 public String getName() {
52 public boolean isOptional() {
57 public boolean implementedBy(ProvidedInterface aDescriptor) {
58 Class[] provided = aDescriptor.getInterfaceTypes();
59 for (Class required : _required) {
60 if ( !serviceProvided(required, provided)) {
64 // all required interfaces are provided.
69 * Check if the required interface is implemented by one of the provided interfaces.
70 * @param aRequired required interface
71 * @param aProvided Provided interfaces.
74 private boolean serviceProvided(Class aRequired, Class[] aProvided) {
75 for (Class provided: aProvided) {
77 provided.asSubclass(aRequired);
79 } catch (ClassCastException e) {
80 // No match, try the next one.
87 public ProvidedInterface getProvider() {
92 public void setProvider(ProvidedInterface aProvider) {
93 _provider = aProvider;
97 public <T> T getImplementation(Class<T> aClass) {
98 if ( _provider == null ) {
101 return (T)_provider.getImplementation();
105 public boolean equals(Object obj) {
106 if ( !(obj instanceof DefaultRequiredInterface)) {
109 DefaultRequiredInterface descr = (DefaultRequiredInterface)obj;
110 if ( _required.length != descr._required.length ) {
113 String[] interfaces1 = new String[_required.length];
114 String[] interfaces2 = new String[_required.length];
115 for (int i = 0; i < _required.length; i++) {
116 interfaces1[i] = _required[i].getName();
117 interfaces2[i] = descr._required[i].getName();
119 Arrays.sort(interfaces1);
120 Arrays.sort(interfaces2);
121 return Arrays.equals(interfaces1, interfaces2);
125 public int hashCode() {
126 return _required.hashCode();
130 public String toString() {
131 StringBuffer buf = new StringBuffer();
132 for (Class intf: _required) {
133 buf.append("." + intf.getName());
135 return buf.toString();