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 } 069 070 /** 071 * Copy constructor. 072 * 073 * @param copyFrom The builder to copy. 074 */ 075 protected Builder(Builder copyFrom) { 076 super(copyFrom); 077 } 078 079 @Override 080 public SimplePartSerializer build() { 081 return cache(CACHE).build(SimplePartSerializer.class); 082 } 083 084 @Override 085 public Builder copy() { 086 return new Builder(this); 087 } 088 089 @Override /* GENERATED - org.apache.juneau.Context.Builder */ 090 public Builder cache(Cache<HashKey,? extends Context> value) { 091 super.cache(value); 092 return this; 093 } 094 } 095 096 //------------------------------------------------------------------------------------------------------------------- 097 // Instance 098 //------------------------------------------------------------------------------------------------------------------- 099 100 /** 101 * Constructor 102 * 103 * @param builder The builder for this object. 104 */ 105 public SimplePartSerializer(Builder builder) { 106 super(builder); 107 } 108 109 @Override 110 public SimplePartSerializerSession getPartSession() { 111 return new SimplePartSerializerSession(); 112 } 113}