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.juneau.httppart.bean;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.lang.reflect.*;
022import java.util.*;
023
024import org.apache.juneau.commons.httppart.*;
025import org.apache.juneau.commons.inject.*;
026import org.apache.juneau.commons.reflect.*;
027import org.apache.juneau.http.*;
028import org.apache.juneau.marshall.httppart.*;
029
030/**
031 * Represents the metadata gathered from a getter method of a class annotated with {@link Response}.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
035 * </ul>
036 */
037public class ResponseBeanPropertyMeta {
038
039   static class Builder {
040      HttpPartType partType;
041      HttpPartSchema schema = HttpPartSchema.DEFAULT;
042      String name;
043      Method getter;
044
045      ResponseBeanPropertyMeta build(Optional<HttpPartSerializer> serializer, Optional<HttpPartParser> parser) {
046         return new ResponseBeanPropertyMeta(this, serializer, parser);
047      }
048
049      Builder getter(Method value) {
050         getter = value;
051         return this;
052      }
053
054      Builder name(String value) {
055         name = value;
056         return this;
057      }
058
059      Builder partType(HttpPartType value) {
060         partType = value;
061         return this;
062      }
063
064      Builder schema(HttpPartSchema value) {
065         schema = value;
066         return this;
067      }
068   }
069
070   static ResponseBeanPropertyMeta.Builder create(HttpPartType partType, HttpPartSchema schema, MethodInfo m) {
071      return new Builder().partType(partType).schema(schema).getter(m.inner());
072   }
073
074   static ResponseBeanPropertyMeta.Builder create(HttpPartType partType, MethodInfo m) {
075      return new Builder().partType(partType).getter(m.inner());
076   }
077
078   private final Method getter;
079   private final HttpPartType partType;
080   private final Optional<HttpPartSerializer> serializer;
081   private final Optional<HttpPartParser> parser;
082
083   private final HttpPartSchema schema;
084
085   @SuppressWarnings({
086      "unchecked" // Type erasure on reflective/generic cast; element type is verified at call site
087   })
088   ResponseBeanPropertyMeta(Builder b, Optional<HttpPartSerializer> serializer, Optional<HttpPartParser> parser) {
089      partType = b.partType;
090      schema = b.schema;
091      getter = b.getter;
092      this.serializer = serializer.or(() -> BeanInstantiator.optionalOf((Class<HttpPartSerializer>) schema.getSerializer()));
093      this.parser = parser.or(() -> BeanInstantiator.optionalOf((Class<HttpPartParser>) schema.getParser()));
094   }
095
096   /**
097    * Returns the name of the Java method getter that defines this property.
098    *
099    * @return
100    *    The name of the Java method getter that defines this property.
101    *    <br>Never <jk>null</jk>.
102    */
103   public Method getGetter() { return getter; }
104
105   /**
106    * Returns the parser to use for parsing the bean property value.
107    *
108    * @return The parser to use for parsing the bean property value.
109    */
110   public Optional<HttpPartParser> getParser() { return parser; }
111
112   /**
113    * Returns the HTTP part name for this property (the query parameter name for example).
114    *
115    * @return The HTTP part name, or <jk>null</jk> if it doesn't have a part name.
116    */
117   public Optional<String> getPartName() { return o(schema.getName()); }
118
119   /**
120    * Returns the HTTP part type for this property (query parameter for example).
121    *
122    * @return
123    *    The HTTP part type for this property.
124    *    <br>Never <jk>null</jk>.
125    */
126   public HttpPartType getPartType() { return partType; }
127
128   /**
129    * Returns the schema information gathered from annotations on the method and return type.
130    *
131    * @return
132    *    The schema information gathered from annotations on the method and return type.
133    *    <br>Never <jk>null</jk>.
134    */
135   public HttpPartSchema getSchema() { return schema; }
136
137   /**
138    * Returns the serializer to use for serializing the bean property value.
139    *
140    * @return The serializer to use for serializing the bean property value.
141    */
142   public Optional<HttpPartSerializer> getSerializer() { return serializer; }
143}