Skip to content

Commit 810d65a

Browse files
committed
chore(loader): remove unnecessary if-else logic
1 parent 1916221 commit 810d65a

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

hugegraph-loader/src/main/java/org/apache/hugegraph/loader/util/DataTypeUtil.java

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.hugegraph.loader.source.InputSource;
3333
import org.apache.hugegraph.loader.source.file.FileSource;
3434
import org.apache.hugegraph.loader.source.file.ListFormat;
35-
import org.apache.hugegraph.loader.source.jdbc.JDBCSource;
3635
import org.apache.hugegraph.loader.source.kafka.KafkaSource;
3736
import org.apache.hugegraph.structure.constant.Cardinality;
3837
import org.apache.hugegraph.structure.constant.DataType;
@@ -126,27 +125,31 @@ private static Object parseSingleValue(String key, Object rawValue, DataType dat
126125
InputSource source) {
127126
// Trim space if raw value is string
128127
Object value = rawValue;
129-
if (rawValue instanceof String) {
128+
boolean isString = rawValue instanceof String;
129+
if (isString) {
130130
value = ((String) rawValue).trim();
131131
}
132132
if (dataType.isNumber()) {
133133
return parseNumber(key, value, dataType);
134-
} else if (dataType.isBoolean()) {
135-
return parseBoolean(key, value);
136-
} else if (dataType.isDate()) {
137-
return parseDate(key, source, value);
138-
} else if (dataType.isUUID()) {
139-
return parseUUID(key, value);
140-
} else if (dataType.isText()) {
141-
if (!(rawValue instanceof String)) {
142-
value = rawValue.toString();
143-
}
144134
}
145-
E.checkArgument(checkDataType(key, value, dataType),
146-
"The value(key='%s') '%s'(%s) is not match with " +
147-
"data type %s and can't convert to it",
148-
key, value, value.getClass(), dataType);
149-
135+
switch (dataType) {
136+
case TEXT:
137+
if (!isString) {
138+
value = rawValue.toString();
139+
}
140+
return value;
141+
case BOOLEAN:
142+
return parseBoolean(key, value);
143+
case DATE:
144+
return parseDate(key, source, value);
145+
case UUID:
146+
return parseUUID(key, value);
147+
default:
148+
E.checkArgument(checkDataType(key, value, dataType),
149+
"The value(key='%s') '%s'(%s) is not match with " +
150+
"data type %s and can't convert to it",
151+
key, value, value.getClass(), dataType);
152+
}
150153
return value;
151154
}
152155

@@ -155,47 +158,49 @@ private static Date parseDate(String key, InputSource source, Object value) {
155158
String dateFormat = null;
156159
String timeZone = null;
157160

158-
if (source instanceof KafkaSource) {
159-
KafkaSource kafkaSource = (KafkaSource) source;
160-
extraDateFormats = kafkaSource.getExtraDateFormats();
161-
dateFormat = kafkaSource.getDateFormat();
162-
timeZone = kafkaSource.getTimeZone();
163-
} else if (source instanceof JDBCSource) {
164-
/* Warn: it uses system default timezone,
165-
* should we think a better way to compatible differ timezone people?
166-
*/
167-
long timestamp = 0L;
168-
if (value instanceof Date) {
169-
timestamp = ((Date) value).getTime();
170-
} else if (value instanceof LocalDateTime) {
171-
timestamp = ((LocalDateTime) value).atZone(ZoneId.systemDefault())
172-
.toInstant()
173-
.toEpochMilli();
174-
}
175-
value = new Date(timestamp);
176-
} else if (source instanceof FileSource) {
177-
FileSource fileSource = (FileSource) source;
178-
dateFormat = fileSource.dateFormat();
179-
timeZone = fileSource.timeZone();
180-
} else {
181-
throw new IllegalArgumentException("Date format source " +
182-
source.getClass().getName() + " not supported");
161+
switch (source.type()) {
162+
case KAFKA:
163+
KafkaSource kafkaSource = (KafkaSource) source;
164+
extraDateFormats = kafkaSource.getExtraDateFormats();
165+
dateFormat = kafkaSource.getDateFormat();
166+
timeZone = kafkaSource.getTimeZone();
167+
break;
168+
case JDBC:
169+
/*Warn:it uses system default timezone,
170+
should we thought a better way to compatible differ timezone people?*/
171+
long timestamp = 0L;
172+
if (value instanceof Date) {
173+
timestamp = ((Date) value).getTime();
174+
} else if (value instanceof LocalDateTime) {
175+
timestamp = ((LocalDateTime) value).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
176+
}
177+
value = new Date(timestamp);
178+
break;
179+
case FILE:
180+
FileSource fileSource = (FileSource) source;
181+
dateFormat = fileSource.dateFormat();
182+
timeZone = fileSource.timeZone();
183+
break;
184+
185+
case HDFS:
186+
default:
187+
throw new IllegalArgumentException("Date format source " + source.getClass().getName() + " not supported");
183188
}
184189

185190
if (extraDateFormats == null || extraDateFormats.isEmpty()) {
186191
return parseDate(key, value, dateFormat, timeZone);
187-
} else {
188-
HashSet<String> allDateFormats = new HashSet<>();
189-
allDateFormats.add(dateFormat);
190-
allDateFormats.addAll(extraDateFormats);
191-
int size = allDateFormats.size();
192-
for (String df : allDateFormats) {
193-
try {
194-
return parseDate(key, value, df, timeZone);
195-
} catch (Exception e) {
196-
if (--size <= 0) {
197-
throw e;
198-
}
192+
}
193+
194+
HashSet<String> allDateFormats = new HashSet<>();
195+
allDateFormats.add(dateFormat);
196+
allDateFormats.addAll(extraDateFormats);
197+
int size = allDateFormats.size();
198+
for (String df : allDateFormats) {
199+
try {
200+
return parseDate(key, value, df, timeZone);
201+
} catch (Exception e) {
202+
if (--size <= 0) {
203+
throw e;
199204
}
200205
}
201206
}

0 commit comments

Comments
 (0)