001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.impl;
013
014import java.io.Serializable;
015import java.lang.reflect.Proxy;
016import java.util.ArrayList;
017import java.util.HashSet;
018import java.util.List;
019import java.util.Set;
020
021import org.nuxeo.ecm.automation.TypeAdapter;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026public class AdapterKeyedRegistry extends SuperKeyedRegistry<TypeAdapterKey, TypeAdapter> {
027
028    protected final Set<Class<?>> blacklist;
029
030    public AdapterKeyedRegistry() {
031        blacklist = new HashSet<Class<?>>();
032        blacklist.add(Serializable.class);
033        blacklist.add(Cloneable.class);
034        blacklist.add(Comparable.class);
035    }
036
037    @Override
038    protected boolean isRoot(TypeAdapterKey key) {
039        return key.input == Object.class;
040    }
041
042    @Override
043    protected List<TypeAdapterKey> getSuperKeys(TypeAdapterKey key) {
044        List<TypeAdapterKey> result = new ArrayList<TypeAdapterKey>();
045        Class<?> cl = key.input.getSuperclass();
046        if (cl != null) {
047            result.add(new TypeAdapterKey(cl, key.output));
048        }
049        for (Class<?> itf : key.input.getInterfaces()) {
050            if (!blacklist.contains(itf)) {
051                result.add(new TypeAdapterKey(itf, key.output));
052            }
053        }
054        return result;
055    }
056
057    @Override
058    protected boolean isCachingEnabled(TypeAdapterKey key) {
059        return !Proxy.isProxyClass(key.input);
060    }
061
062}