Skip to content

Commit

Permalink
- Added new Converter conversions for StringBuffer to StringBuilder t…
Browse files Browse the repository at this point in the history
…o String.

- Added new ClassUtilities API that allows you to test if one class is a wrapper for another
  • Loading branch information
jdereg committed Apr 22, 2024
1 parent 9467d14 commit 50d5f85
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ Both of these features ensure that our library can be seamlessly integrated into
To include in your project:
##### Gradle
```
implementation 'com.cedarsoftware:java-util:2.7.0'
implementation 'com.cedarsoftware:java-util:2.8.0'
```

##### Maven
```
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
</dependency>
```
---
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
### Revision History
* 2.8.0
* Added `ClassUtilities.doesOneWrapTheOther()` API so that it is easy to test if one class is wrapping the other.
* Added `StringBuilder` and `StringBuffer` to `Strings` to the `Converter.` Eliminates special cases for `.toString()` calls where generalized `convert(src, type)` is being used.
* 2.7.0
* Added `ConcurrentHashList,` which implements a thread-safe `List.` Provides all API support except for `listIterator(),` however, it implements `iterator()` which returns an iterator to a snapshot copy of the `List.`
* Added `ConcurrentHashSet,` a true `Set` which is a bit easier to use than `ConcurrentSkipListSet,` which as a `NaviableSet` and `SortedSet,` requires each element to be `Comparable.`
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<packaging>bundle</packaging>
<version>2.7.0</version>
<version>2.8.0</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down
24 changes: 21 additions & 3 deletions src/main/java/com/cedarsoftware/util/ClassUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
public class ClassUtilities
{
private static final Set<Class<?>> prims = new HashSet<>();

private static final Map<Class<?>, Class<?>> primitiveToWrapper = new HashMap<>(20, .8f);
private static final Map<String, Class<?>> nameToClass = new HashMap<>();
private static final Map<Class<?>, Class<?>> wrapperMap = new HashMap<>();

static
{
Expand Down Expand Up @@ -76,6 +76,23 @@ public class ClassUtilities
primitiveToWrapper.put(byte.class, Byte.class);
primitiveToWrapper.put(short.class, Short.class);
primitiveToWrapper.put(void.class, Void.class);

wrapperMap.put(int.class, Integer.class);
wrapperMap.put(Integer.class, int.class);
wrapperMap.put(char.class, Character.class);
wrapperMap.put(Character.class, char.class);
wrapperMap.put(byte.class, Byte.class);
wrapperMap.put(Byte.class, byte.class);
wrapperMap.put(short.class, Short.class);
wrapperMap.put(Short.class, short.class);
wrapperMap.put(long.class, Long.class);
wrapperMap.put(Long.class, long.class);
wrapperMap.put(float.class, Float.class);
wrapperMap.put(Float.class, float.class);
wrapperMap.put(double.class, Double.class);
wrapperMap.put(Double.class, double.class);
wrapperMap.put(boolean.class, Boolean.class);
wrapperMap.put(Boolean.class, boolean.class);
}

/**
Expand Down Expand Up @@ -178,7 +195,6 @@ private static int comparePrimitiveToWrapper(Class<?> source, Class<?> destinati
}
}


/**
* Given the passed in String class name, return the named JVM class.
* @param name String name of a JVM class.
Expand Down Expand Up @@ -333,5 +349,7 @@ public static Class<?> toPrimitiveWrapperClass(Class<?> primitiveClass) {
return c;
}


public static boolean doesOneWrapTheOther(Class<?> x, Class<?> y) {
return wrapperMap.get(x) == y;
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,9 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(URL.class, String.class), StringConversions::toString);
CONVERSION_DB.put(pair(URI.class, String.class), StringConversions::toString);
CONVERSION_DB.put(pair(TimeZone.class, String.class), TimeZoneConversions::toString);

CONVERSION_DB.put(pair(StringBuilder.class, String.class), StringBuilderConversions::toString);
CONVERSION_DB.put(pair(StringBuffer.class, String.class), StringBufferConversions::toString);

// URL conversions
CONVERSION_DB.put(pair(Void.class, URL.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(URL.class, URL.class), Converter::identity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cedarsoftware.util.convert;

/**
* @author John DeRegnaucourt ([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 StringBufferConversions {

private StringBufferConversions() {}

static String toString(Object from, Converter converter) {
return from.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cedarsoftware.util.convert;

/**
* @author John DeRegnaucourt ([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 StringBuilderConversions {

private StringBuilderConversions() {}

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

0 comments on commit 50d5f85

Please sign in to comment.