Skip to content

Commit

Permalink
Added StringBuffer, StringBuidler, ByteBuffer, CharBuffer, byte[], ch…
Browse files Browse the repository at this point in the history
…ar[] conversions....more tests to come
  • Loading branch information
kpartlow committed Jan 28, 2024
1 parent 333788e commit a3b3478
Show file tree
Hide file tree
Showing 9 changed files with 501 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/cedarsoftware/util/ArrayUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public final class ArrayUtilities
* Immutable common arrays.
*/
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];

public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final char[] EMPTY_CHAR_ARRAY = new char[0];

public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cedarsoftware.util.convert;

import com.cedarsoftware.util.StringUtilities;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.concurrent.atomic.AtomicInteger;

public class ByteArrayConversions {

static String toString(Object from, ConverterOptions options) {
byte[] bytes = (byte[])from;
return (bytes == null) ? StringUtilities.EMPTY : new String(bytes, options.getCharset());
}

static ByteBuffer toByteBuffer(Object from) {
return ByteBuffer.wrap((byte[])from);
}

static String toString(Object from, Converter converter, ConverterOptions options) {
return toString(from, options);
}

static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) {
return toByteBuffer(from);
}

static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) {
return CharBuffer.wrap(toString(from, options));
}

static char[] toCharArray(Object from, Converter converter, ConverterOptions options) {
return toString(from, options).toCharArray();
}

static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) {
return new StringBuffer(toString(from, options));
}

static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) {
return new StringBuilder(toString(from, options));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.cedarsoftware.util.convert;

import com.cedarsoftware.util.StringUtilities;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY;

public class ByteBufferConversions {

static ByteBuffer asReadOnlyBuffer(Object from) {
// Create a readonly buffer so we aren't changing
// the original buffers mark and position when
// working with this buffer. This could be inefficient
// if constantly fed with writeable buffers so should be documented
return ((ByteBuffer)from).asReadOnlyBuffer();
}

static byte[] toByteArray(Object from) {
ByteBuffer buffer = asReadOnlyBuffer(from);

if (buffer == null || !buffer.hasRemaining()) {
return EMPTY_BYTE_ARRAY;
}

byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return bytes;
}

static CharBuffer toCharBuffer(Object from, ConverterOptions options) {
ByteBuffer buffer = asReadOnlyBuffer(from);
return options.getCharset().decode(buffer);
}


static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) {
return toCharBuffer(from, options);
}

static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) {
return asReadOnlyBuffer(from);
}

static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) {
return toByteArray(from);
}

static String toString(Object from, Converter converter, ConverterOptions options) {
return toCharBuffer(from, options).toString();
}

static char[] toCharArray(Object from, Converter converter, ConverterOptions options) {
return CharBufferConversions.toCharArray(toCharBuffer(from, options));
}

static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) {
return new StringBuffer(toCharBuffer(from, options));
}

static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) {
return new StringBuilder(toCharBuffer(from, options));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.cedarsoftware.util.convert;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Arrays;

public class CharArrayConversions {

static String toString(Object from) {
char[] chars = (char[])from;
return new String(chars);
}

static CharBuffer toCharBuffer(Object from) {
char[] chars = (char[])from;
return CharBuffer.wrap(chars);
}

static ByteBuffer toByteBuffer(Object from, ConverterOptions options) {
return options.getCharset().encode(toCharBuffer(from));
}


static String toString(Object from, Converter converter, ConverterOptions options) {
return toString(from);
}


static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) {
return toCharBuffer(from);
}

static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) {
return new StringBuffer(toCharBuffer(from));
}

static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) {
return new StringBuilder(toCharBuffer(from));
}

static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) {
return toByteBuffer(from, options);
}

static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) {
ByteBuffer buffer = toByteBuffer(from, options);
byte[] byteArray = new byte[buffer.remaining()];
buffer.get(byteArray);
return byteArray;
}

static char[] toCharArray(Object from, Converter converter, ConverterOptions options) {
char[] chars = (char[])from;
if (chars == null) {
return null;
}
return Arrays.copyOf(chars, chars.length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.cedarsoftware.util.convert;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY;
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_CHAR_ARRAY;

public class CharBufferConversions {
static CharBuffer asReadOnlyBuffer(Object from) {
// Create a readonly buffer so we aren't changing
// the original buffers mark and position when
// working with this buffer. This could be inefficient
// if constantly fed with writeable buffers so should be documented
return ((CharBuffer)from).asReadOnlyBuffer();
}

static char[] toCharArray(Object from) {
CharBuffer buffer = asReadOnlyBuffer(from);

if (buffer == null || !buffer.hasRemaining()) {
return EMPTY_CHAR_ARRAY;
}

char[] chars = new char[buffer.remaining()];
buffer.get(chars);
return chars;
}

static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) {
return asReadOnlyBuffer(from);
}

static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) {
return ByteBufferConversions.toByteArray(toByteBuffer(from, converter, options));
}

static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) {
return options.getCharset().encode(asReadOnlyBuffer(from));
}

