/* * Copyright 2005-2010 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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * Default implementation of a service descriptor. * * @author Erik Brakkee */ public class DefaultProvidedInterface implements ProvidedInterface { private String name; private Class[] interfaces; /** * Constructs the descriptor. * * @param aInterface * Type of service. */ public DefaultProvidedInterface(String aName, Class aInterface) { this(aName, new Class[] { aInterface }); } /** * Creates a new DefaultProvidedInterface object. * */ 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 String toString() { StringBuffer buf = new StringBuffer(); buf.append(getName()); buf.append(":"); for (Class intf : interfaces) { buf.append(" " + intf.getName()); } return buf.toString(); } @Override public boolean equals(Object aObj) { return this == aObj; /* * if ( !(aObj instanceof DefaultProvidedInterface)) { return false; } * DefaultProvidedInterface provided = (DefaultProvidedInterface)aObj; * return * getEqualsRepresentation().equals(provided.getEqualsRepresentation()); */ } @Override public int hashCode() { return getEqualsRepresentation().hashCode(); } @Override public boolean covers(ProvidedInterface aInterface) { // TODO do more than just equals. if (!(aInterface instanceof DefaultProvidedInterface)) { return false; } return getEqualsRepresentation().equals( ((DefaultProvidedInterface) aInterface).getEqualsRepresentation()); } private String getEqualsRepresentation() { List result = new ArrayList(); for (Class cls : interfaces) { result.add(cls.getName()); } Collections.sort(result); String value = ""; for (String str : result) { value += (":" + str); } return value; } }