This is a first batch of simple modernization of the codebase

I threw airsonic at IntelliJ's IDEA analysis,
and asked it to flag what could be modernized
for Java > 5.

- foreach instead of for…
- I added some null-deref checks
- Integer.ValueOf, since Integer(…) is deprecated
- Contextual try
- Objects.equals instead of handcrafted comparisons
- StringBuilder instead of StringBuffer
- Removal of outdated/wrong javadoc comments
master
jvoisin 6 years ago
parent 143a220719
commit ca489f8220
  1. 4
      airsonic-main/src/main/java/org/airsonic/player/ajax/TagService.java
  2. 5
      airsonic-main/src/main/java/org/airsonic/player/controller/DownloadController.java
  3. 5
      airsonic-main/src/main/java/org/airsonic/player/controller/PlayerSettingsController.java
  4. 6
      airsonic-main/src/main/java/org/airsonic/player/controller/UploadController.java
  5. 3
      airsonic-main/src/main/java/org/airsonic/player/domain/MusicIndex.java
  6. 4
      airsonic-main/src/main/java/org/airsonic/player/domain/PlayQueue.java
  7. 4
      airsonic-main/src/main/java/org/airsonic/player/domain/Transcoding.java
  8. 2
      airsonic-main/src/main/java/org/airsonic/player/domain/User.java
  9. 2
      airsonic-main/src/main/java/org/airsonic/player/io/TranscodeInputStream.java
  10. 2
      airsonic-main/src/main/java/org/airsonic/player/monitor/MetricsManager.java
  11. 4
      airsonic-main/src/main/java/org/airsonic/player/service/MediaFileService.java
  12. 8
      airsonic-main/src/main/java/org/airsonic/player/service/MusicIndexService.java
  13. 10
      airsonic-main/src/main/java/org/airsonic/player/service/PodcastService.java
  14. 4
      airsonic-main/src/main/java/org/airsonic/player/service/SecurityService.java
  15. 4
      airsonic-main/src/main/java/org/airsonic/player/service/metadata/JaudiotaggerParser.java
  16. 2
      airsonic-main/src/main/java/org/airsonic/player/taglib/UrlTag.java
  17. 7
      airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java