static String toString(Object from, Converter converter, ConverterOptions options) {
return asReadOnlyBuffer(from).toString();
}

static char[] toCharArray(Object from, Converter converter, ConverterOptions options) {
return toCharArray(from);
}

static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) {
return new StringBuffer(asReadOnlyBuffer(from));
}

static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) {
return new StringBuilder(asReadOnlyBuffer(from));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cedarsoftware.util.convert;

public class ClassConversions {
static String toString(Object from, Converter converter, ConverterOptions options) {
Class<?> cls = (Class<?>) from;
return cls.getName();
}
}
78 changes: 72 additions & 6 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -94,7 +96,7 @@ private static void buildPrimitiveWrappers() {
}

private static void buildFactoryConversions() {
// Byte/byte Conversions supported
// toByte
DEFAULT_FACTORY.put(pair(Void.class, byte.class), NumberConversions::toByteZero);
DEFAULT_FACTORY.put(pair(Void.class, Byte.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Byte.class, Byte.class), Converter::identity);
Expand All @@ -115,7 +117,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Map.class, Byte.class), MapConversions::toByte);
DEFAULT_FACTORY.put(pair(String.class, Byte.class), StringConversions::toByte);

// Short/short conversions supported
// toShort
DEFAULT_FACTORY.put(pair(Void.class, short.class), NumberConversions::toShortZero);
DEFAULT_FACTORY.put(pair(Void.class, Short.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Byte.class, Short.class), NumberConversions::toShort);
Expand All @@ -135,7 +137,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Map.class, Short.class), MapConversions::toShort);
DEFAULT_FACTORY.put(pair(String.class, Short.class), StringConversions::toShort);

// Integer/int conversions supported
// toInteger
DEFAULT_FACTORY.put(pair(Void.class, int.class), NumberConversions::toIntZero);
DEFAULT_FACTORY.put(pair(Void.class, Integer.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Byte.class, Integer.class), NumberConversions::toInt);
Expand All @@ -155,7 +157,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Map.class, Integer.class), MapConversions::toInt);
DEFAULT_FACTORY.put(pair(String.class, Integer.class), StringConversions::toInt);

// Long/long conversions supported
// toLong
DEFAULT_FACTORY.put(pair(Void.class, long.class), NumberConversions::toLongZero);
DEFAULT_FACTORY.put(pair(Void.class, Long.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Byte.class, Long.class), NumberConversions::toLong);
Expand Down Expand Up @@ -183,7 +185,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Map.class, Long.class), MapConversions::toLong);
DEFAULT_FACTORY.put(pair(String.class, Long.class), StringConversions::toLong);

// Float/float conversions supported
// toFloat
DEFAULT_FACTORY.put(pair(Void.class, float.class), NumberConversions::toFloatZero);
DEFAULT_FACTORY.put(pair(Void.class, Float.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Byte.class, Float.class), NumberConversions::toFloat);
Expand Down Expand Up @@ -577,6 +579,10 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(AtomicBoolean.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(AtomicInteger.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(AtomicLong.class, String.class), StringConversions::toString);
DEFAULT_FACTORY.put(pair(byte[].class, String.class), ByteArrayConversions::toString);
DEFAULT_FACTORY.put(pair(char[].class, String.class), CharArrayConversions::toString);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, String.class), ByteBufferConversions::toString);
DEFAULT_FACTORY.put(pair(CharBuffer.class, String.class), CharBufferConversions::toString);
DEFAULT_FACTORY.put(pair(Class.class, String.class), StringConversions::classToString);
DEFAULT_FACTORY.put(pair(Date.class, String.class), DateConversions::dateToString);
DEFAULT_FACTORY.put(pair(java.sql.Date.class, String.class), DateConversions::sqlDateToString);
Expand Down Expand Up @@ -636,6 +642,66 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(String.class, MonthDay.class), StringConversions::toMonthDay);
DEFAULT_FACTORY.put(pair(Map.class, MonthDay.class), MapConversions::toMonthDay);

// toStringBuffer
DEFAULT_FACTORY.put(pair(Void.class, StringBuffer.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(String.class, StringBuffer.class), StringConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(StringBuilder.class, StringBuffer.class), StringConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(StringBuffer.class, StringBuffer.class), StringConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, StringBuffer.class), ByteBufferConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(CharBuffer.class, StringBuffer.class), CharBufferConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(char[].class, StringBuffer.class), CharArrayConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(byte[].class, StringBuffer.class), ByteArrayConversions::toStringBuffer);

