001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.spi;
018
019import org.apache.camel.Endpoint;
020import org.apache.camel.Processor;
021
022/**
023 * A Contract which represents the input type and/or output type of the {@link Endpoint} or {@link Processor}.
024 */
025public class Contract {
026
027    private DataType inputType;
028    private DataType outputType;
029    private boolean validateInput;
030    private boolean validateOutput;
031    private String contractString;
032    
033    public DataType getInputType() {
034        return inputType;
035    }
036    
037    /**
038     * Set the input data type.
039     *
040     * @param inputType input data type
041     */
042    public void setInputType(String inputType) {
043        this.inputType = new DataType(inputType);
044        this.contractString = null;
045    }
046    
047    /**
048     * Set the input data type with Java class.
049     *
050     * @param clazz Java class which represents input data type
051     */
052    public void setInputType(Class<?> clazz) {
053        this.inputType = new DataType(clazz);
054        this.contractString = null;
055    }
056    
057    public DataType getOutputType() {
058        return outputType;
059    }
060    
061    /**
062     * Set the output data type.
063     *
064     * @param outputType output data type
065     */
066    public void setOutputType(String outputType) {
067        this.outputType = new DataType(outputType);
068        this.contractString = null;
069    }
070    
071    /**
072     * Set the output data type with Java class.
073     *
074     * @param clazz Java class which represents output data type
075     */
076    public void setOutputType(Class<?> clazz) {
077        this.outputType = new DataType(clazz);
078        this.contractString = null;
079    }
080    
081    public boolean isValidateInput() {
082        return validateInput;
083    }
084
085    /**
086     * Whether to validate the input
087     */
088    public void setValidateInput(boolean validate) {
089        this.validateInput = validate;
090    }
091    
092    public boolean isValidateOutput() {
093        return validateOutput;
094    }
095
096    /**
097     * Whether to validate the output
098     */
099    public void setValidateOutput(boolean validate) {
100        this.validateOutput = validate;
101    }
102    
103    @Override
104    public String toString() {
105        if (contractString == null) {
106            this.contractString = "DataType[input=" + this.inputType + ", output=" + this.outputType + "]";
107        }
108        return contractString;
109    }
110
111    public boolean isEmpty() {
112        return inputType == null && outputType == null;
113    }
114
115    @Override
116    public boolean equals(Object target) {
117        if (!(target instanceof Contract)) {
118            return false;
119        }
120        Contract targetContract = (Contract)target;
121        if (getInputType() != null || targetContract.getInputType() != null) {
122            if (getInputType() == null || targetContract.getInputType() == null
123                || !getInputType().equals(targetContract.getInputType())) {
124                return false;
125            }
126        }
127        if (getOutputType() != null || targetContract.getOutputType() != null) {
128            if (getOutputType() == null || targetContract.getOutputType() == null
129                || !getOutputType().equals(targetContract.getOutputType())) {
130                return false;
131            }
132        }
133        return true;
134    }
135
136    @Override
137    public int hashCode() {
138        return toString().hashCode();
139    }
140}