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 static org.apache.juneau.commons.utils.AssertionUtils.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.commons.collections.*;
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 org.apache.juneau.reflect.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    * Builder class.
042    */
043   public static class Builder extends BaseHttpPartSerializer.Builder {
044
045      private static final Cache<HashKey,SimplePartSerializer> CACHE = Cache.of(HashKey.class, SimplePartSerializer.class).build();
046
047      /**
048       * Constructor.
049       */
050      protected Builder() {}
051
052      /**
053       * Copy constructor.
054       *
055       * @param copyFrom The builder to copy.
056       *    <br>Cannot be <jk>null</jk>.
057       */
058      protected Builder(Builder copyFrom) {
059         super(assertArgNotNull("copyFrom", copyFrom));
060      }
061
062      @Override
063      public SimplePartSerializer build() {
064         return cache(CACHE).build(SimplePartSerializer.class);
065      }
066
067      @Override /* Overridden from Context */
068      public Builder cache(Cache<HashKey,? extends Context> value) {
069         super.cache(value);
070         return this;
071      }
072
073      @Override
074      public Builder copy() {
075         return new Builder(this);
076      }
077   }
078
079   /** Reusable instance of {@link SimplePartSerializer}, all default settings. */
080   public static final SimplePartSerializer DEFAULT = create().build();
081
082   /**
083    * Creates a new builder for this object.
084    *
085    * @return A new builder.
086    */
087   public static Builder create() {
088      return new Builder();
089   }
090
091   /**
092    * Constructor
093    *
094    * @param builder The builder for this object.
095    *    <br>Cannot be <jk>null</jk>.
096    */
097   public SimplePartSerializer(Builder builder) {
098      super(assertArgNotNull("builder", builder));
099   }
100
101   @Override
102   public SimplePartSerializerSession getPartSession() { return new SimplePartSerializerSession(); }
103}