Skip to content

Commit

Permalink
Removed options from 'Convert' functional interface. Removed options …
Browse files Browse the repository at this point in the history
…from being able to be passed in on the convert() API. The options are used from the Convert instance, which was constructed with ConvertOptions.
  • Loading branch information
jdereg committed Feb 10, 2024
1 parent 7a32f34 commit 52ab759
Show file tree
Hide file tree
Showing 39 changed files with 1,734 additions and 1,815 deletions.
12 changes: 1 addition & 11 deletions src/main/java/com/cedarsoftware/util/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.cedarsoftware.util.convert.CommonValues;
import com.cedarsoftware.util.convert.Convert;
import com.cedarsoftware.util.convert.ConverterOptions;
import com.cedarsoftware.util.convert.DefaultConverterOptions;

/**
Expand Down Expand Up @@ -75,16 +74,7 @@ private Converter() { }
public static <T> T convert(Object fromInstance, Class<T> toType) {
return instance.convert(fromInstance, toType);
}

/**
* Allows you to specify (per each call) different conversion options. Useful so you don't have
* to recreate the instance of Converter that is out there for every configuration option. Just
* provide a different set of ConverterOptions on the call itself.
*/
public static <T> T convert(Object fromInstance, Class<T> toType, ConverterOptions options) {
return instance.convert(fromInstance, toType, options);
}


/**
* Check to see if a conversion from type to another type is supported (may use inheritance via super classes/interfaces).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,72 +23,73 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class AtomicBooleanConversions {
final class AtomicBooleanConversions {

private AtomicBooleanConversions() {}

static Byte toByte(Object from, Converter converter, ConverterOptions options) {
static Byte toByte(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.BYTE_ONE : CommonValues.BYTE_ZERO;
}

static Short toShort(Object from, Converter converter, ConverterOptions options) {
static Short toShort(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.SHORT_ONE : CommonValues.SHORT_ZERO;
}

static Integer toInteger(Object from, Converter converter, ConverterOptions options) {
static Integer toInteger(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.INTEGER_ONE : CommonValues.INTEGER_ZERO;
}

static Long toLong(Object from, Converter converter, ConverterOptions options) {
static Long toLong(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.LONG_ONE : CommonValues.LONG_ZERO;
}

static Float toFloat(Object from, Converter converter, ConverterOptions options) {
static Float toFloat(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.FLOAT_ONE : CommonValues.FLOAT_ZERO;
}

static Double toDouble(Object from, Converter converter, ConverterOptions options) {
static Double toDouble(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.DOUBLE_ONE : CommonValues.DOUBLE_ZERO;
}

static boolean toBoolean(Object from, Converter converter, ConverterOptions options) {
static boolean toBoolean(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get();
}

static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) {
static AtomicBoolean toAtomicBoolean(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return new AtomicBoolean(b.get());
}

static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) {
static AtomicInteger toAtomicInteger(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? new AtomicInteger(1) : new AtomicInteger (0);
}


static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) {
static AtomicLong toAtomicLong(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? new AtomicLong(1) : new AtomicLong(0);
}

static Character toCharacter(Object from, Converter converter, ConverterOptions options) {
static Character toCharacter(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
ConverterOptions options = converter.getOptions();
return b.get() ? options.trueChar() : options.falseChar();
}

static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
static BigDecimal toBigDecimal(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? BigDecimal.ONE : BigDecimal.ZERO;
}

public static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
public static BigInteger toBigInteger(Object from, Converter converter) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? BigInteger.ONE : BigInteger.ZERO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,66 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class BooleanConversions {
final class BooleanConversions {
private BooleanConversions() {}

static Byte toByte(Object from, Converter converter, ConverterOptions options) {
static Byte toByte(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b ? CommonValues.BYTE_ONE : CommonValues.BYTE_ZERO;
}

static Short toShort(Object from, Converter converter, ConverterOptions options) {
static Short toShort(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b ? CommonValues.SHORT_ONE : CommonValues.SHORT_ZERO;
}

static Integer toInteger(Object from, Converter converter, ConverterOptions options) {
static Integer toInteger(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b ? CommonValues.INTEGER_ONE : CommonValues.INTEGER_ZERO;
}

static AtomicInteger toAtomicInteger(Object from, Converter converter, ConverterOptions options) {
static AtomicInteger toAtomicInteger(Object from, Converter converter) {
Boolean b = (Boolean) from;
return new AtomicInteger(b ? 1 : 0);
}

static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOptions options) {
static AtomicLong toAtomicLong(Object from, Converter converter) {
Boolean b = (Boolean) from;
return new AtomicLong(b ? 1 : 0);
}

static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) {
static AtomicBoolean toAtomicBoolean(Object from, Converter converter) {
Boolean b = (Boolean) from;
return new AtomicBoolean(b);
}

static Long toLong(Object from, Converter converter, ConverterOptions options) {
static Long toLong(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b.booleanValue() ? CommonValues.LONG_ONE : CommonValues.LONG_ZERO;
}

static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
static BigDecimal toBigDecimal(Object from, Converter converter) {
Boolean b = (Boolean)from;
return b ? BigDecimal.ONE : BigDecimal.ZERO;
}

static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
static BigInteger toBigInteger(Object from, Converter converter) {
return ((Boolean)from) ? BigInteger.ONE : BigInteger.ZERO;
}

static Float toFloat(Object from, Converter converter, ConverterOptions options) {
static Float toFloat(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b ? CommonValues.FLOAT_ONE : CommonValues.FLOAT_ZERO;
}

static Double toDouble(Object from, Converter converter, ConverterOptions options) {
static Double toDouble(Object from, Converter converter) {
Boolean b = (Boolean) from;
return b ? CommonValues.DOUBLE_ONE : CommonValues.DOUBLE_ZERO;
}

static char toCharacter(Object from, Converter converter, ConverterOptions options) {
static char toCharacter(Object from, Converter converter) {
Boolean b = (Boolean) from;
ConverterOptions options = converter.getOptions();
return b ? options.trueChar() : options.falseChar();
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
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 final class ByteArrayConversions {
import com.cedarsoftware.util.StringUtilities;

/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class ByteArrayConversions {

private ByteArrayConversions() {}

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

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

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

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

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

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

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
Expand Up @@ -5,20 +5,42 @@

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

public final class ByteBufferConversions {
/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class ByteBufferConversions {

private ByteBufferConversions() {}

static CharBuffer toCharBuffer(Object from, Converter converter) {
ByteBuffer buffer = toByteBuffer(from, converter);
return converter.getOptions().getCharset().decode(buffer);
}

static ByteBuffer asReadOnlyBuffer(Object from) {
static ByteBuffer toByteBuffer(Object from, Converter converter) {
// 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();
return ((ByteBuffer) from).asReadOnlyBuffer();
}

static byte[] toByteArray(Object from) {
ByteBuffer buffer = asReadOnlyBuffer(from);
static byte[] toByteArray(Object from, Converter converter) {
ByteBuffer buffer = toByteBuffer(from, converter);

if (buffer == null || !buffer.hasRemaining()) {
return EMPTY_BYTE_ARRAY;
Expand All @@ -29,37 +51,19 @@ static byte[] toByteArray(Object from) {
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 String toString(Object from, Converter converter) {
return toCharBuffer(from, converter).toString();
}

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

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

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

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));
}

}
Loading

0 comments on commit 52ab759

Please sign in to comment.