From 78480ab766bbbe637946037d02579165e7fa7a0e Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 23:34:39 +0200 Subject: [PATCH] Remove a homemade implementation of Pair Since apache-commons is already implementing a Pair type, there is no need to reinvent the wheel. --- .../player/controller/HLSController.java | 14 ++-- .../player/service/PlaylistService.java | 8 +-- .../DefaultPlaylistImportHandler.java | 4 +- .../playlist/PlaylistImportHandler.java | 2 +- .../playlist/XspfPlaylistImportHandler.java | 4 +- .../sonos/SonosServiceRegistration.java | 36 +++++----- .../java/org/airsonic/player/util/Pair.java | 69 ------------------- .../controller/HLSControllerTestCase.java | 12 ++-- 8 files changed, 40 insertions(+), 109 deletions(-) delete mode 100644 airsonic-main/src/main/java/org/airsonic/player/util/Pair.java diff --git a/airsonic-main/src/main/java/org/airsonic/player/controller/HLSController.java b/airsonic-main/src/main/java/org/airsonic/player/controller/HLSController.java index add23b51..c70f10a8 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/controller/HLSController.java +++ b/airsonic-main/src/main/java/org/airsonic/player/controller/HLSController.java @@ -25,9 +25,9 @@ import org.airsonic.player.service.JWTSecurityService; import org.airsonic.player.service.MediaFileService; import org.airsonic.player.service.PlayerService; import org.airsonic.player.service.SecurityService; -import org.airsonic.player.util.Pair; import org.airsonic.player.util.StringUtil; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestUtils; @@ -128,11 +128,11 @@ public class HLSController { } int kbps = Integer.parseInt(matcher.group(1)); if (matcher.group(3) == null) { - return new Pair(kbps, null); + return Pair.of(kbps, null); } else { int width = Integer.parseInt(matcher.group(3)); int height = Integer.parseInt(matcher.group(4)); - return new Pair(kbps, new Dimension(width, height)); + return Pair.of(kbps, new Dimension(width, height)); } } @@ -143,7 +143,7 @@ public class HLSController { String contextPath = getContextPath(request); for (Pair bitRate : bitRates) { - Integer kbps = bitRate.getFirst(); + Integer kbps = bitRate.getLeft(); writer.println("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=" + kbps * 1000L); UriComponentsBuilder url = (UriComponentsBuilder.fromUriString(contextPath + "ext/hls/hls.m3u8") .queryParam("id", id) @@ -151,7 +151,7 @@ public class HLSController { .queryParam("bitRate", kbps)); jwtSecurityService.addJWTToken(url); writer.print(url.toUriString()); - Dimension dimension = bitRate.getSecond(); + Dimension dimension = bitRate.getRight(); if (dimension != null) { writer.print("@" + dimension.width + "x" + dimension.height); } @@ -188,8 +188,8 @@ public class HLSController { builder.queryParam("player", player.getId()); builder.queryParam("duration", duration); if (bitRate != null) { - builder.queryParam("maxBitRate", bitRate.getFirst()); - Dimension dimension = bitRate.getSecond(); + builder.queryParam("maxBitRate", bitRate.getLeft()); + Dimension dimension = bitRate.getRight(); if (dimension != null) { builder.queryParam("size", dimension.width); builder.queryParam("x", dimension.height); diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/PlaylistService.java b/airsonic-main/src/main/java/org/airsonic/player/service/PlaylistService.java index 1b81d13e..8b470ecf 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/PlaylistService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/PlaylistService.java @@ -31,9 +31,9 @@ import org.airsonic.player.domain.User; import org.airsonic.player.service.playlist.PlaylistExportHandler; import org.airsonic.player.service.playlist.PlaylistImportHandler; import org.airsonic.player.util.FileUtil; -import org.airsonic.player.util.Pair; import org.airsonic.player.util.StringUtil; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -192,11 +192,11 @@ public class PlaylistService { Pair, List> result = importHandler.handle(inputSpecificPlaylist); - if (result.getFirst().isEmpty() && !result.getSecond().isEmpty()) { + if (result.getLeft().isEmpty() && !result.getRight().isEmpty()) { throw new Exception("No songs in the playlist were found."); } - for (String error : result.getSecond()) { + for (String error : result.getRight()) { LOG.warn("File in playlist '" + fileName + "' not found: " + error); } Date now = new Date(); @@ -215,7 +215,7 @@ public class PlaylistService { playlist = existingPlaylist; } - setFilesInPlaylist(playlist.getId(), result.getFirst()); + setFilesInPlaylist(playlist.getId(), result.getLeft()); return playlist; } diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/playlist/DefaultPlaylistImportHandler.java b/airsonic-main/src/main/java/org/airsonic/player/service/playlist/DefaultPlaylistImportHandler.java index c34555b5..cc56cca9 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/playlist/DefaultPlaylistImportHandler.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/playlist/DefaultPlaylistImportHandler.java @@ -3,7 +3,7 @@ package org.airsonic.player.service.playlist; import chameleon.playlist.*; import org.airsonic.player.domain.MediaFile; import org.airsonic.player.service.MediaFileService; -import org.airsonic.player.util.Pair; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; @@ -87,7 +87,7 @@ public class DefaultPlaylistImportHandler implements PlaylistImportHandler { errors.add(e.getMessage()); } - return Pair.create(mediaFiles, errors); + return Pair.of(mediaFiles, errors); } @Override diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/playlist/PlaylistImportHandler.java b/airsonic-main/src/main/java/org/airsonic/player/service/playlist/PlaylistImportHandler.java index 3d0d0df9..0cff2ea2 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/playlist/PlaylistImportHandler.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/playlist/PlaylistImportHandler.java @@ -2,7 +2,7 @@ package org.airsonic.player.service.playlist; import chameleon.playlist.SpecificPlaylist; import org.airsonic.player.domain.MediaFile; -import org.airsonic.player.util.Pair; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.core.Ordered; import java.util.List; diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/playlist/XspfPlaylistImportHandler.java b/airsonic-main/src/main/java/org/airsonic/player/service/playlist/XspfPlaylistImportHandler.java index 6746de01..a11d8a43 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/playlist/XspfPlaylistImportHandler.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/playlist/XspfPlaylistImportHandler.java @@ -6,7 +6,7 @@ import chameleon.playlist.xspf.Playlist; import chameleon.playlist.xspf.StringContainer; import org.airsonic.player.domain.MediaFile; import org.airsonic.player.service.MediaFileService; -import org.airsonic.player.util.Pair; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -60,7 +60,7 @@ public class XspfPlaylistImportHandler implements PlaylistImportHandler { errors.add(errorMsg); } }); - return Pair.create(mediaFiles, errors); + return Pair.of(mediaFiles, errors); } @Override diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/sonos/SonosServiceRegistration.java b/airsonic-main/src/main/java/org/airsonic/player/service/sonos/SonosServiceRegistration.java index d83b1805..00c4ba7a 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/sonos/SonosServiceRegistration.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/sonos/SonosServiceRegistration.java @@ -19,8 +19,8 @@ package org.airsonic.player.service.sonos; -import org.airsonic.player.util.Pair; import org.airsonic.player.util.StringUtil; +import org.apache.commons.lang3.tuple.Pair; import org.apache.http.NameValuePair; import org.apache.http.client.ResponseHandler; import org.apache.http.client.config.RequestConfig; @@ -54,23 +54,23 @@ public class SonosServiceRegistration { ", SID " + sonosServiceId + ", and Airsonic URL " + localUrl); List> params = new ArrayList>(); - params.add(Pair.create("sid", String.valueOf(sonosServiceId))); + params.add(Pair.of("sid", String.valueOf(sonosServiceId))); if (enabled) { - params.add(Pair.create("name", sonosServiceName)); - params.add(Pair.create("uri", localUrl)); - params.add(Pair.create("secureUri", localUrl)); - params.add(Pair.create("pollInterval", "1200")); - params.add(Pair.create("authType", "UserId")); - params.add(Pair.create("containerType", "MService")); - params.add(Pair.create("caps", "search")); - params.add(Pair.create("caps", "trFavorites")); - params.add(Pair.create("caps", "alFavorites")); - params.add(Pair.create("caps", "ucPlaylists")); - params.add(Pair.create("caps", "extendedMD")); - params.add(Pair.create("presentationMapVersion", "1")); - params.add(Pair.create("presentationMapUri", airsonicBaseUrl + "sonos/presentationMap.xml")); - params.add(Pair.create("stringsVersion", "5")); - params.add(Pair.create("stringsUri", airsonicBaseUrl + "sonos/strings.xml")); + params.add(Pair.of("name", sonosServiceName)); + params.add(Pair.of("uri", localUrl)); + params.add(Pair.of("secureUri", localUrl)); + params.add(Pair.of("pollInterval", "1200")); + params.add(Pair.of("authType", "UserId")); + params.add(Pair.of("containerType", "MService")); + params.add(Pair.of("caps", "search")); + params.add(Pair.of("caps", "trFavorites")); + params.add(Pair.of("caps", "alFavorites")); + params.add(Pair.of("caps", "ucPlaylists")); + params.add(Pair.of("caps", "extendedMD")); + params.add(Pair.of("presentationMapVersion", "1")); + params.add(Pair.of("presentationMapUri", airsonicBaseUrl + "sonos/presentationMap.xml")); + params.add(Pair.of("stringsVersion", "5")); + params.add(Pair.of("stringsUri", airsonicBaseUrl + "sonos/strings.xml")); } String result = execute(controllerUrl, params); @@ -80,7 +80,7 @@ public class SonosServiceRegistration { private String execute(String url, List> parameters) throws IOException { List params = new ArrayList(); for (Pair parameter : parameters) { - params.add(new BasicNameValuePair(parameter.getFirst(), parameter.getSecond())); + params.add(new BasicNameValuePair(parameter.getLeft(), parameter.getRight())); } RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(20 * 1000) // 20 seconds diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/Pair.java b/airsonic-main/src/main/java/org/airsonic/player/util/Pair.java deleted file mode 100644 index e6e58603..00000000 --- a/airsonic-main/src/main/java/org/airsonic/player/util/Pair.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - This file is part of Airsonic. - - Airsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Airsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Airsonic. If not, see . - - Copyright 2016 (C) Airsonic Authors - Based upon Subsonic, Copyright 2009 (C) Sindre Mehus - */ -package org.airsonic.player.util; - -import org.apache.commons.lang.ObjectUtils; - -import java.io.Serializable; - -/** - * @author Sindre Mehus - */ -public class Pair implements Serializable { - - private final S first; - private final T second; - - public static Pair create(S first, T second) { - return new Pair(first, second); - } - - public Pair(S first, T second) { - this.first = first; - this.second = second; - } - - public S getFirst() { - return first; - } - - public T getSecond() { - return second; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Pair pair = (Pair) o; - - return ObjectUtils.equals(first, pair.first) && ObjectUtils.equals(second, pair.second); - } - - @Override - public int hashCode() { - return ObjectUtils.hashCode(first) * ObjectUtils.hashCode(second); - } -} diff --git a/airsonic-main/src/test/java/org/airsonic/player/controller/HLSControllerTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/controller/HLSControllerTestCase.java index ab0550a3..77c5f33d 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/controller/HLSControllerTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/controller/HLSControllerTestCase.java @@ -20,7 +20,7 @@ package org.airsonic.player.controller; import junit.framework.TestCase; -import org.airsonic.player.util.Pair; +import org.apache.commons.lang3.tuple.Pair; import java.awt.*; @@ -34,13 +34,13 @@ public class HLSControllerTestCase extends TestCase { HLSController controller = new HLSController(); Pair pair = controller.parseBitRate("1000"); - assertEquals(1000, pair.getFirst().intValue()); - assertNull(pair.getSecond()); + assertEquals(1000, pair.getLeft().intValue()); + assertNull(pair.getRight()); pair = controller.parseBitRate("1000@400x300"); - assertEquals(1000, pair.getFirst().intValue()); - assertEquals(400, pair.getSecond().width); - assertEquals(300, pair.getSecond().height); + assertEquals(1000, pair.getLeft().intValue()); + assertEquals(400, pair.getRight().width); + assertEquals(300, pair.getRight().height); try { controller.parseBitRate("asdfl");