@ -73,7 +73,7 @@ public class TagService {
Integer trackNumber = null;
if (track != null) {
try {
trackNumber = new Integer(track);
trackNumber = Integer.valueOf(track);
} catch (NumberFormatException x) {
LOG.warn("Illegal track number: " + track, x);
}
@ -82,7 +82,7 @@ public class TagService {
Integer yearNumber = null;
if (year != null) {
try {
yearNumber = new Integer(year);
yearNumber = Integer.valueOf(year);
} catch (NumberFormatException x) {
LOG.warn("Illegal year: " + year, x);
}

@ -368,9 +368,8 @@ public class DownloadController implements LastModified {
*/
private long computeCrc(File file) throws IOException {
CRC32 crc = new CRC32();
InputStream in = new FileInputStream(file);
try {
try (InputStream in = new FileInputStream(file)) {
byte[] buf = new byte[8192];
int n = in.read(buf);
@ -379,8 +378,6 @@ public class DownloadController implements LastModified {
n = in.read(buf);
}
} finally {
in.close();
}
return crc.getValue();

@ -106,8 +106,9 @@ public class PlayerSettingsController {
command.setAdmin(user.isAdminRole());
command.setJavaJukeboxMixers(Arrays.stream(AudioSystemUtils.listAllMixers()).map(info -> info.getName()).toArray(String[]::new));
command.setJavaJukeboxMixer(player.getJavaJukeboxMixer());
if (player != null) {
command.setJavaJukeboxMixer(player.getJavaJukeboxMixer());
}
model.addAttribute("command",command);
}

@ -166,9 +166,7 @@ public class UploadController {
private void unzip(File file, List<File> unzippedFiles) throws Exception {
LOG.info("Unzipping " + file);
ZipFile zipFile = new ZipFile(file);
try {
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<?> entries = zipFile.entries();
@ -210,8 +208,6 @@ public class UploadController {
zipFile.close();
file.delete();
} finally {
zipFile.close();
}
}

@ -24,6 +24,7 @@ import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* A music index is a mapping from an index string to a list of prefixes. A complete index consists of a list of
@ -99,7 +100,7 @@ public class MusicIndex implements Serializable {
final MusicIndex musicIndex = (MusicIndex) o;
if (index != null ? !index.equals(musicIndex.index) : musicIndex.index != null) {
if (!Objects.equals(index, musicIndex.index)) {
return false;
}

@ -215,9 +215,7 @@ public class PlayQueue {
}
files.remove(index);
if (index != -1) {
this.index = Math.max(0, Math.min(this.index, size() - 1));
}
this.index = Math.max(0, Math.min(this.index, size() - 1));
}
/**

@ -21,6 +21,8 @@ package org.airsonic.player.domain;
import org.airsonic.player.util.StringUtil;
import java.util.Objects;
/**
* Contains the configuration for a transcoding, i.e., a specification of how a given media format
* should be converted to another.
@ -213,7 +215,7 @@ public class Transcoding {
}
Transcoding that = (Transcoding) o;
return !(id != null ? !id.equals(that.id) : that.id != null);
return Objects.equals(id, that.id);
}
public int hashCode() {

@ -206,7 +206,7 @@ public class User {
@Override
public String toString() {
StringBuffer result = new StringBuffer(username);
StringBuilder result = new StringBuilder(username);
if (isAdminRole) {
result.append(" [admin]");

@ -56,7 +56,7 @@ public class TranscodeInputStream extends InputStream {
public TranscodeInputStream(ProcessBuilder processBuilder, final InputStream in, File tmpFile) throws IOException {
this.tmpFile = tmpFile;
StringBuffer buf = new StringBuffer("Starting transcoder: ");
StringBuilder buf = new StringBuilder("Starting transcoder: ");
for (String s : processBuilder.command()) {
buf.append('[').append(s).append("] ");
}

@ -49,7 +49,7 @@ public class MetricsManager {
}
}
}
return metricsActivatedByConfiguration.booleanValue();
return metricsActivatedByConfiguration;
}
/**

@ -347,10 +347,6 @@ public class MediaFileService {
/**
* Returns random songs matching search criteria.
*
* @param criteria Random search criteria.
* @param count Max number of songs to return.
* @return Random songs
* @see SearchService.getRandomSongs
*/
public List<MediaFile> getRandomSongs(RandomSearchCriteria criteria, String username) {
return mediaFileDao.getRandomSongs(criteria, username);

@ -265,13 +265,7 @@ public class MusicIndexService {
indexB = Integer.MAX_VALUE;
}
if (indexA < indexB) {
return -1;
}
if (indexA > indexB) {
return 1;
}
return 0;
return Integer.compare(indexA, indexB);
}
}
}

@ -450,7 +450,7 @@ public class PodcastService {
if (getEpisodeByUrl(url) == null) {
Long length = null;
try {
length = new Long(enclosure.getAttributeValue("length"));
length = Long.valueOf(enclosure.getAttributeValue("length"));
} catch (Exception x) {
LOG.warn("Failed to parse enclosure length.", x);
}
@ -470,13 +470,7 @@ public class PodcastService {
long timeA = a.getPublishDate() == null ? 0L : a.getPublishDate().getTime();
long timeB = b.getPublishDate() == null ? 0L : b.getPublishDate().getTime();
if (timeA < timeB) {
return 1;
}
if (timeA > timeB) {
return -1;
}
return 0;
return Long.compare(timeB, timeA);
}
});

@ -96,8 +96,8 @@ public class SecurityService implements UserDetailsService {
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("IS_AUTHENTICATED_ANONYMOUSLY"));
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
for (int i = 0; i < roles.length; i++) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + roles[i].toUpperCase()));
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.toUpperCase()));
}
return authorities;
}

@ -161,7 +161,7 @@ public class JaudiotaggerParser extends MetaDataParser {
Integer result = null;
try {
result = new Integer(trackNumber);
result = Integer.valueOf(trackNumber);
} catch (NumberFormatException x) {
Matcher matcher = TRACK_NUMBER_PATTERN.matcher(trackNumber);
if (matcher.matches()) {
@ -187,7 +187,7 @@ public class JaudiotaggerParser extends MetaDataParser {
Integer result = null;
try {
result = new Integer(year);
result = Integer.valueOf(year);
} catch (NumberFormatException x) {
Matcher matcher = YEAR_NUMBER_PATTERN.matcher(year);
if (matcher.matches()) {

@ -88,7 +88,7 @@ public class UrlTag extends BodyTagSupport {
private String formatUrl() throws JspException {
String baseUrl = UrlSupport.resolveUrl(value, null, pageContext);
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
result.append(baseUrl);
if (!parameters.isEmpty()) {
result.append('?');

@ -30,10 +30,7 @@ import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -327,7 +324,7 @@ public final class StringUtil {
* @return Whether a and b are equal, or both null.
*/
public static boolean isEqual(Object a, Object b) {
return a == null ? b == null : a.equals(b);
return Objects.equals(a, b);
}
/**

Loading…
Cancel
Save