001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.httppart;
014
015import org.apache.juneau.*;
016import org.apache.juneau.internal.*;
017import org.apache.juneau.reflect.*;
018import org.apache.juneau.utils.*;
019
020/**
021 * An implementation of {@link HttpPartSerializer} that simply serializes everything using {@link Object#toString()}.
022 *
023 * <p>
024 * More precisely, uses the {@link Mutaters#toString(Object)} method to stringify objects.
025 *
026 * <h5 class='section'>Notes:</h5><ul>
027 *    <li class='note'>This class is thread safe and reusable.
028 * </ul>
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
032
033 * </ul>
034 */
035public class SimplePartSerializer extends BaseHttpPartSerializer {
036
037   //-----------------------------------------------------------------------------------------------------------------
038   // Static
039   //-----------------------------------------------------------------------------------------------------------------
040
041   /** Reusable instance of {@link SimplePartSerializer}, all default settings. */
042   public static final SimplePartSerializer DEFAULT = create().build();
043
044   /**
045    * Creates a new builder for this object.
046    *
047    * @return A new builder.
048    */
049   public static Builder create() {
050      return new Builder();
051   }
052
053   //-------------------------------------------------------------------------------------------------------------------
054   // Builder
055   //-------------------------------------------------------------------------------------------------------------------
056
057   /**
058    * Builder class.
059    */
060   public static class Builder extends BaseHttpPartSerializer.Builder {
061
062      private static final Cache<HashKey,SimplePartSerializer> CACHE = Cache.of(HashKey.class, SimplePartSerializer.class).build();
063
064      /**
065       * Constructor.
066       */
067      protected Builder() {
068         super();
069      }
070
071      /**
072       * Copy constructor.
073       *
074       * @param copyFrom The builder to copy.
075       */
076      protected Builder(Builder copyFrom) {
077         super(copyFrom);
078      }
079
080      @Override
081      public SimplePartSerializer build() {
082         return cache(CACHE).build(SimplePartSerializer.class);
083      }
084
085      @Override
086      public Builder copy() {
087         return new Builder(this);
088      }
089
090      @Override /* GENERATED - org.apache.juneau.Context.Builder */
091      public Builder cache(Cache<HashKey,? extends Context> value) {
092         super.cache(value);
093         return this;
094      }
095   }
096
097   //-------------------------------------------------------------------------------------------------------------------
098   // Instance
099   //-------------------------------------------------------------------------------------------------------------------
100
101   /**
102    * Constructor
103    *
104    * @param builder The builder for this object.
105    */
106   public SimplePartSerializer(Builder builder) {
107      super(builder);
108   }
109
110   @Override
111   public SimplePartSerializerSession getPartSession() {
112      return new SimplePartSerializerSession();
113   }
114}