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.marshall.httppart;
018
019
020import org.apache.juneau.commons.collections.*;
021import static org.apache.juneau.commons.utils.Shorts.*;
022
023/**
024 * An implementation of {@link HttpPartParser} that takes in the strings and tries to convert them to POJOs using constructors and static create methods.
025 *
026 * <p>
027 * The class being created must be one of the following in order to convert it from a string:
028 *
029 * <ul>
030 *    <li>
031 *       An <jk>enum</jk>.
032 *    <li>
033 *       Have a public constructor with a single <c>String</c> parameter.
034 *    <li>
035 *       Have one of the following public static methods that takes in a single <c>String</c> parameter:
036 *       <ul>
037 *          <li><c>fromString</c>
038 *          <li><c>fromValue</c>
039 *          <li><c>valueOf</c>
040 *          <li><c>parse</c>
041 *          <li><c>parseString</c>
042 *          <li><c>forName</c>
043 *          <li><c>forString</c>
044 *    </ul>
045 * </ul>
046 *
047 * <h5 class='section'>Notes:</h5><ul>
048 *    <li class='note'>This class is thread safe and reusable.
049 * </ul>
050 *
051 * <h5 class='section'>See Also:</h5><ul>
052 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
053
054 * </ul>
055 */
056@SuppressWarnings({
057   "java:S1192" // Duplicated literals (argument/property names) read more clearly inline than as constants
058})
059public class SimplePartParser extends BaseHttpPartParser {
060
061   /**
062    * Builder class.
063    */
064   public static class Builder extends BaseHttpPartParser.Builder<Builder> {
065
066      private static final Cache<HashKey,SimplePartParser> CACHE = Cache.of(HashKey.class, SimplePartParser.class).build();
067
068      /**
069       * Constructor.
070       */
071      protected Builder() {}
072
073      /**
074       * Copy constructor.
075       *
076       * @param copyFrom The builder to copy.
077       *    <br>Cannot be <jk>null</jk>.
078       */
079      protected Builder(Builder copyFrom) {
080         super(reqnn("copyFrom", copyFrom));
081      }
082
083      @Override
084      public SimplePartParser build() {
085         return cache(CACHE).build(SimplePartParser.class);
086      }
087
088      @Override
089      public Builder copy() {
090         return new Builder(this);
091      }
092   }
093
094   /** Reusable instance of {@link SimplePartParser}, all default settings. */
095   public static final SimplePartParser DEFAULT = create().build();
096
097   /** Reusable instance of {@link SimplePartParser}, all default settings. */
098   public static final SimplePartParserSession DEFAULT_SESSION = DEFAULT.getPartSession();
099
100   /**
101    * Creates a new builder for this object.
102    *
103    * @return A new builder.
104    */
105   public static Builder create() {
106      return new Builder();
107   }
108
109   /**
110    * Constructor
111    *
112    * @param builder The builder for this object.
113    *    <br>Cannot be <jk>null</jk>.
114    */
115   public SimplePartParser(Builder builder) {
116      super(reqnn("builder", builder));
117   }
118
119   @Override
120   public SimplePartParserSession getPartSession() { return new SimplePartParserSession(); }
121}