Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enturno] Remove org.apache.commons #14406

Merged
merged 14 commits into from
Oct 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.openhab.binding.enturno.internal.connection.EnturCommunicationException;
import org.openhab.binding.enturno.internal.connection.EnturConfigurationException;
import org.openhab.binding.enturno.internal.connection.EnturNoConnection;
import org.openhab.binding.enturno.internal.model.simplified.DisplayData;
import org.openhab.binding.enturno.internal.dto.simplified.DisplayData;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.Channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
Expand All @@ -42,10 +41,11 @@
import org.eclipse.jetty.http.HttpHeader;
import org.openhab.binding.enturno.internal.EnturNoConfiguration;
import org.openhab.binding.enturno.internal.EnturNoHandler;
import org.openhab.binding.enturno.internal.model.EnturJsonData;
import org.openhab.binding.enturno.internal.model.estimated.EstimatedCalls;
import org.openhab.binding.enturno.internal.model.simplified.DisplayData;
import org.openhab.binding.enturno.internal.model.stopplace.StopPlace;
import org.openhab.binding.enturno.internal.dto.EnturJsonData;
import org.openhab.binding.enturno.internal.dto.estimated.EstimatedCalls;
import org.openhab.binding.enturno.internal.dto.simplified.DisplayData;
import org.openhab.binding.enturno.internal.dto.stopplace.StopPlace;
import org.openhab.binding.enturno.internal.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -189,10 +189,8 @@ private String getRequestBody(Map<String, String> params) throws IOException {
}

private List<DisplayData> processData(StopPlace stopPlace, String lineCode) {
Map<String, List<EstimatedCalls>> departures = stopPlace.estimatedCalls.stream()
.filter(call -> StringUtils.equalsIgnoreCase(
StringUtils.trimToEmpty(call.serviceJourney.journeyPattern.line.publicCode),
StringUtils.trimToEmpty(lineCode)))
Map<String, List<EstimatedCalls>> departures = stopPlace.estimatedCalls.stream().filter(
call -> call.serviceJourney.journeyPattern.line.publicCode.strip().equalsIgnoreCase(lineCode.strip()))
.collect(groupingBy(call -> call.quay.id));

List<DisplayData> processedData = new ArrayList<>();
Expand All @@ -214,8 +212,8 @@ private DisplayData getDisplayData(StopPlace stopPlace, Map<String, List<Estimat
List<String> keys = new ArrayList<>(departures.keySet());
DisplayData processedData = new DisplayData();
List<EstimatedCalls> quayCalls = departures.get(keys.get(quayIndex));
List<String> departureTimes = quayCalls.stream().map(eq -> eq.expectedDepartureTime).map(this::getIsoDateTime)
.collect(Collectors.toList());
List<String> departureTimes = quayCalls.stream().map(eq -> eq.expectedDepartureTime)
.map(DateUtil::getIsoDateTime).collect(Collectors.toList());

List<String> estimatedFlags = quayCalls.stream().map(es -> es.realtime).collect(Collectors.toList());

Expand All @@ -233,13 +231,4 @@ private DisplayData getDisplayData(StopPlace stopPlace, Map<String, List<Estimat
processedData.transportMode = stopPlace.transportMode;
return processedData;
}

private String getIsoDateTime(String dateTimeWithoutColonInZone) {
String dateTime = StringUtils.substringBeforeLast(dateTimeWithoutColonInZone, "+");
String offset = StringUtils.substringAfterLast(dateTimeWithoutColonInZone, "+");

StringBuilder builder = new StringBuilder();
return builder.append(dateTime).append("+").append(StringUtils.substring(offset, 0, 2)).append(":00")
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model;
package org.openhab.binding.enturno.internal.dto;

import org.openhab.binding.enturno.internal.model.stopplace.StopPlace;
import org.openhab.binding.enturno.internal.dto.stopplace.StopPlace;

/**
* Generated Plain Old Java Objects class for {@link Data} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model;
package org.openhab.binding.enturno.internal.dto;

/**
* Generated Plain Old Java Objects class for {@link Data} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model;
package org.openhab.binding.enturno.internal.dto;

/**
* {@link EnturJsonData} is a root level class to holding reference of data generated from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.estimated;
package org.openhab.binding.enturno.internal.dto.estimated;

/**
* Generated Plain Old Java Objects class for {@link DestinationDisplay} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.estimated;
package org.openhab.binding.enturno.internal.dto.estimated;

/**
* Generated Plain Old Java Objects class for {@link EstimatedCalls} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.estimated;
package org.openhab.binding.enturno.internal.dto.estimated;

/**
* Generated Plain Old Java Objects class for {@link JourneyPattern} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.estimated;
package org.openhab.binding.enturno.internal.dto.estimated;

/**
* Generated Plain Old Java Objects class for {@link Line} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.estimated;
package org.openhab.binding.enturno.internal.dto.estimated;

/**
* Generated Plain Old Java Objects class for {@link Quay} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.estimated;
package org.openhab.binding.enturno.internal.dto.estimated;

/**
* Generated Plain Old Java Objects class for {@link ServiceJourney} from JSON.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.simplified;
package org.openhab.binding.enturno.internal.dto.simplified;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.model.stopplace;
package org.openhab.binding.enturno.internal.dto.stopplace;

import org.openhab.binding.enturno.internal.model.estimated.EstimatedCalls;
import org.openhab.binding.enturno.internal.dto.estimated.EstimatedCalls;

/**
* Generated Plain Old Java Objects class for {@link StopPlace} from JSON.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.util;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* EnturNo date utility methods.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DateUtil {
/**
* Converts a zoned date time string that lacks a colon in the zone to an ISO-8601 formatted string.
*
* @param dateTimeWithoutColonInZone
* @return ISO-8601 formatted string
*/
public static String getIsoDateTime(String dateTimeWithoutColonInZone) {
ZonedDateTime zonedDateTime = null;
try {
zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone);
} catch (DateTimeParseException e) {
// Skip
}
try {
zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone.replaceAll("(\\d{2})(\\d{2})$", "$1:$2"));
} catch (DateTimeParseException e) {
// Skip
}
if (zonedDateTime != null) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime);
}
return dateTimeWithoutColonInZone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.util;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests for {@link DateUtil}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DateUtilTest {
@ParameterizedTest
@MethodSource("provideTestCasesForGetIsoDateTime")
void getIsoDateTime(String value, String expected) {
assertThat(DateUtil.getIsoDateTime(value), is(expected));
}

private static Stream<Arguments> provideTestCasesForGetIsoDateTime() {
return Stream.of( //
Arguments.of("2023-10-25T09:01:00+0200", "2023-10-25T09:01:00+02:00"),
Arguments.of("2023-10-25T09:01:00+02:00", "2023-10-25T09:01:00+02:00"),
Arguments.of("2023-10-25T09:01:00-0300", "2023-10-25T09:01:00-03:00"),
Arguments.of("2023-10-25T09:01:00+02:30", "2023-10-25T09:01:00+02:30"),
Arguments.of("2023-10-25T09:01:00", "2023-10-25T09:01:00"));
}
}