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 org.apache.juneau.commons.conversion.*; 022import static org.apache.juneau.commons.utils.Shorts.*; 023 024/** 025 * An implementation of {@link HttpPartSerializer} that simply serializes everything using {@link Object#toString()}. 026 * 027 * <p> 028 * More precisely, uses {@link BasicConverter#INSTANCE} 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 */ 039@SuppressWarnings({ 040 "java:S1192" // Duplicated literals (argument/property names) read more clearly inline than as constants 041}) 042public class SimplePartSerializer extends BaseHttpPartSerializer { 043 044 /** 045 * Builder class. 046 */ 047 public static class Builder extends BaseHttpPartSerializer.Builder<Builder> { 048 049 private static final Cache<HashKey,SimplePartSerializer> CACHE = Cache.of(HashKey.class, SimplePartSerializer.class).build(); 050 051 /** 052 * Constructor. 053 */ 054 protected Builder() {} 055 056 /** 057 * Copy constructor. 058 * 059 * @param copyFrom The builder to copy. 060 * <br>Cannot be <jk>null</jk>. 061 */ 062 protected Builder(Builder copyFrom) { 063 super(reqnn("copyFrom", copyFrom)); 064 } 065 066 @Override 067 public SimplePartSerializer build() { 068 return cache(CACHE).build(SimplePartSerializer.class); 069 } 070 071 @Override 072 public Builder copy() { 073 return new Builder(this); 074 } 075 } 076 077 /** Reusable instance of {@link SimplePartSerializer}, all default settings. */ 078 public static final SimplePartSerializer DEFAULT = create().build(); 079 080 /** 081 * Creates a new builder for this object. 082 * 083 * @return A new builder. 084 */ 085 public static Builder create() { 086 return new Builder(); 087 } 088 089 /** 090 * Constructor 091 * 092 * @param builder The builder for this object. 093 * <br>Cannot be <jk>null</jk>. 094 */ 095 public SimplePartSerializer(Builder builder) { 096 super(reqnn("builder", builder)); 097 } 098 099 @Override 100 public SimplePartSerializerSession getPartSession() { return new SimplePartSerializerSession(); } 101}