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.utils;
014
015import java.io.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.json.*;
019import org.apache.juneau.serializer.*;
020
021/**
022 * A serializer/object pair used for delayed object serialization.
023 *
024 * <p>
025 * Useful in certain conditions such as logging when you don't want to needlessly serialize objects.
026 *
027 * <p>
028 * Instances of this method are created by the {@link WriterSerializer#toStringObject(Object)} method.
029 *
030 * <h5 class='section'>Example:</h5>
031 * <p class='bcode w800'>
032 *    <jc>// The POJO will not be serialized unless DEBUG is enabled.</jc>
033 *    logger.log(<jsf>DEBUG</jsf>, <js>"Object contents are: {0}"</js>, JsonSerializer.<jsf>DEFAULT</jsf>.toObjectString(myPojo));
034 * </p>
035 */
036public class StringObject implements CharSequence, Writable {
037
038   private final WriterSerializer s;
039   private final Object o;
040   private String results;
041
042   /**
043    * Constructor.
044    *
045    * @param s The serializer to use to serialize the object.
046    * @param o The object to be serialized.
047    */
048   public StringObject(WriterSerializer s, Object o) {
049      this.s = s;
050      this.o = o;
051   }
052
053   /**
054    * Constructor with default serializer {@link SimpleJsonSerializer#DEFAULT}
055    *
056    * @param o The object to be serialized.
057    */
058   public StringObject(Object o) {
059      this(SimpleJsonSerializer.DEFAULT, o);
060   }
061
062   @Override /* Object */
063   public String toString() {
064      if (results == null)
065         results = s.toString(o);
066      return results;
067   }
068
069   @Override /* CharSequence */
070   public int length() {
071      return toString().length();
072   }
073
074   @Override /* CharSequence */
075   public char charAt(int index) {
076      return toString().charAt(index);
077   }
078
079   @Override /* CharSequence */
080   public CharSequence subSequence(int start, int end) {
081      return toString().subSequence(start, end);
082   }
083
084   @Override /* Writable */
085   public Writer writeTo(Writer w) throws IOException {
086      try {
087         s.serialize(o, w);
088         return w;
089      } catch (SerializeException e) {
090         throw new IOException(e);
091      }
092   }
093
094   @Override /* Writable */
095   public String getMediaType() {
096      return s.getPrimaryMediaType().toString();
097   }
098}