Skip to content

Commit 508f8e7

Browse files
Appandable for toString
1 parent 5385b4e commit 508f8e7

File tree

22 files changed

+240
-149
lines changed

22 files changed

+240
-149
lines changed

SerialX-core/src/main/java/org/ugp/serialx/converters/BooleanConverter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static java.lang.Boolean.*;
44
import static org.ugp.serialx.Utils.equalsLowerCase;
55

6+
import java.io.IOException;
7+
68
/**
79
* This converter is capable of converting {@link String}.
810
* Its case insensitive!
@@ -76,10 +78,14 @@ public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
7678
}
7779

7880
@Override
79-
public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args)
81+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException
8082
{
8183
if (obj instanceof Boolean)
82-
return isShorten() ? (boolean) obj ? "T" : "F" : (boolean) obj ? "true" : "false";
84+
{
85+
if (isShorten())
86+
return source.append((boolean) obj ? 'T' : 'F');
87+
return source.append((boolean) obj ? "true" : "false");
88+
}
8389
return CONTINUE;
8490
}
8591

SerialX-core/src/main/java/org/ugp/serialx/converters/CharacterConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.ugp.serialx.converters;
22

3+
import java.io.IOException;
4+
35
/**
46
* This converter is capable of converting {@link Character}.
57
* Its case sensitive!
@@ -52,10 +54,10 @@ public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
5254
}
5355

5456
@Override
55-
public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args)
57+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException
5658
{
5759
if (obj instanceof Character)
58-
return "'"+(int) (char) obj+"'";
60+
return source.append("'"+(int) (char) obj+"'");
5961
return CONTINUE;
6062
}
6163

SerialX-core/src/main/java/org/ugp/serialx/converters/DataConverter.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.ugp.serialx.converters;
22

3+
import java.io.IOException;
4+
35
/**
46
* This is DataParser with extended functionality! {@link DataConverter} can also parse data like DataParser but is also capable of converting them back to string!
57
* This to string convertation is performed by {@link DataConverter#toString(Object)} and result of this convertation supposed to be parsable by {@link DataConverter#parse(String, Object...)} meaning one converter supposed to be parsing and converting via the same string format!
@@ -22,7 +24,33 @@ public interface DataConverter extends DataParser
2224
*
2325
* @since 1.3.0
2426
*/
25-
CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args);
27+
@Deprecated
28+
default CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args)
29+
{
30+
try
31+
{
32+
return (CharSequence) toString(new StringBuilder().append(0), myHomeRegistry, obj, args);
33+
}
34+
catch (IOException e)
35+
{
36+
throw new RuntimeException(e); // Rare...
37+
}
38+
}
39+
40+
/**
41+
* @param source | Source to append the properly stringified object (obj) into. Should be treated as only and only {@link Appendable}, no casting!
42+
* @param myHomeRegistry | Registry where this parser is registered provided by {@link ParserRegistry#parse(String, boolean, Class, Object...)} otherwise it demands on implementation (it should not be null)!
43+
* @param obj | Object to convert into string!
44+
* @param args | Some additional args. This can be anything and it demands on implementation of DataConverter. Default SerialX API implementation will provide some flags about formating (2 ints)!
45+
*
46+
* @return The source appendable after stringified object (obj) was appropriately appended into it.
47+
* Return {@link DataParser#CONTINUE} to tell that this converter is not suitable for converting this object! You most likely want to do this when obtained obj is not suitable instance!
48+
*
49+
* @throws IOException When appending into source throws it...
50+
*
51+
* @since 1.3.9
52+
*/
53+
Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException;
2654

2755
/**
2856
* @param myHomeRegistry | Registry of parsers (might be null)!

SerialX-core/src/main/java/org/ugp/serialx/converters/DataParser.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ugp.serialx.converters;
22

3+
import java.io.IOException;
34
import java.util.Collection;
45

56
import org.ugp.serialx.LogProvider;
@@ -29,10 +30,11 @@ public interface DataParser
2930

3031
/**
3132
* This is connected with {@link DataParser#parse(String, Object...)} and {@link DataParser#parseObj(String, Object...)}! And its a way to tell that this parser is not suitable for parsing obtained string and search for optimal one should continue.
33+
* Should not be modified under any circumstances, always treat as immutable!
3234
*
3335
* @since 1.3.0
3436
*/
35-
public static final String CONTINUE = new String();
37+
public static final Appendable CONTINUE = new StringBuilder();
3638

