This library answers the question "How to convert X
to Y
?" How to convert String
to Long
? How to convert Date
to LocalDate
? How to convert array
to stream
? How to convert List
to Map
?
Most of the time we convert data from X
type to Y
. soyuz-to
simplifies your life:
- Simple way to convert
X
toY
- Simple way to init / convert
Map
,List
,Set
- + a little bit functional programming...
Basic rule: to.$requredType($sourceValue)
. E.g. we want to convert java.util.Date
to java.time.LocalDate
:
Date publishedAt = getPublishedAt();
LocalDate publishDate = to.localDate(publishedAt);
Integer integer = to.Integer("5"); //5.7m views - https://stackoverflow.com/q/5585779/716027
Boolean aBoolean = to.Boolean("true"); //535k views - https://stackoverflow.com/q/1538755/716027
LocalDate localDate = to.localDate(new Date()); //374k views- https://stackoverflow.com/q/21242110/716027
Date date = to.date(LocalDate.now()); //322k views - https://stackoverflow.com/q/22929237/716027
Stream<String> stream = to.stream(new String[]{"1", "2"}); //59k views - https://stackoverflow.com/q/27888429/716027
Use simple transformation functions to transform Collections
/ Maps
:
List<Car> cars = to.list(new Car(1, "bmw"), new Car(2, "lada"));
Set<String> carTitles = to.set(cars, Car::getTitle); //492k views https://stackoverflow.com/q/1429860/716027
Map<Integer, Car> carsById = to.map(cars, Car::getId); //356k views https://stackoverflow.com/q/4138364/716027
List<String> carTitlesFromMap = to.list(carsById, (k, v) -> v.getTitle()); //662k views https://stackoverflow.com/q/1026723/716027
@AllArgsConstructor
@Getter
@EqualsAndHashCode
private static class Car {
private int id;
private String title;
}
Simple way to objects:
List<Integer> numbers = to.list(1, 2, 3); //1.2m views https://stackoverflow.com/q/13395114/716027
Set<String> strings = to.set("a", "b", "a"); //714k views https://stackoverflow.com/q/2041778/716027
Map<String, Integer> map = to.map("bmw", 4, "audi", 3, "lada", 5); //1.1m + 800k views https://stackoverflow.com/q/6802483/716027 + https://stackoverflow.com/q/507602/716027
Use to.orDefault
to return values if it's not null
or default value (0
, ""
...) otherwise
Map<String, Integer> carsCount = to.map("bmw", 3, "audi", 4);
int ladas = to.orDefault(carsCount.get("lada"));
<dependency>
<groupId>io.thedocs</groupId>
<artifactId>soyuz-to</artifactId>
<version>1.0.1</version>
</dependency>
repositories {
mavenCentral()
}
dependencies {
compile 'io.thedocs:soyuz-to:1.0.1'
}
import io.thedocs.soyuz.to;
This library depends on slf4j-api
and com.google.code.findbugs:jsr305
(javax.annotation.Nullable
)
- soyuz-is - java thruthy checks (don't use != null check in your if statements)
- soyuz-loge - slf4j wrapper which makes your logs cleaner and easier to read / find
- soyuz-validator - bean fluent validator based on builder pattern
MIT