Skip to content

Commit

Permalink
added private constructors for static classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 30, 2024
1 parent e96ba1d commit cdfda89
Show file tree
Hide file tree
Showing 29 changed files with 215 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The classes in the`.jar`file are version 52 (`JDK 1.8`).

---
To include in your project:
##### Gradle
##### GradleF
```
implementation 'com.cedarsoftware:java-util:2.4.0'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class AtomicBooleanConversions {
public final class AtomicBooleanConversions {

private AtomicBooleanConversions() {}

static Byte toByte(Object from, Converter converter, ConverterOptions options) {
AtomicBoolean b = (AtomicBoolean) from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
* limitations under the License.
*/
public final class BooleanConversions {

private BooleanConversions() {
}
private BooleanConversions() {}

static Byte toByte(Object from, Converter converter, ConverterOptions options) {
Boolean b = (Boolean) from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.nio.CharBuffer;
import java.util.concurrent.atomic.AtomicInteger;

public class ByteArrayConversions {
public final class ByteArrayConversions {

private ByteArrayConversions() {}

static String toString(Object from, ConverterOptions options) {
byte[] bytes = (byte[])from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

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

public class ByteBufferConversions {
public final class ByteBufferConversions {

private ByteBufferConversions() {}

static ByteBuffer asReadOnlyBuffer(Object from) {
// Create a readonly buffer so we aren't changing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CalendarConversions {
public final class CalendarConversions {

private CalendarConversions() {}

static Date toDate(Object from) {
return ((Calendar)from).getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.nio.CharBuffer;
import java.util.Arrays;

public class CharArrayConversions {
public final class CharArrayConversions {

private CharArrayConversions() {}

static String toString(Object from) {
char[] chars = (char[])from;
Expand All @@ -25,7 +27,6 @@ static String toString(Object from, Converter converter, ConverterOptions option
return toString(from);
}


static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) {
return toCharBuffer(from);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY;
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_CHAR_ARRAY;

public class CharBufferConversions {
public final class CharBufferConversions {

private CharBufferConversions() {}

static CharBuffer asReadOnlyBuffer(Object from) {
// Create a readonly buffer so we aren't changing
// the original buffers mark and position when
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.cedarsoftware.util.convert;

public class CharacterArrayConversions {

static StringBuilder toStringBuilder(Object from) {
Character[] chars = (Character[]) from;
StringBuilder builder = new StringBuilder(chars.length);
for (Character ch : chars) {
builder.append(ch);
}
return builder;
}

static StringBuffer toStringBuffer(Object from) {
Character[] chars = (Character[]) from;
StringBuffer buffer = new StringBuffer(chars.length);
for (Character ch : chars) {
buffer.append(ch);
}
return buffer;
}

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CharacterConversions {
public final class CharacterConversions {

private CharacterConversions() {
}
private CharacterConversions() {}

static boolean toBoolean(Object from) {
char c = (char) from;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.cedarsoftware.util.convert;

public class ClassConversions {
public final class ClassConversions {

private ClassConversions() {}

static String toString(Object from, Converter converter, ConverterOptions options) {
Class<?> cls = (Class<?>) from;
return cls.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CommonValues {
public final class CommonValues {

private CommonValues() {}
public static final Byte BYTE_ZERO = (byte) 0;
public static final Byte BYTE_ONE = (byte) 1;
public static final Short SHORT_ZERO = (short) 0;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,10 @@ private static void buildFactoryConversions() {
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(Character[].class, String.class), CharacterArrayConversions::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(Class.class, String.class), ClassConversions::toString);
DEFAULT_FACTORY.put(pair(Date.class, String.class), DateConversions::dateToString);
DEFAULT_FACTORY.put(pair(java.sql.Date.class, String.class), DateConversions::sqlDateToString);
DEFAULT_FACTORY.put(pair(Timestamp.class, String.class), DateConversions::timestampToString);
Expand Down Expand Up @@ -649,6 +650,7 @@ private static void buildFactoryConversions() {
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(Character[].class, StringBuffer.class), CharacterArrayConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(char[].class, StringBuffer.class), CharArrayConversions::toStringBuffer);
DEFAULT_FACTORY.put(pair(byte[].class, StringBuffer.class), ByteArrayConversions::toStringBuffer);

Expand All @@ -659,6 +661,7 @@ private static void buildFactoryConversions() {
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(Character[].class, StringBuilder.class), CharacterArrayConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(char[].class, StringBuilder.class), CharArrayConversions::toStringBuilder);
DEFAULT_FACTORY.put(pair(byte[].class, StringBuilder.class), ByteArrayConversions::toStringBuilder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@
public interface ConverterOptions {


ConcurrentHashMap<String, Object> customOptions = new ConcurrentHashMap();

/**
* @return zoneId to use for source conversion when on is not provided on the source (Date, Instant, etc.)
*/
//TODO: should we just throw an exception here if they don't override?
default ZoneId getSourceZoneIdForLocalDates() { return ZoneId.systemDefault(); }

/**
Expand All @@ -59,7 +56,7 @@ public interface ConverterOptions {
/**
* @return custom option
*/
default <T> T getCustomOption(String name) { return (T)customOptions.get(name); }
default <T> T getCustomOption(String name) { return null; }

/**
* @return TimeZone expected on the target when finished (only for types that support ZoneId or TimeZone)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class DateConversions {
public final class DateConversions {

private DateConversions() {}

static long toLong(Object from) {
return ((Date) from).getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class DurationConversions {
public final class DurationConversions {

private DurationConversions() {}

static Map toMap(Object from, Converter converter, ConverterOptions options) {
long sec = ((Duration) from).getSeconds();
long nanos = ((Duration) from).getNano();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class InstantConversions {
public final class InstantConversions {

private InstantConversions() {}

static long toLong(Object from) {
return ((Instant)from).toEpochMilli();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class LocalDateConversions {
public final class LocalDateConversions {

private LocalDateConversions() {}

private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return ((LocalDate)from).atStartOfDay(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class LocalDateTimeConversions {
public final class LocalDateTimeConversions {

private LocalDateTimeConversions() {}

private static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return ((LocalDateTime)from).atZone(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class LocalTimeConversions {
public final class LocalTimeConversions {

private LocalTimeConversions() {}

static Map toMap(Object from, Converter converter, ConverterOptions options) {
LocalTime localTime = (LocalTime) from;
Expand All @@ -39,4 +41,4 @@ static Map toMap(Object from, Converter converter, ConverterOptions options) {
}
return target;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class MapConversions {
public final class MapConversions {

private static final String V = "_v";
private static final String VALUE = "value";
Expand All @@ -59,6 +59,8 @@ public class MapConversions {
private static final String NANOS = "nanos";
private static final String MOST_SIG_BITS = "mostSigBits";
private static final String LEAST_SIG_BITS = "leastSigBits";

private MapConversions() {}

public static final String KEY_VALUE_ERROR_MESSAGE = "To convert from Map to %s the map must include one of the following: %s[_v], or [value] with associated values.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class MonthDayConversions {
public final class MonthDayConversions {

private MonthDayConversions() {}

static Map toMap(Object from, Converter converter, ConverterOptions options) {
MonthDay monthDay = (MonthDay) from;
Map<String, Object> target = new CompactLinkedMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class NumberConversions {
public final class NumberConversions {

private NumberConversions() {}

static byte toByte(Object from, Converter converter, ConverterOptions options) {
return ((Number)from).byteValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class StringConversions {
public final class StringConversions {
private static final BigDecimal bigDecimalMinByte = BigDecimal.valueOf(Byte.MIN_VALUE);
private static final BigDecimal bigDecimalMaxByte = BigDecimal.valueOf(Byte.MAX_VALUE);
private static final BigDecimal bigDecimalMinShort = BigDecimal.valueOf(Short.MIN_VALUE);
Expand All @@ -55,6 +55,8 @@ public class StringConversions {
private static final BigDecimal bigDecimalMaxLong = BigDecimal.valueOf(Long.MAX_VALUE);
private static final BigDecimal bigDecimalMinLong = BigDecimal.valueOf(Long.MIN_VALUE);

private StringConversions() {}

static String asString(Object from) {
return from == null ? null : from.toString();
}
Expand Down Expand Up @@ -264,10 +266,6 @@ static Class<?> toClass(Object from, Converter converter, ConverterOptions optio
throw new IllegalArgumentException("Cannot convert String '" + str + "' to class. Class not found.");
}

static String classToString(Object from, Converter converter, ConverterOptions options) {
return ((Class<?>) from).getName();
}

static MonthDay toMonthDay(Object from, Converter converter, ConverterOptions options) {
String monthDay = (String) from;
return MonthDay.parse(monthDay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
* limitations under the License.
*/
public final class VoidConversions {

private VoidConversions() {
}

static Object toNull(Object from, Converter converter, ConverterOptions options) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class ZonedDateTimeConversions {
public final class ZonedDateTimeConversions {

private ZonedDateTimeConversions() {}

static ZonedDateTime toDifferentZone(Object from, ConverterOptions options) {
return ((ZonedDateTime)from).withZoneSameInstant(options.getZoneId());
Expand Down
Loading

0 comments on commit cdfda89

Please sign in to comment.