Skip to content

Commit

Permalink
Updated TestConverter to have no commented out tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Jan 10, 2024
1 parent 2ef887d commit 150bf17
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 240 deletions.
55 changes: 4 additions & 51 deletions src/main/java/com/cedarsoftware/util/Converter.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.cedarsoftware.util;

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

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
Expand All @@ -12,16 +8,17 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

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

/**
* Handy conversion utilities. Convert from primitive to other primitives, plus support for Date, TimeStamp SQL Date,
* and the Atomic's.
Expand Down Expand Up @@ -111,50 +108,6 @@ public static String convertToString(Object fromInstance)
return instance.convert(fromInstance, String.class);
}

public static Class<?> convertToClass(Object fromInstance) {
if (fromInstance instanceof Class) {
return (Class<?>)fromInstance;
} else if (fromInstance instanceof String) {
try {
Class<?> clazz = Class.forName((String)fromInstance);
return clazz;
}
catch (ClassNotFoundException ignore) {
}
}
throw new IllegalArgumentException("value [" + name(fromInstance) + "] could not be converted to a 'Class'");
}

public static UUID convertToUUID(Object fromInstance) {
try {
if (fromInstance instanceof UUID) {
return (UUID)fromInstance;
} else if (fromInstance instanceof String) {
return UUID.fromString((String)fromInstance);
} else if (fromInstance instanceof BigInteger) {
BigInteger bigInteger = (BigInteger) fromInstance;
BigInteger mask = BigInteger.valueOf(Long.MAX_VALUE);
long mostSignificantBits = bigInteger.shiftRight(64).and(mask).longValue();
long leastSignificantBits = bigInteger.and(mask).longValue();
return new UUID(mostSignificantBits, leastSignificantBits);
}
else if (fromInstance instanceof Map) {
Map<?, ?> map = (Map<?, ?>) fromInstance;
if (map.containsKey("mostSigBits") && map.containsKey("leastSigBits")) {
long mostSigBits = convert2long(map.get("mostSigBits"));
long leastSigBits = convert2long(map.get("leastSigBits"));
return new UUID(mostSigBits, leastSigBits);
} else {
throw new IllegalArgumentException("To convert Map to UUID, the Map must contain both a 'mostSigBits' and 'leastSigBits' key.");
}
}
} catch (Exception e) {
throw new IllegalArgumentException("value [" + name(fromInstance) + "] could not be converted to a 'UUID'", e);
}
nope(fromInstance, "UUID");
return null;
}

/**
* Convert from the passed in instance to a BigDecimal. If null or "" is passed in, this method will return a
* BigDecimal with the value of 0. Possible inputs are String (base10 numeric values in string), BigInteger,
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.cedarsoftware.util.convert;


import com.cedarsoftware.util.ClassUtilities;
import com.cedarsoftware.util.CollectionUtilities;
import com.cedarsoftware.util.DateUtilities;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
Expand Down Expand Up @@ -37,6 +33,10 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.ClassUtilities;
import com.cedarsoftware.util.CollectionUtilities;
import com.cedarsoftware.util.DateUtilities;

/**
* Instance conversion utility. Convert from primitive to other primitives, plus support for Number, Date,
* TimeStamp, SQL Date, LocalDate, LocalDateTime, ZonedDateTime, Calendar, Big*, Atomic*, Class, UUID,
Expand Down Expand Up @@ -538,7 +538,7 @@ private static void buildFactoryConversions() {
try {
return new AtomicLong(Long.parseLong(str));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Value: " + fromInstance + " not parseable as a AtomicLong value or outside " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
throw new IllegalArgumentException("Value: " + fromInstance + " not parseable as an AtomicLong value or outside " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
}
});

Expand Down
Loading

0 comments on commit 150bf17

Please sign in to comment.