View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.io;
33  
34  import java.io.IOException;
35  import java.io.UncheckedIOException;
36  import java.util.ConcurrentModificationException;
37  import java.util.function.BiConsumer;
38  import javax.annotation.Nullable;
39  import javax.annotation.ParametersAreNonnullByDefault;
40  import org.spf4j.base.CoreTextMediaType;
41  import org.spf4j.base.EscapeJsonStringAppendableWrapper;
42  
43  /**
44   * @author zoly
45   * @param <T> - type of object to append.
46   */
47  @ParametersAreNonnullByDefault
48  @FunctionalInterface
49  public interface ObjectAppender<T> extends BiConsumer<T, Appendable> {
50  
51    /**
52     * the MimeType of the format used to write the Object.
53     * @return
54     */
55    default CoreTextMediaType getAppendedType() {
56      return CoreTextMediaType.TEXT_PLAIN;
57    }
58  
59    /**
60     * Write an Object to a char stream.
61     * @param object
62     * @param appendTo
63     * @throws IOException
64     */
65    void append(T object, Appendable appendTo) throws IOException;
66  
67  
68    static void appendNullable(@Nullable final Object o, final Appendable appendTo,
69            final ObjectAppenderSupplier appenderSupplier)
70            throws IOException {
71      if (o == null) {
72        appendTo.append("null");
73      } else {
74        appenderSupplier.get(o.getClass()).append(o, appendTo, appenderSupplier);
75      }
76    }
77  
78    static void appendNullableJson(@Nullable final Object o, final Appendable appendTo,
79            final ObjectAppenderSupplier appenderSupplier)
80            throws IOException {
81      if (o == null) {
82        appendTo.append("null");
83      } else {
84        ObjectAppender app = appenderSupplier.get(CoreTextMediaType.APPLICATION_JSON, o.getClass());
85        if (app != null) {
86           app.append(o, appendTo, appenderSupplier);
87        } else {
88          app = appenderSupplier.get(CoreTextMediaType.TEXT_PLAIN, o.getClass());
89          EscapeJsonStringAppendableWrapper sEsc = new EscapeJsonStringAppendableWrapper(appendTo);
90          appendTo.append('"');
91          app.append(o, sEsc, appenderSupplier);
92          appendTo.append('"');
93        }
94      }
95    }
96  
97  
98    /**
99     * Method to overwrite for implementing apenders for container objects.
100    */
101   default void append(T object, Appendable appendTo, ObjectAppenderSupplier appenderSupplier) throws IOException {
102     append(object, appendTo);
103   }
104 
105   default void accept(final T object, final Appendable appendTo) {
106     try {
107       append(object, appendTo);
108     } catch (IOException ex) {
109       throw new UncheckedIOException(ex);
110     }
111   }
112 
113   default String toString(final T object) {
114     StringBuilder sb = new StringBuilder();
115     this.accept(object, sb);
116     return sb.toString();
117   }
118 
119   /**
120    * A simple Object appender that invokes the toString method of the object and writes the object out.
121    */
122   ObjectAppender<Object> TOSTRING_APPENDER = new ObjectAppender<Object>() {
123     @Override
124     public void append(final Object object, final Appendable appendTo) throws IOException {
125 
126       String toString = null;
127       int i = 10;
128       do {
129         try {
130           toString = object.toString();
131         } catch (ConcurrentModificationException ex) {
132           i--;
133         }
134       } while (toString == null && i > 0);
135       if (i != 10) {
136         appendTo.append("ConcurrentlyModifiedDuringToString:");
137       }
138       if (toString == null) {
139         appendTo.append(object.getClass().getName()).append('@')
140                 .append(Integer.toHexString(System.identityHashCode(object)));
141       } else {
142         appendTo.append(toString);
143       }
144     }
145 
146   };
147 
148 }