Skip to content

Commit

Permalink
> * ClassUtilities.getClassLoader() added. This will safely return …
Browse files Browse the repository at this point in the history
…the correct class loader when running in OSGi, JPMS, or neither.

> * `ArrayUtilities.createArray()` added. This method accepts a variable number of arguments and returns them as an array of type `T[].`
> * Fixed bug when converting `Map` containing "time" key (and no `date` nor `zone` keys) with value to `java.sql.Date.`  The millisecond portion was set to 0.
  • Loading branch information
jdereg committed Oct 13, 2024
1 parent de67d1a commit 899b5dc
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ Both of these features ensure that our library can be seamlessly integrated into
To include in your project:
##### Gradle
```groovy
implementation 'com.cedarsoftware:java-util:2.16.0'
implementation 'com.cedarsoftware:java-util:2.17.0'
```

##### Maven
```xml
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>2.16.0</version>
<version>2.17.0</version>
</dependency>
```
---
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
### Revision History
#### 2.17.0
> * `ClassUtilities.getClassLoader()` added. This will safely return the correct class loader when running in OSGi, JPMS, or neither.
> * `ArrayUtilities.createArray()` added. This method accepts a variable number of arguments and returns them as an array of type `T[].`
> * Fixed bug when converting `Map` containing "time" key (and no `date` nor `zone` keys) with value to `java.sql.Date.` The millisecond portion was set to 0.
#### 2.16.0
> * `SealableMap, LRUCache,` and `TTLCache` updated to use `ConcurrentHashMapNullSafe` internally, to simplify their implementation, as they no longer have to implement the null-safe work, `ConcurrentHashMapNullSafe` does that for them.
> * Added `ConcurrentNavigableMapNullSafe` and `ConcurrentNavigableSetNullSafe`
Expand Down
36 changes: 32 additions & 4 deletions 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.16.0</version>
<version>2.17.0</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down Expand Up @@ -36,8 +36,8 @@
<version.junit-jupiter-api>5.11.1</version.junit-jupiter-api>
<version.junit-jupiter-params>5.11.1</version.junit-jupiter-params>
<version.mockito-junit-jupiter>4.11.0</version.mockito-junit-jupiter>
<version.assertj-core>3.26.3</version.assertj-core>
<version.json-io>4.26.0</version.json-io>
<version.assertj-core>3.24.2</version.assertj-core>
<version.json-io>4.28.0</version.json-io>
<version.agrona>1.22.0</version.agrona> <!-- do not increase past 1.22.0 until abandoning JDK 8 support, class file format 52 -->

<!-- Build maven-***-plugins -->
Expand All @@ -57,6 +57,33 @@
</properties>

<profiles>

<profile>
<id>jdk9-and-above</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
<!-- Remove source and target to prevent conflicts -->
<maven.compiler.source></maven.compiler.source>
<maven.compiler.target></maven.compiler.target>
</properties>
</profile>

<!-- Profile for JDK8 -->
<profile>
<id>jdk8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Do not set release for JDK8 -->
<maven.compiler.release></maven.compiler.release>
</properties>
</profile>

<profile>
<id>release-sign-artifacts</id>
Expand Down Expand Up @@ -184,9 +211,10 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.maven-compiler-plugin}</version>
<configuration>
<release>${maven.compiler.release}</release>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<release>${maven.compiler.release}</release>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/cedarsoftware/util/ArrayUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,44 @@ public static <T> T[] shallowCopy(final T[] array)
return array.clone();
}

/**
* Creates and returns an array containing the provided elements.
*
* <p>This method accepts a variable number of arguments and returns them as an array of type {@code T[]}.
* It is primarily used to facilitate array creation in generic contexts, where type inference is necessary.
*
* <p><strong>Example Usage:</strong>
* <pre>{@code
* String[] stringArray = createArray("Apple", "Banana", "Cherry");
* Integer[] integerArray = createArray(1, 2, 3, 4);
* Person[] personArray = createArray(new Person("Alice"), new Person("Bob"));
* }</pre>
*
* <p><strong>Important Considerations:</strong>
* <ul>
* <li><strong>Type Safety:</strong> Due to type erasure in Java generics, this method does not perform any type checks
* beyond what is already enforced by the compiler. Ensure that all elements are of the expected type {@code T} to avoid
* {@code ClassCastException} at runtime.</li>
* <li><strong>Heap Pollution:</strong> The method is annotated with {@link SafeVarargs} to suppress warnings related to heap
* pollution when using generics with varargs. It is safe to use because the method does not perform any unsafe operations
* on the varargs parameter.</li>
* <li><strong>Null Elements:</strong> The method does not explicitly handle {@code null} elements. If {@code null} values
* are passed, they will be included in the returned array.</li>
* <li><strong>Immutable Arrays:</strong> The returned array is mutable. To create an immutable array, consider wrapping it
* using {@link java.util.Collections#unmodifiableList(List)} or using third-party libraries like Guava's
* {@link com.google.common.collect.ImmutableList}.</li>
* </ul>
*
* @param <T> the component type of the array
* @param elements the elements to be stored in the array
* @return an array containing the provided elements
* @throws NullPointerException if the {@code elements} array is {@code null}
*/
@SafeVarargs
public static <T> T[] createArray(T... elements) {
return elements;
}

/**
* <p>Adds all the elements of the given arrays into a new array.
* </p>
Expand Down
Loading

0 comments on commit 899b5dc

Please sign in to comment.