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;
018
019import org.apache.juneau.*;
020import org.apache.juneau.internal.*;
021import org.apache.juneau.reflect.*;
022import org.apache.juneau.utils.*;
023
024/**
025 * An implementation of {@link HttpPartSerializer} that simply serializes everything using {@link Object#toString()}.
026 *
027 * <p>
028 * More precisely, uses the {@link Mutaters#toString(Object)} method to stringify objects.
029 *
030 * <h5 class='section'>Notes:</h5><ul>
031 *    <li class='note'>This class is thread safe and reusable.
032 * </ul>
033 *
034 * <h5 class='section'>See Also:</h5><ul>
035 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
036
037 * </ul>
038 */
039public class SimplePartSerializer extends BaseHttpPartSerializer {
040
041   //-----------------------------------------------------------------------------------------------------------------
042   // Static
043   //-----------------------------------------------------------------------------------------------------------------
044
045   /** Reusable instance of {@link SimplePartSerializer}, all default settings. */
046   public static final SimplePartSerializer DEFAULT = create().build();
047
048   /**
049    * Creates a new builder for this object.
050    *
051    * @return A new builder.
052    */
053   public static Builder create() {
054      return new Builder();
055   }
056
057   //-------------------------------------------------------------------------------------------------------------------
058   // Builder
059   //-------------------------------------------------------------------------------------------------------------------
060
061   /**
062    * Builder class.
063    */
064   public static class Builder extends BaseHttpPartSerializer.Builder {
065
066      private static final Cache<HashKey,SimplePartSerializer> CACHE = Cache.of(HashKey.class, SimplePartSerializer.class).build();
067
068      /**
069       * Constructor.
070       */
071      protected Builder() {
072      }
073
074      /**
075       * Copy constructor.
076       *
077       * @param copyFrom The builder to copy.
078       */
079      protected Builder(Builder copyFrom) {
080         super(copyFrom);
081      }
082
083      @Override
084      public SimplePartSerializer build() {
085         return cache(CACHE).build(SimplePartSerializer.class);
086      }
087
088      @Override
089      public Builder copy() {
090         return new Builder(this);
091      }
092
093   @Override /* Overridden from Context */
094   public Builder cache(Cache<HashKey,? extends Context> value) {
095         super.cache(value);
096         return this;
097      }
098   }
099
100   //-------------------------------------------------------------------------------------------------------------------
101   // Instance
102   //-------------------------------------------------------------------------------------------------------------------
103
104   /**
105    * Constructor
106    *
107    * @param builder The builder for this object.
108    */
109   public SimplePartSerializer(Builder builder) {
110      super(builder);
111   }
112
113   @Override
114   public SimplePartSerializerSession getPartSession() {
115      return new SimplePartSerializerSession();
116   }
117}