Zero-dependency collection of basic tools to work with Java Object
.
It is never recommended to work with Java Object
objects and cast/convert them in required class.
In most cases if far better to use corresponding classes, beans, POJO's - anything but Object
.
But sometimes there is no other way - and in this case this library can be helpful.
// Constructing source data
HashMap<String, Object> source = new HashMap<>();
source.put("foo", "bar");
source.put("some", "1234567890");
ObjectMap<String> o = ObjectMap.wrap(source);
System.out.println(o.getString("foo"));
System.out.println(o.getString("foo").getClass());
System.out.println(o.getString("some"));
System.out.println(o.getString("some").getClass());
System.out.println(o.getInt("some"));
System.out.println(o.getUnixSecondsInstant("some"));
System.out.println(o.getUnixSecondsInstant("some").getClass());
output:
bar
class java.lang.String
1234567890
class java.lang.String
1234567890
2009-02-13T23:31:30Z
class java.time.Instant
This library introduces two main abstractions:
Mixed
- container, that holds arbitrary data and is capable to convert it into requested class (likeMixed.wrap("12345").getInt()
)ObjectMap<K>
- map-like container that convert its values to requested classes. Pay attention thatObjectMap
does not extend/implementMap
interface.
All implementation of abstractions above are thread safe and immutable.
To help work with data conversion Converter
interface with its implementations can be used
Builds are available on Maven Central
<dependency>
<groupId>io.github.mjcro</groupId>
<artifactId>objects</artifactId>
<version>2.0.2</version>
</dependency>
Latest builds of this repository can be also obtained from JitPack.
Add JitPack repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add dependency:
<dependency>
<groupId>io.github.mjcro</groupId>
<artifactId>objects</artifactId>
<version>2.0.2</version>
</dependency>