Skip to content

Commit

Permalink
Cache lookups of fromStringConverter
Browse files Browse the repository at this point in the history
* Bidirectional converters have always been cached
* Now fromString uni-directional converters are cached
  • Loading branch information
jodastephen committed Jan 28, 2024
1 parent d7a5b88 commit e4aca55
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/java/org/joda/convert/StringConvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,17 @@ public <T> FromStringConverter<T> findFromStringConverter(final Class<T> cls) {
var converter = findConverterQuiet(cls);
if (converter == null) {
var fromStringConverter = (FromStringConverter<T>) fromStrings.get(cls);
if (fromStringConverter == null) {
if (fromStringConverter == CACHED_NULL) {
throw new IllegalStateException("No registered converter found: " + cls);
}
return fromStringConverter;
}
return converter;
}

// low-level method to find a converter, returning null if not found, unless a factory threw an exception
// the result is a full-featured converter
// if the class only has a from-string converter, the fromStrings map is updated
@SuppressWarnings("unchecked")
private <T> TypedStringConverter<T> findConverterQuiet(final Class<T> cls) {
if (cls == null) {
Expand All @@ -498,11 +501,13 @@ private <T> TypedStringConverter<T> findConverterQuiet(final Class<T> cls) {
if (conv == null) {
registered.putIfAbsent(cls, CACHED_NULL);
// search for from-string only converters now, so that our cache is accurate for all kinds of converter
var fromString = AnnotationStringConverterFactory.INSTANCE.findFromStringConverter(cls);
if (fromString != null) {
fromStrings.put(cls, fromString);
}
return null;
fromStrings.computeIfAbsent(
cls,
key -> {
var fromString = AnnotationStringConverterFactory.INSTANCE.findFromStringConverter(cls);
return fromString != null ? fromString : CACHED_NULL;
});
return null; // caller must lookup fromStrings map
}
registered.putIfAbsent(cls, conv);
}
Expand Down

0 comments on commit e4aca55

Please sign in to comment.