3739
/**
3840
* This is DataParser registry. Here your parser implementations should be registered in order to work properly!
@@ -165,18 +167,43 @@ public DataConverter getConverterFor(Object obj, Object... args)
165167
*
166168
* @since 1.3.5
167169
*/
170+
@Deprecated
168171
public CharSequence toString(Object obj, Object... args)
169172
{
170-
CharSequence str;
173+
try
174+
{
175+
return (CharSequence) toString(new StringBuilder(), obj, args);
176+
}
177+
catch (IOException e)
178+
{
179+
throw new RuntimeException(e);
180+
}
181+
}
182+
183+
/**
184+
* @param source | Source to append the properly stringified object (obj) into. Should be treated as only and only {@link Appendable}, no casting!
185+
* @param obj | Object to convert into string!
186+
* @param args | Additional arguments that will be obtained in {@link DataParser#toString(String, Object...)}!
187+
*
188+
* @return The source appendable after stringified object (obj) was appropriately appended into it.
189+
* Return {@link DataParser#CONTINUE} to tell that this converter is not suitable for converting this object! You most likely want to do this when obtained obj is not suitable instance!
190+
*
191+
* @throws IOException When appending into source throws it...
192+
*
193+
* @since 1.3.9
194+
*/
195+
public Appendable toString(Appendable source, Object obj, Object... args) throws IOException
196+
{
197+
Appendable str;
171198
if (convertingCache != null)
172199
for (DataParser parser : convertingCache)
173-
if (parser != null && (str = ((DataConverter) parser).toString(this, obj, args)) != CONTINUE)
200+
if (parser != null && (str = ((DataConverter) parser).toString(source, this, obj, args)) != CONTINUE)
174201
return str;
175202

176203
for (int i = 0, size = size(); i < size; i++)
177204
{
178205
DataParser parser = get(i);
179-
if (parser instanceof DataConverter && (str = ((DataConverter) parser).toString(this, obj, args)) != CONTINUE)
206+
if (parser instanceof DataConverter && (str = ((DataConverter) parser).toString(source, this, obj, args)) != CONTINUE)
180207
{
181208
if (convertingCache != null && i < convertingCache.length)
182209
convertingCache[i] = parser;

SerialX-core/src/main/java/org/ugp/serialx/converters/NullConverter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.ugp.serialx.Utils.equalsLowerCase;
44

5+
import java.io.IOException;
6+
57
/**
68
* This converter is capable of converting "nothing" otherwise known as null and {@link DataParser#VOID}.
79
* Its case insensitive!
@@ -49,10 +51,10 @@ public Object parse(ParserRegistry registry, String str, Object... args)
4951
}
5052

5153
@Override
52-
public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args)
54+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException
5355
{
5456
if (obj == null)
55-
return "null";
57+
return source.append("null");
5658
return CONTINUE;
5759
}
5860

SerialX-core/src/main/java/org/ugp/serialx/converters/NumberConverter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.ugp.serialx.Utils.contains;
44

5+
import java.io.IOException;
6+
57
/**
68
* This converter is capable of converting {@link Number} including all common implementations like {@link Double}, {@link Float}, {@link Integer} and others. They are determine by suffixes like in java!
79
* Its case insensitive!
@@ -106,22 +108,23 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
106108
}
107109

108110
@Override
109-
public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args)
111+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException
110112
{
111113
if (obj instanceof Number)
112114
{
113-
StringBuilder str = new StringBuilder(format((Number) obj));
115+
String str;
116+
source.append(str = format((Number) obj));
114117
if (obj instanceof Double && !contains(str, '.'))
115-
str.append('D');
118+
source.append('D');
116119
else if (obj instanceof Float)
117-
str.append('F');
120+
source.append('F');
118121
else if (obj instanceof Long)
119-
str.append('L');
122+
source.append('L');
120123
else if (obj instanceof Short)
121-
str.append('S');
124+
source.append('S');
122125
else if (obj instanceof Byte)
123-
str.append('Y');
124-
return str;
126+
source.append('Y');
127+
return source;
125128
}
126129
return CONTINUE;
127130
}

SerialX-core/src/main/java/org/ugp/serialx/converters/ProtocolConverter.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.ugp.serialx.Utils.indexOfNotInObj;
55
import static org.ugp.serialx.Utils.splitValues;
66

7+
import java.io.IOException;
78
import java.io.Serializable;
89
import java.lang.reflect.InvocationTargetException;
910
import java.util.ArrayList;
@@ -166,24 +167,25 @@ protected Object parse(ParserRegistry myHomeRegistry, Class<?> objClass, String
166167
}
167168

168169
@Override
169-
public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Object... args)
170+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException
170171
{
171-
return toString(myHomeRegistry, obj, null, args);
172+
return toString(source, myHomeRegistry, obj, null, args);
172173
}
173174

174175
/**
176+
* @param source | Source to append the properly stringified object (obj) into. Should be treated as only and only {@link Appendable}, no casting!
175177
* @param myHomeRegistry | Registry where this parser is registered provided by {@link ParserRegistry#parse(String, boolean, Class, Object...)} otherwise it demands on implementation (it should not be null)!
176178
* @param obj | Object to convert into string!
177179
* @param preferedProtocol | Protocol to use preferably.
178180
* @param args | Some additional args. This can be anything and it demands on implementation of DataConverter. Default SerialX API implementation will provide some flags about formating (2 ints)!
179181
*
180-
* @return Object converted to string. Easiest way to do this is obj.toString() but you most likely want some more sofisticated formating.
182+
* @return The source appendable after stringified object (obj) was appropriately appended into it.
181183
* Return {@link DataParser#CONTINUE} to tell that this converter is not suitable for converting this object! You most likely want to do this when obtained obj is not suitable instance!
182184
*
183185
* @since 1.3.5
184186
*/
185187
@SuppressWarnings("unchecked")
186-
public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, SerializationProtocol<Object> preferedProtocol, Object... args)
188+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, SerializationProtocol<Object> preferedProtocol, Object... args)
187189
{
188190
if (obj == null)
189191
return CONTINUE;
@@ -205,42 +207,45 @@ public CharSequence toString(ParserRegistry myHomeRegistry, Object obj, Serializ
205207
{
206208
Object[] objArgs = preferedProtocol.serialize(obj);
207209
final int len = objArgs.length;
210+
211+
boolean wrap;
212+
if (wrap = index > 0 && len != 0)
213+
source.append('{');
208214

209-
StringBuilder sb = new StringBuilder(ImportsProvider.getAliasFor(args[0], obj.getClass()));
215+
source.append(ImportsProvider.getAliasFor(args[0], obj.getClass()));
210216
args = args.clone();
211217
if (args[5] instanceof Byte && (byte) args[5] != 0) // Format
212218
{
213-
for (int i = 0, sizeEndl = 10000; i < len; i++)
219+
for (int i = 0; i < len; i++)
214220
{
215-
if (sb.length() > sizeEndl)
221+
if (i != 0 && i % 1000 == 0)
216222
{
217-
sb.append('\n');
223+
source.append('\n');
218224
for (int j = 0; j < tabs+1; j++)
219-
sb.append('\t');
220-
sizeEndl += 10000;
225+
source.append('\t');
221226
}
222227
else
223-
sb.append(' ');
228+
source.append(' ');
224229

225230
if (args.length > 2)
226231
args[2] = index + 1; // DO NOT TOUCH
227-
sb.append(myHomeRegistry.toString(objArgs[i], args));
232+
myHomeRegistry.toString(source, objArgs[i], args);
228233
}
229234
}
230235
else
231236
{
232237
for (int i = 0; i < len; i++)
233238
{
234-
sb.append(' ');
239+
source.append(' ');
235240

236241
if (args.length > 2)
237242
args[2] = index + 1; // DO NOT TOUCH
238-
sb.append(myHomeRegistry.toString(objArgs[i], args));
243+
myHomeRegistry.toString(source, objArgs[i], args);
239244
}
240245
}
241246

242247
args[4] = oldObjectClass;
243-
return index > 0 && len != 0 ? sb.insert(0, '{').append('}') : sb;
248+
return wrap ? source.append('}') : source;
244249
}
245250
catch (Exception e)
246251
{

SerialX-core/src/main/java/org/ugp/serialx/converters/SerializableBase64Converter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
6565
}
6666
catch (Exception e)
6767
{
68-
LogProvider.instance.logErr("Looks like there appear some problems with unserializing some object, the instance of java.io.Serializable from string \"" + arg + "\"! This string is most likely corrupted! See error below:", e);
68+
LogProvider.instance.logErr("Looks like there were some problems with unserializing some object, the instance of java.io.Serializable from string \"" + arg + "\"! This string is most likely corrupted! See error below:", e);
6969
e.printStackTrace();
7070
return null;
7171
}
@@ -74,16 +74,16 @@ public Object parse(ParserRegistry myHomeRegistry, String arg, Object... args)
7474
}
7575

7676
@Override
77-
public CharSequence toString(ParserRegistry myHomeRegistry, Object arg, Object... args)
77+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args)
7878
{
79-
if (arg instanceof Serializable)
79+
if (obj instanceof Serializable)
8080
try
8181
{
82-
return "#" + URLEncoder.encode(SerializeClassic((Serializable) arg), "UTF-8").replace('%', '#');
82+
return source.append('#').append(URLEncoder.encode(SerializeClassic((Serializable) obj), "UTF-8").replace('%', '#'));
8383
}
8484
catch (Exception e)
8585
{
86-
LogProvider.instance.logErr("Looks like there appear some problems with serializing \"" + arg + "\", the instance of java.io.Serializable. This could happen when certain object contains non-transient unserializable objects. Use custom valid protocol for serializing \"" + arg + "\" might solve the problem!", e);
86+
LogProvider.instance.logErr("Looks like there were some problems with serializing \"" + obj + "\", the instance of java.io.Serializable. This could happen when certain object contains non-transient unserializable objects. Using custom valid protocol for serializing \"" + obj + "\" might solve the problem!", e);
8787
e.printStackTrace();
8888
return null;
8989
}

SerialX-core/src/main/java/org/ugp/serialx/converters/StringConverter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.ugp.serialx.Utils.contains;
44
import static org.ugp.serialx.Utils.indexOfNotInObj;
55

6+
import java.io.IOException;
67
import java.util.Map;
78

89
import org.ugp.serialx.Registry;
@@ -48,7 +49,7 @@ public class StringConverter implements DataConverter
4849
protected Map<String, String> parsingCache;
4950

5051
@Override
51-
public String parse(ParserRegistry myHomeRegistry, String str, Object... args)
52+
public Object parse(ParserRegistry myHomeRegistry, String str, Object... args)
5253
{
5354
String result;
5455
int len;
@@ -67,24 +68,24 @@ public String parse(ParserRegistry myHomeRegistry, String str, Object... args)
6768
}
6869

6970
@Override
70-
public CharSequence toString(ParserRegistry myHomeRegistry, Object arg, Object... args)
71+
public Appendable toString(Appendable source, ParserRegistry myHomeRegistry, Object obj, Object... args) throws IOException
7172
{
72-
if (arg instanceof String)
73+
if (obj instanceof String)
7374
{
74-
String str = arg.toString();
75+
String str = (String) obj;
7576
if (str.startsWith("${") && str.endsWith("}"))
7677
{
7778
str = str.substring(2, str.length()-1);
7879
if (str.contains("::") && indexOfNotInObj(str, ' ') > -1)
79-
str = "{"+str+"}";
80-
return str;
80+
return source.append('{').append(str).append('}');
81+
return source.append(str);
8182
}
8283

8384
if (serializeStringNormally)
8485
{
8586
if (contains(str, '\"', '\n', '\r'))
8687
return CONTINUE;
87-
return "\""+str+"\"";
88+
return source.append('"').append(str).append('"');
8889
}
8990
}
9091
return CONTINUE;

0 commit comments

Comments
 (0)