/* * 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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.UUID; /** * Default implementation of a service descriptor. * * @author Erik Brakkee */ public class DefaultProvidedInterface implements ProvidedInterface { private String _name; private Class[] _interfaces; private String _uniqueId; /** * Constructs the descriptor. * @param aInterface Type of service. */ public DefaultProvidedInterface(String aName, Class aInterface) { this(aName, new Class[] { aInterface }); } public DefaultProvidedInterface(String aName, Class[] aInterfaces) { _name = aName; _interfaces = Arrays.copyOf(aInterfaces, aInterfaces.length); _uniqueId = UUID.randomUUID().toString(); } @Override public String getName() { return _name; } @Override public Class[] getInterfaceTypes() { return _interfaces; } @Override public String getUniqueId() { return _uniqueId; } @Override public void publish(Object aImplementation, Scope aScope) { aScope.publishInterface(this, aImplementation); } @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) { if ( !(aObj instanceof DefaultProvidedInterface)) { return false; } DefaultProvidedInterface provided = (DefaultProvidedInterface)aObj; return getEqualsRepresentation().equals(provided.getEqualsRepresentation()); } @Override public int hashCode() { return getEqualsRepresentation().hashCode(); } 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; } }