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
014/**
015 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
016 */
017public final class TypeAdapterKey {
018
019    public final Class<?> input;
020
021    public final Class<?> output;
022
023    private int hashCode;
024
025    public TypeAdapterKey(Class<?> input, Class<?> output) {
026        this.input = input;
027        this.output = output;
028    }
029
030    @Override
031    public boolean equals(Object obj) {
032        if (this == obj) {
033            return true;
034        }
035        // this class is final - do not need instanceof check.
036        if (obj == null) {
037            return false;
038        }
039        if (obj.getClass() == TypeAdapterKey.class) {
040            TypeAdapterKey key = (TypeAdapterKey) obj;
041            return key.input == input && key.output == output;
042        }
043        return false;
044    }
045
046    @Override
047    public String toString() {
048        return input + ":" + output;
049    }
050
051    @Override
052    public int hashCode() {
053        if (hashCode == 0) {
054            hashCode = createHashCode();
055        }
056        return hashCode;
057    }
058
059    protected int createHashCode() {
060        int result = input.hashCode() | output.hashCode();
061        return result == 0 ? 0xbabe : result;
062    }
063
064}