Skip to content

Commit

Permalink
Merge pull request #17 from joutvhu/development
Browse files Browse the repository at this point in the history
Convertor service provider
  • Loading branch information
joutvhu authored Jun 29, 2021
2 parents af0f66d + db5ff98 commit ed134b2
Show file tree
Hide file tree
Showing 26 changed files with 231 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Utility to parse String to Date by target type and string format
## Using

```java
Date date = DateParser.instance().parse(Date.class, "2021-06-27 21:52:25.408", "yyyy-MM-dd HH:mm:ss.SSS");
Date date = DateParser.getInstance().parse(Date.class, "2021-06-27 21:52:25.408", "yyyy-MM-dd HH:mm:ss.SSS");

LocalDateTime localDateTime = DateParser.quickParse(LocalDateTime.class, "2021-06-27 21:52:25.408", "yyyy-MM-dd HH:mm:ss.SSS");
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'com.github.joutvhu'
version '1.0.0.1-SNAPSHOT'
version '1.0.0.2-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
39 changes: 11 additions & 28 deletions src/main/java/com/joutvhu/date/parser/DateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @since 1.0.0
*/
public class DateParser {
private static DateParser parser;
private static DateParser instance;

private Locale defaultLocale;
private TimeZone defaultZone;
Expand All @@ -37,31 +37,10 @@ public DateParser() {
*
* @return The date parser.
*/
public static DateParser instance() {
if (parser == null) {
parser = new DateParser()
.with(CalendarConvertor.INSTANCE)
.with(DateConvertor.INSTANCE)
.with(LocalDateConvertor.INSTANCE)
.with(LocalDateTimeConvertor.INSTANCE)
.with(LocalTimeConvertor.INSTANCE)
.with(InstantConvertor.INSTANCE)
.with(SqlDateConvertor.INSTANCE)
.with(SqlTimeConvertor.INSTANCE)
.with(SqlTimestampConvertor.INSTANCE)
.with(DayOfWeekConvertor.INSTANCE)
.with(LongConvertor.INSTANCE)
.with(MonthConvertor.INSTANCE)
.with(MonthDayConvertor.INSTANCE)
.with(OffsetDateTimeConvertor.INSTANCE)
.with(OffsetTimeConvertor.INSTANCE)
.with(TimeZoneConvertor.INSTANCE)
.with(YearConvertor.INSTANCE)
.with(YearMonthConvertor.INSTANCE)
.with(ZonedDateTimeConvertor.INSTANCE)
.with(ZoneOffsetConvertor.INSTANCE);
}
return parser;
public static DateParser getInstance() {
if (instance == null)
instance = new DateParser();
return instance;
}

/**
Expand Down Expand Up @@ -141,6 +120,10 @@ public <T> T convert(Class<T> type, ObjectiveDate objective) {
return convertor.convert(objective);
}

Convertor<T> convertor = ConvertorService.getInstance().getConvertor(type);
if (convertor != null)
return convertor.convert(objective);

throw new ClassCastException("Unsupported " + type.getName() + " class.");
}

Expand Down Expand Up @@ -179,7 +162,7 @@ public <T> T parse(Class<T> type, String value, String... patterns) {
}

/**
* Quick parse with default {@link DateParser#instance()}
* Quick parse with default {@link DateParser#getInstance()}
*
* @param type is the target type class
* @param value is string of date need to parse
Expand All @@ -188,6 +171,6 @@ public <T> T parse(Class<T> type, String value, String... patterns) {
* @return target object
*/
public static <T> T quickParse(Class<T> type, String value, String... patterns) {
return DateParser.instance().parse(type, value, patterns);
return DateParser.getInstance().parse(type, value, patterns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.Objects;

public class CalendarConvertor implements Convertor<Calendar> {
public static final CalendarConvertor INSTANCE = new CalendarConvertor();
private static CalendarConvertor instance;

public static synchronized CalendarConvertor getInstance() {
if (instance == null)
instance = new CalendarConvertor();
return instance;
}

@Override
public Calendar convert(ObjectiveDate objective) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.joutvhu.date.parser.convertor;

import java.util.Iterator;
import java.util.ServiceLoader;

public class ConvertorService {
private static ConvertorService instance;
private final ServiceLoader<Convertor> loader;

public ConvertorService() {
this.loader = ServiceLoader.load(Convertor.class);
}

public static synchronized ConvertorService getInstance() {
if (instance == null)
instance = new ConvertorService();
return instance;
}

public <T> Convertor<T> getConvertor(Class<T> type) {
if (type == null)
return null;
Iterator<Convertor> convertorIterator = this.loader.iterator();
Convertor<T> subtypeConvertor = null;

while (convertorIterator.hasNext()) {
Convertor convertor = convertorIterator.next();
Class<?> targetType = Convertor.typeOfConvertor(convertor);

if (type.equals(targetType))
return convertor;
if (subtypeConvertor == null && type.isAssignableFrom(targetType))
subtypeConvertor = convertor;
}
return subtypeConvertor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import java.util.Date;

public class DateConvertor implements Convertor<Date> {
public static final DateConvertor INSTANCE = new DateConvertor();
private static DateConvertor instance;

public static synchronized DateConvertor getInstance() {
if (instance == null)
instance = new DateConvertor();
return instance;
}

@Override
public Date convert(ObjectiveDate objective) {
return CalendarConvertor.INSTANCE.convert(objective).getTime();
return CalendarConvertor.getInstance().convert(objective).getTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import java.time.LocalDate;

public class DayOfWeekConvertor implements Convertor<DayOfWeek> {
public static final DayOfWeekConvertor INSTANCE = new DayOfWeekConvertor();
private static DayOfWeekConvertor instance;

public static synchronized DayOfWeekConvertor getInstance() {
if (instance == null)
instance = new DayOfWeekConvertor();
return instance;
}

@Override
public DayOfWeek convert(ObjectiveDate objective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import java.time.Instant;

public class InstantConvertor implements Convertor<Instant> {
public static final InstantConvertor INSTANCE = new InstantConvertor();
private static InstantConvertor instance;

public static synchronized InstantConvertor getInstance() {
if (instance == null)
instance = new InstantConvertor();
return instance;
}

@Override
public Instant convert(ObjectiveDate objective) {
Instant instant = CalendarConvertor.INSTANCE.convert(objective).toInstant();
Instant instant = CalendarConvertor.getInstance().convert(objective).toInstant();
int oldNano = instant.getNano();
if (objective.getNano() != null && oldNano != objective.getNano())
instant = instant.minusNanos(oldNano).plusNanos(objective.getNano());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.Objects;

public class LocalDateConvertor implements Convertor<LocalDate> {
public static final LocalDateConvertor INSTANCE = new LocalDateConvertor();
private static LocalDateConvertor instance;

public static synchronized LocalDateConvertor getInstance() {
if (instance == null)
instance = new LocalDateConvertor();
return instance;
}

@Override
public LocalDate convert(ObjectiveDate objective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import java.time.LocalDateTime;

public class LocalDateTimeConvertor implements Convertor<LocalDateTime> {
public static final LocalDateTimeConvertor INSTANCE = new LocalDateTimeConvertor();
private static LocalDateTimeConvertor instance;

public static synchronized LocalDateTimeConvertor getInstance() {
if (instance == null)
instance = new LocalDateTimeConvertor();
return instance;
}

@Override
public LocalDateTime convert(ObjectiveDate objective) {
return LocalDateTime.of(
LocalDateConvertor.INSTANCE.convert(objective),
LocalTimeConvertor.INSTANCE.convert(objective)
LocalDateConvertor.getInstance().convert(objective),
LocalTimeConvertor.getInstance().convert(objective)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.Objects;

public class LocalTimeConvertor implements Convertor<LocalTime> {
public static final LocalTimeConvertor INSTANCE = new LocalTimeConvertor();
private static LocalTimeConvertor instance;

public static synchronized LocalTimeConvertor getInstance() {
if (instance == null)
instance = new LocalTimeConvertor();
return instance;
}

@Override
public LocalTime convert(ObjectiveDate objective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import com.joutvhu.date.parser.domain.ObjectiveDate;

public class LongConvertor implements Convertor<Long> {
public static final LongConvertor INSTANCE = new LongConvertor();
private static LongConvertor instance;

public static synchronized LongConvertor getInstance() {
if (instance == null)
instance = new LongConvertor();
return instance;
}

@Override
public Long convert(ObjectiveDate objective) {
return CalendarConvertor.INSTANCE.convert(objective).getTimeInMillis();
return CalendarConvertor.getInstance().convert(objective).getTimeInMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.Objects;

public class MonthConvertor implements Convertor<Month> {
public static final MonthConvertor INSTANCE = new MonthConvertor();
private static MonthConvertor instance;

public static synchronized MonthConvertor getInstance() {
if (instance == null)
instance = new MonthConvertor();
return instance;
}

@Override
public Month convert(ObjectiveDate objective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.util.Objects;

public class MonthDayConvertor implements Convertor<MonthDay> {
public static final MonthDayConvertor INSTANCE = new MonthDayConvertor();
private static MonthDayConvertor instance;

public static synchronized MonthDayConvertor getInstance() {
if (instance == null)
instance = new MonthDayConvertor();
return instance;
}

@Override
public MonthDay convert(ObjectiveDate objective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import java.time.OffsetDateTime;

public class OffsetDateTimeConvertor implements Convertor<OffsetDateTime> {
public static final OffsetDateTimeConvertor INSTANCE = new OffsetDateTimeConvertor();
private static OffsetDateTimeConvertor instance;

public static synchronized OffsetDateTimeConvertor getInstance() {
if (instance == null)
instance = new OffsetDateTimeConvertor();
return instance;
}

@Override
public OffsetDateTime convert(ObjectiveDate objective) {
return OffsetDateTime.of(
LocalDateTimeConvertor.INSTANCE.convert(objective),
ZoneOffsetConvertor.INSTANCE.convert(objective)
LocalDateTimeConvertor.getInstance().convert(objective),
ZoneOffsetConvertor.getInstance().convert(objective)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import java.time.OffsetTime;

public class OffsetTimeConvertor implements Convertor<OffsetTime> {
public static final OffsetTimeConvertor INSTANCE = new OffsetTimeConvertor();
private static OffsetTimeConvertor instance;

public static synchronized OffsetTimeConvertor getInstance() {
if (instance == null)
instance = new OffsetTimeConvertor();
return instance;
}

@Override
public OffsetTime convert(ObjectiveDate objective) {
return OffsetTime.of(
LocalTimeConvertor.INSTANCE.convert(objective),
ZoneOffsetConvertor.INSTANCE.convert(objective)
LocalTimeConvertor.getInstance().convert(objective),
ZoneOffsetConvertor.getInstance().convert(objective)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import java.sql.Date;

public class SqlDateConvertor implements Convertor<Date> {
public static final SqlDateConvertor INSTANCE = new SqlDateConvertor();
private static SqlDateConvertor instance;

public static synchronized SqlDateConvertor getInstance() {
if (instance == null)
instance = new SqlDateConvertor();
return instance;
}

@Override
public Date convert(ObjectiveDate objective) {
return Date.valueOf(LocalDateConvertor.INSTANCE.convert(objective));
return Date.valueOf(LocalDateConvertor.getInstance().convert(objective));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import java.sql.Time;

public class SqlTimeConvertor implements Convertor<Time> {
public static final SqlTimeConvertor INSTANCE = new SqlTimeConvertor();
private static SqlTimeConvertor instance;

public static synchronized SqlTimeConvertor getInstance() {
if (instance == null)
instance = new SqlTimeConvertor();
return instance;
}

@Override
public Time convert(ObjectiveDate objective) {
return Time.valueOf(LocalTimeConvertor.INSTANCE.convert(objective));
return Time.valueOf(LocalTimeConvertor.getInstance().convert(objective));
}
}
Loading

0 comments on commit ed134b2

Please sign in to comment.