// toStringBuilder
DEFAULT_FACTORY.put(pair(Void.class, StringBuilder.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(String.class, StringBuilder.class), StringConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(StringBuilder.class, StringBuilder.class), StringConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(StringBuffer.class, StringBuilder.class), StringConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, StringBuilder.class), ByteBufferConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(CharBuffer.class, StringBuilder.class), CharBufferConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(char[].class, StringBuilder.class), CharArrayConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(byte[].class, StringBuilder.class), ByteArrayConversions::toStringBuilder);

// toByteArray
DEFAULT_FACTORY.put(pair(Void.class, byte[].class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(String.class, byte[].class), StringConversions::toByteArray);
DEFAULT_FACTORY.put(pair(StringBuilder.class, byte[].class), StringConversions::toByteArray);
DEFAULT_FACTORY.put(pair(StringBuffer.class, byte[].class), StringConversions::toByteArray);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, byte[].class), ByteBufferConversions::toByteArray);
DEFAULT_FACTORY.put(pair(CharBuffer.class, byte[].class), CharBufferConversions::toByteArray);
DEFAULT_FACTORY.put(pair(char[].class, byte[].class), CharArrayConversions::toByteArray);
DEFAULT_FACTORY.put(pair(byte[].class, byte[].class), Converter::identity);

// toCharArray
DEFAULT_FACTORY.put(pair(Void.class, char[].class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(String.class, char[].class), StringConversions::toCharArray);
DEFAULT_FACTORY.put(pair(StringBuilder.class, char[].class), StringConversions::toCharArray);
DEFAULT_FACTORY.put(pair(StringBuffer.class, char[].class), StringConversions::toCharArray);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, char[].class), ByteBufferConversions::toCharArray);
DEFAULT_FACTORY.put(pair(CharBuffer.class, char[].class), CharBufferConversions::toCharArray);
DEFAULT_FACTORY.put(pair(char[].class, char[].class), CharArrayConversions::toCharArray);
DEFAULT_FACTORY.put(pair(byte[].class, char[].class), ByteArrayConversions::toCharArray);

//toCharBuffer
DEFAULT_FACTORY.put(pair(Void.class, CharBuffer.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(String.class, CharBuffer.class), StringConversions::toCharBuffer);
DEFAULT_FACTORY.put(pair(StringBuilder.class, CharBuffer.class), StringConversions::toCharBuffer);
DEFAULT_FACTORY.put(pair(StringBuffer.class, CharBuffer.class), StringConversions::toCharBuffer);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, CharBuffer.class), ByteBufferConversions::toCharBuffer);
DEFAULT_FACTORY.put(pair(CharBuffer.class, CharBuffer.class), CharBufferConversions::toCharBuffer);
DEFAULT_FACTORY.put(pair(char[].class, CharBuffer.class), CharArrayConversions::toCharBuffer);
DEFAULT_FACTORY.put(pair(byte[].class, CharBuffer.class), ByteArrayConversions::toCharBuffer);

// toByteBuffer
DEFAULT_FACTORY.put(pair(Void.class, ByteBuffer.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(String.class, ByteBuffer.class), StringConversions::toByteBuffer);
DEFAULT_FACTORY.put(pair(StringBuilder.class, ByteBuffer.class), StringConversions::toByteBuffer);
DEFAULT_FACTORY.put(pair(StringBuffer.class, ByteBuffer.class), StringConversions::toByteBuffer);
DEFAULT_FACTORY.put(pair(ByteBuffer.class, ByteBuffer.class), ByteBufferConversions::toByteBuffer);
DEFAULT_FACTORY.put(pair(CharBuffer.class, ByteBuffer.class), CharBufferConversions::toByteBuffer);
DEFAULT_FACTORY.put(pair(char[].class, ByteBuffer.class), CharArrayConversions::toByteBuffer);
DEFAULT_FACTORY.put(pair(byte[].class, ByteBuffer.class), ByteArrayConversions::toByteBuffer);

// Map conversions supported
DEFAULT_FACTORY.put(pair(Void.class, Map.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Byte.class, Map.class), MapConversions::initMap);
Expand Down Expand Up @@ -963,4 +1029,4 @@ private static Class<?> toPrimitiveWrapperClass(Class<?> primitiveClass) {
private static <T> T identity(T one, Converter converter, ConverterOptions options) {
return one;
}
}
}
Loading

0 comments on commit a3b3478

Please sign in to comment.