Skip to content

Commit a86fcd3

Browse files
committed
Add missing comments
- Remove redundant Javadoc @SInCE tags - Use final - Inline one-time use local variables - Reduce vertical whitespace
1 parent e3c46da commit a86fcd3

File tree

7 files changed

+8
-69
lines changed

7 files changed

+8
-69
lines changed

src/main/java/org/apache/commons/beanutils2/converters/AbstractConverter.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,37 +144,29 @@ public <R> R convert(final Class<R> type, Object value) {
144144
if (type == null) {
145145
return convertToDefaultType(value);
146146
}
147-
148147
Class<?> sourceType = value == null ? null : value.getClass();
149148
final Class<R> targetType = ConvertUtils.primitiveToWrapper(type);
150-
151149
if (log().isDebugEnabled()) {
152150
log().debug(
153151
"Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'");
154152
}
155-
156153
value = convertArray(value);
157-
158154
// Missing Value
159155
if (value == null) {
160156
return handleMissing(targetType);
161157
}
162-
163158
sourceType = value.getClass();
164-
165159
try {
166160
// Convert --> String
167161
if (targetType.equals(String.class)) {
168162
return targetType.cast(convertToString(value));
169-
170163
// No conversion necessary
171164
}
172165
if (targetType.equals(sourceType)) {
173166
if (log().isDebugEnabled()) {
174167
log().debug(" No conversion required, value is already a " + toString(targetType));
175168
}
176169
return targetType.cast(value);
177-
178170
// Convert --> Type
179171
}
180172
final Object result = convertToType(targetType, value);
@@ -346,7 +338,6 @@ protected <T> T handleMissing(final Class<T> type) {
346338
// value is now either null or of the desired target type
347339
return type.cast(value);
348340
}
349-
350341
final ConversionException cex = ConversionException.format("No value specified for '%s'", toString(type));
351342
if (log().isDebugEnabled()) {
352343
log().debug(" Throwing ConversionException: " + cex.getMessage());

src/main/java/org/apache/commons/beanutils2/converters/CharacterConverter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,11 @@ protected String convertToString(final Object value) {
8080
protected <T> T convertToType(final Class<T> type, final Object value) throws Exception {
8181
if (Character.class.equals(type) || Character.TYPE.equals(type)) {
8282
final String stringValue = toString(value);
83-
8483
if (stringValue.isEmpty()) {
8584
throw new IllegalArgumentException("Value must not be empty");
8685
}
87-
8886
return type.cast(Character.valueOf(stringValue.charAt(0)));
8987
}
90-
9188
throw conversionException(type, value);
9289
}
9390

src/main/java/org/apache/commons/beanutils2/converters/DateTimeConverter.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ protected String convertToString(final Object value) {
139139
} else if (value instanceof Instant) {
140140
date = Date.from((Instant) value);
141141
}
142-
143142
String result = null;
144143
if (useLocaleFormat && date != null) {
145144
DateFormat format = null;
@@ -195,73 +194,60 @@ protected String convertToString(final Object value) {
195194
@Override
196195
protected <T> T convertToType(final Class<T> targetType, final Object value) throws Exception {
197196
final Class<?> sourceType = value.getClass();
198-
199197
// Handle java.sql.Timestamp
200198
if (value instanceof java.sql.Timestamp) {
201-
202199
// Prior to JDK 1.4 the Timestamp's getTime() method
203200
// didn't include the milliseconds. The following code
204201
// ensures it works consistently across JDK versions
205202
final java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
206203
long timeInMillis = timestamp.getTime() / 1000 * 1000;
207204
timeInMillis += timestamp.getNanos() / 1000000;
208-
209205
return toDate(targetType, timeInMillis);
210206
}
211-
212207
// Handle Date (includes java.sql.Date & java.sql.Time)
213208
if (value instanceof Date) {
214209
final Date date = (Date) value;
215210
return toDate(targetType, date.getTime());
216211
}
217-
218212
// Handle Calendar
219213
if (value instanceof Calendar) {
220214
final Calendar calendar = (Calendar) value;
221215
return toDate(targetType, calendar.getTime().getTime());
222216
}
223-
224217
// Handle Long
225218
if (value instanceof Long) {
226219
final Long longObj = (Long) value;
227220
return toDate(targetType, longObj.longValue());
228221
}
229-
230222
// Handle LocalDate
231223
if (value instanceof LocalDate) {
232224
final LocalDate date = (LocalDate) value;
233225
return toDate(targetType, date.atStartOfDay(getZoneId()).toInstant().toEpochMilli());
234226
}
235-
236227
// Handle LocalDateTime
237228
if (value instanceof LocalDateTime) {
238229
final LocalDateTime date = (LocalDateTime) value;
239230
return toDate(targetType, date.atZone(getZoneId()).toInstant().toEpochMilli());
240231
}
241-
242232
// Handle ZonedDateTime
243233
if (value instanceof ZonedDateTime) {
244234
final ZonedDateTime date = (ZonedDateTime) value;
245235
return toDate(targetType, date.toInstant().toEpochMilli());
246236
}
247-
248237
// Handle OffsetDateTime
249238
if (value instanceof OffsetDateTime) {
250239
final OffsetDateTime date = (OffsetDateTime) value;
251240
return toDate(targetType, date.toInstant().toEpochMilli());
252241
}
253-
254242
if (value instanceof Instant) {
255243
final Instant date = (Instant) value;
256244
return toDate(targetType, date.toEpochMilli());
257245
}
258-
259246
// Convert all other types to String & handle
260247
final String stringValue = toTrim(value);
261248
if (stringValue.isEmpty()) {
262249
return handleMissing(targetType);
263250
}
264-
265251
// Parse the Date/Time
266252
if (useLocaleFormat) {
267253
Calendar calendar = null;
@@ -276,7 +262,6 @@ protected <T> T convertToType(final Class<T> targetType, final Object value) thr
276262
}
277263
return toDate(targetType, calendar.getTime().getTime());
278264
}
279-
280265
// Default String conversion
281266
return toDate(targetType, stringValue);
282267
}
@@ -524,50 +509,42 @@ private <T> T toDate(final Class<T> type, final long value) {
524509
if (type.equals(Date.class)) {
525510
return type.cast(new Date(value));
526511
}
527-
528512
// java.sql.Date
529513
if (type.equals(java.sql.Date.class)) {
530514
return type.cast(new java.sql.Date(value));
531515
}
532-
533516
// java.sql.Time
534517
if (type.equals(java.sql.Time.class)) {
535518
return type.cast(new java.sql.Time(value));
536519
}
537-
538520
// java.sql.Timestamp
539521
if (type.equals(java.sql.Timestamp.class)) {
540522
return type.cast(new java.sql.Timestamp(value));
541523
}
542-
543524
// java.time.LocalDateTime
544525
if (type.equals(LocalDate.class)) {
545526
final LocalDate localDate = Instant.ofEpochMilli(value).atZone(getZoneId()).toLocalDate();
546527
return type.cast(localDate);
547528
}
548-
549529
// java.time.LocalDateTime
550530
if (type.equals(LocalDateTime.class)) {
551531
final LocalDateTime localDateTime = Instant.ofEpochMilli(value).atZone(getZoneId()).toLocalDateTime();
552532
return type.cast(localDateTime);
553533
}
554-
555534
// java.time.ZonedDateTime
556535
if (type.equals(ZonedDateTime.class)) {
557536
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), getZoneId());
558537
return type.cast(zonedDateTime);
559538
}
560-
561539
// java.time.OffsetDateTime
562540
if (type.equals(OffsetDateTime.class)) {
563541
final OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(value), getZoneId());
564542
return type.cast(offsetDateTime);
565543
}
566-
544+
// java.time.Instant
567545
if (type.equals(Instant.class)) {
568546
return type.cast(Instant.ofEpochMilli(value));
569547
}
570-
571548
// java.util.Calendar
572549
if (type.equals(Calendar.class)) {
573550
Calendar calendar = null;
@@ -584,7 +561,6 @@ private <T> T toDate(final Class<T> type, final long value) {
584561
calendar.setLenient(false);
585562
return type.cast(calendar);
586563
}
587-
588564
final String msg = toString(getClass()) + " cannot handle conversion to '" + toString(type) + "'";
589565
if (log().isWarnEnabled()) {
590566
log().warn(" " + msg);
@@ -619,7 +595,6 @@ private <T> T toDate(final Class<T> type, final String value) {
619595
throw new ConversionException("String must be in JDBC format [yyyy-MM-dd] to create a java.sql.Date");
620596
}
621597
}
622-
623598
// java.sql.Time
624599
if (type.equals(java.sql.Time.class)) {
625600
try {
@@ -628,7 +603,6 @@ private <T> T toDate(final Class<T> type, final String value) {
628603
throw new ConversionException("String must be in JDBC format [HH:mm:ss] to create a java.sql.Time");
629604
}
630605
}
631-
632606
// java.sql.Timestamp
633607
if (type.equals(java.sql.Timestamp.class)) {
634608
try {
@@ -637,15 +611,14 @@ private <T> T toDate(final Class<T> type, final String value) {
637611
throw new ConversionException("String must be in JDBC format [yyyy-MM-dd HH:mm:ss.fffffffff] to create a java.sql.Timestamp");
638612
}
639613
}
640-
614+
// java.time.Instant
641615
if (type.equals(Instant.class)) {
642616
try {
643617
return type.cast(Instant.parse(value));
644618
} catch (final DateTimeParseException ex) {
645619
throw new ConversionException("String must be in ISO-8601 format to create a java.time.Instant");
646620
}
647621
}
648-
649622
final String msg = toString(getClass()) + " does not support default String to '" + toString(type) + "' conversion.";
650623
if (log().isWarnEnabled()) {
651624
log().warn(" " + msg);

src/main/java/org/apache/commons/beanutils2/converters/DurationConverter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,19 @@ public DurationConverter(final Duration defaultValue) {
5454
* @param value The input value to be converted.
5555
* @return The converted value.
5656
* @throws Throwable if an error occurs converting to the specified type
57-
* @since 2.0
5857
*/
5958
@Override
6059
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
6160
if (Duration.class.equals(type)) {
6261
return type.cast(Duration.parse(toString(value)));
6362
}
64-
6563
throw conversionException(type, value);
6664
}
6765

6866
/**
6967
* Gets the default type this {@code Converter} handles.
7068
*
7169
* @return The default type this {@code Converter} handles.
72-
* @since 2.0
7370
*/
7471
@Override
7572
protected Class<Duration> getDefaultType() {

src/main/java/org/apache/commons/beanutils2/converters/EnumConverter.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,31 @@ public EnumConverter(final Enum<E> defaultValue) {
6060
protected <R> R convertToType(final Class<R> type, final Object value) throws Throwable {
6161
if (Enum.class.isAssignableFrom(type)) {
6262
final String stringValue = toString(value);
63-
6463
try {
6564
return type.cast((Enum) Enum.valueOf((Class) type, stringValue));
66-
} catch (IllegalArgumentException ex) {
65+
} catch (final IllegalArgumentException ex) {
6766
// Continue to check fully qualified name.
6867
}
69-
7068
final int lastHash = stringValue.lastIndexOf('#');
7169
final int lastDot = stringValue.lastIndexOf('.');
72-
7370
if (lastDot == -1 && lastHash == -1) {
74-
throw new IllegalArgumentException(
75-
"Expected fully qualified name for Enum constant like: java.time.DayOfWeek.MONDAY");
71+
throw new IllegalArgumentException("Expected fully qualified name for Enum constant like: java.time.DayOfWeek.MONDAY");
7672
}
77-
7873
final String enumValue = stringValue.substring(Math.max(lastHash, lastDot) + 1);
7974
final String className = stringValue.substring(0, stringValue.length() - enumValue.length() - 1);
80-
8175
try {
82-
Class classForName = Class.forName(className);
83-
76+
final Class classForName = Class.forName(className);
8477
if (!classForName.isEnum()) {
8578
throw new IllegalArgumentException("Value isn't an enumerated type.");
8679
}
87-
8880
if (!type.isAssignableFrom(classForName)) {
8981
throw new IllegalArgumentException("Class is not the required type.");
9082
}
91-
9283
return type.cast((Enum) Enum.valueOf(classForName, enumValue));
93-
} catch (ClassNotFoundException ex) {
84+
} catch (final ClassNotFoundException ex) {
9485
throw new IllegalArgumentException("Class \"" + className + "\" doesn't exist.", ex);
9586
}
9687
}
97-
9888
throw conversionException(type, value);
9989
}
10090

src/test/java/org/apache/commons/beanutils2/converters/CharacterConverterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ void testConvertToCharacterNullNoDefault() {
7676
void testConvertToString() {
7777
@SuppressWarnings("rawtypes")
7878
final Converter raw = converter;
79-
8079
assertEquals("N", raw.convert(String.class, Character.valueOf('N')), "Character Test");
8180
assertEquals("F", raw.convert(String.class, "FOO"), "String Test");
8281
assertEquals("3", raw.convert(String.class, Integer.valueOf(321)), "Integer Test");

src/test/java/org/apache/commons/beanutils2/converters/EnumConverterTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,11 @@ public void tearDown() throws Exception {
6161
@Test
6262
void testSimpleConversion() throws Exception {
6363
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from String", };
64-
6564
final Object[] input = { "DELIVERED", "ORDERED", "READY" };
66-
6765
final PizzaStatus[] expected = { PizzaStatus.DELIVERED, PizzaStatus.ORDERED, PizzaStatus.READY };
68-
6966
for (int i = 0; i < expected.length; i++) {
7067
assertEquals(expected[i], converter.convert(PizzaStatus.class, input[i]), message[i] + " to Enum");
7168
}
72-
7369
for (int i = 0; i < expected.length; i++) {
7470
assertEquals(input[i], converter.convert(String.class, expected[i]), input[i] + " to String");
7571
}
@@ -85,16 +81,12 @@ void testUnsupportedType() {
8581

8682
@Test
8783
void testConvertTimeUnit() {
88-
final TimeUnit expected = TimeUnit.NANOSECONDS;
89-
final Enum actual = converter.convert(Enum.class, "java.util.concurrent.TimeUnit.NANOSECONDS");
90-
assertEquals(expected, actual);
84+
assertEquals(TimeUnit.NANOSECONDS, converter.convert(Enum.class, "java.util.concurrent.TimeUnit.NANOSECONDS"));
9185
}
9286

9387
@Test
9488
void testConvertDayOfWeek() {
95-
final DayOfWeek expected = DayOfWeek.MONDAY;
96-
final DayOfWeek actual = converter.convert(DayOfWeek.class, "java.time.DayOfWeek#MONDAY");
97-
assertEquals(expected, actual);
89+
assertEquals(DayOfWeek.MONDAY, converter.convert(DayOfWeek.class, "java.time.DayOfWeek#MONDAY"));
9890
}
9991

10092
@Test

0 commit comments

Comments
 (0)