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     */
017    package org.apache.camel.impl;
018    
019    import java.lang.reflect.Field;
020    
021    import org.apache.camel.spi.UriParam;
022    
023    import static org.apache.camel.util.StringQuoteHelper.doubleQuote;
024    
025    /**
026     * Represents the configuration of a URI query parameter value to allow type conversion
027     * and better validation of the configuration of URIs and Endpoints
028     */
029    public class ParameterConfiguration {
030        private final String name;
031        private final Class<?> parameterType;
032    
033        public ParameterConfiguration(String name, Class<?> parameterType) {
034            this.name = name;
035            this.parameterType = parameterType;
036        }
037    
038        @Override
039        public String toString() {
040            return "ParameterConfiguration[" + name + " on " + parameterType + "]";
041        }
042    
043        /**
044         * Returns the name of the parameter value
045         */
046        public String getName() {
047            return name;
048        }
049    
050        /**
051         * Returns the type of the parameter value
052         */
053        public Class<?> getParameterType() {
054            return parameterType;
055        }
056    
057        /**
058         * Factory method to create a new ParameterConfiguration from a field
059         */
060        public static ParameterConfiguration newInstance(String name, Field field, UriParam uriParam) {
061            return new AnnotatedParameterConfiguration(name, field.getType(), field);
062        }
063    
064        /**
065         * Returns the JSON format of this parameter configuration
066         */
067        public String toJson() {
068            String typeName = parameterType.getCanonicalName();
069            // TODO would be nice to add a description; wonder if we can find that from somewhere
070            // generated by the APT tool?
071            return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName) + " }";
072        }
073    }