Don't need to nest else clause. Refactor magic numbers into constants.
MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -823,12 +823,11 @@ public static boolean contentEqualsIgnoreEOL(final File file1, final File file2,
public static File toFile(final URL url) {
if (url == null || !"file".equalsIgnoreCase(url.getProtocol())) {
return null;
- } else {
+ }
String filename = url.getFile().replace('/', File.separatorChar);
filename = decodeUrl(filename);
return new File(filename);
}
- }
/**
* Decodes the specified URL as per RFC 3986, i.e. transforms
@@ -2542,9 +2541,8 @@ public static long sizeOf(final File file) {
if (file.isDirectory()) {
return sizeOfDirectory0(file); // private method; expects directory
- } else {
- return file.length();
}
+ return file.length();
}
@@ -2575,9 +2573,8 @@ public static BigInteger sizeOfAsBigInteger(final File file) {
if (file.isDirectory()) {
return sizeOfDirectoryBig0(file); // internal method
- } else {
- return BigInteger.valueOf(file.length());
}
+ return BigInteger.valueOf(file.length());
}
@@ -2638,9 +2635,8 @@ private static long sizeOfDirectory0(final File directory) {
private static long sizeOf0(final File file) {
if (file.isDirectory()) {
return sizeOfDirectory0(file);
- } else {
- return file.length(); // will be 0 if file does not exist
}
+ return file.length(); // will be 0 if file does not exist
}
/**
@@ -2694,9 +2690,8 @@ private static BigInteger sizeOfDirectoryBig0(final File directory) {
private static BigInteger sizeOfBig0(final File fileOrDir) {
if (fileOrDir.isDirectory()) {
return sizeOfDirectoryBig0(fileOrDir);
- } else {
- return BigInteger.valueOf(fileOrDir.length());
}
+ return BigInteger.valueOf(fileOrDir.length());
}
/**

MODIFY
src/main/java/org/apache/commons/io/comparator/DirectoryFileComparator.java
src/main/java/org/apache/commons/io/comparator/DirectoryFileComparator.java
*/
public class DirectoryFileComparator extends AbstractFileComparator implements Serializable {
+ private static final int TYPE_FILE = 2;
+
+ private static final int TYPE_DIRECTORY = 1;
+
private static final long serialVersionUID = 296132640160964395L;
/** Singleton default comparator instance */
@@ -72,10 +76,6 @@ public int compare(final File file1, final File file2) {
* @return 1 for directories and 2 for files
*/
private int getType(final File file) {
- if (file.isDirectory()) {
- return 1;
- } else {
- return 2;
- }
+ return file.isDirectory() ? TYPE_DIRECTORY : TYPE_FILE;
}
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java
src/main/java/org/apache/commons/io/filefilter/DelegateFileFilter.java
@@ -74,9 +74,8 @@ public DelegateFileFilter(final FileFilter filter) {
public boolean accept(final File file) {
if (fileFilter != null) {
return fileFilter.accept(file);
- } else {
- return super.accept(file);
}
+ return super.accept(file);
}
/**
@@ -90,9 +89,8 @@ public boolean accept(final File file) {
public boolean accept(final File dir, final String name) {
if (filenameFilter != null) {
return filenameFilter.accept(dir, name);
- } else {
- return super.accept(dir, name);
}
+ return super.accept(dir, name);
}
/**

MODIFY
src/main/java/org/apache/commons/io/filefilter/EmptyFileFilter.java
src/main/java/org/apache/commons/io/filefilter/EmptyFileFilter.java
@@ -79,9 +79,8 @@ public boolean accept(final File file) {
if (file.isDirectory()) {
final File[] files = file.listFiles();
return files == null || files.length == 0;
- } else {
- return file.length() == 0;
}
+ return file.length() == 0;
}
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java
src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java
@@ -738,11 +738,7 @@ public static IOFileFilter magicNumberFileFilter(final byte[] magicNumber, final
* @since 1.1 (method existed but had bug in 1.0)
*/
public static IOFileFilter makeCVSAware(final IOFileFilter filter) {
- if (filter == null) {
- return cvsFilter;
- } else {
- return and(filter, cvsFilter);
- }
+ return filter == null ? cvsFilter : and(filter, cvsFilter);
}
/**
@@ -755,11 +751,7 @@ public static IOFileFilter makeCVSAware(final IOFileFilter filter) {
* @since 1.1
*/
public static IOFileFilter makeSVNAware(final IOFileFilter filter) {
- if (filter == null) {
- return svnFilter;
- } else {
- return and(filter, svnFilter);
- }
+ return filter == null ? svnFilter : and(filter, svnFilter);
}
//-----------------------------------------------------------------------

MODIFY
src/main/java/org/apache/commons/io/input/CharSequenceReader.java
src/main/java/org/apache/commons/io/input/CharSequenceReader.java
@@ -84,9 +84,8 @@ public boolean markSupported() {
public int read() {
if (idx >= charSequence.length()) {
return EOF;
- } else {
- return charSequence.charAt(idx++);
}
+ return charSequence.charAt(idx++);
}
/**

MODIFY
src/main/java/org/apache/commons/io/input/DemuxInputStream.java
src/main/java/org/apache/commons/io/input/DemuxInputStream.java
@@ -75,9 +75,6 @@ public int read()
{
return input.read();
}
- else
- {
return EOF;
}
}
-}

MODIFY
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
@@ -256,7 +256,7 @@ private FilePart rollOver() throws IOException {
if (no > 1) {
return new FilePart(no - 1, blockSize, leftOver);
- } else {
+ }
// NO 1 was the last FilePart, we're finished
if (leftOver != null) {
throw new IllegalStateException("Unexpected leftover of the last block: leftOverOfThisFilePart="
@@ -264,7 +264,6 @@ private FilePart rollOver() throws IOException {
}
return null;
}
- }
/**
* Reads a line.

MODIFY
src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
@@ -73,7 +73,6 @@ public int read() throws IOException {
if ( eofSeen ) {
return eofGame(previousWasSlashR);
}
- else {
final int target = readWithUpdate();
if ( eofSeen ) {
return eofGame(previousWasSlashR);
@@ -89,7 +88,6 @@ public int read() throws IOException {
return target;
}
- }
/**
* Handles the eof-handling at the end of the stream
@@ -103,9 +101,8 @@ private int eofGame(final boolean previousWasSlashR) {
if ( !slashNSeen ) {
slashNSeen = true;
return '\n';
- } else {
- return -1;
}
+ return -1;
}
/**

MODIFY
src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
@@ -109,9 +109,8 @@ private int eofGame() {
slashRSeen = false;
slashNSeen = true;
return '\n';
- } else {
- return -1;
}
+ return -1;
}
/**

MODIFY
src/main/java/org/apache/commons/io/input/XmlStreamReader.java
src/main/java/org/apache/commons/io/input/XmlStreamReader.java
@@ -430,9 +430,8 @@ private String doRawStream(final BOMInputStream bom, final BOMInputStream pis, f
} catch (final XmlStreamReaderException ex) {
if (lenient) {
return doLenientDetection(null, ex);
- } else {
- throw ex;
}
+ throw ex;
}
}
@@ -458,9 +457,8 @@ private String doHttpStream(final BOMInputStream bom, final BOMInputStream pis,
} catch (final XmlStreamReaderException ex) {
if (lenient) {
return doLenientDetection(httpContentType, ex);
- } else {
- throw ex;
}
+ throw ex;
}
}
@@ -601,9 +599,8 @@ String calculateHttpEncoding(final String httpContentType,
if (cTEnc == null) {
if (appXml) {
return calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc);
- } else {
- return defaultEncoding == null ? US_ASCII : defaultEncoding;
}
+ return defaultEncoding == null ? US_ASCII : defaultEncoding;
}
// UTF-16BE or UTF-16LE content type encoding
@@ -722,12 +719,11 @@ private static String getXmlProlog(final InputStream is, final String guessedEnc
if (firstGT == -1) {
if (c == -1) {
throw new IOException("Unexpected end of XML stream");
- } else {
+ }
throw new IOException(
"XML prolog or ROOT element not found on first "
+ offset + " bytes");
}
- }
final int bytesRead = offset;
if (bytesRead > 0) {
is.reset();

Don't need to nest else clause. Refactor magic numbers into constants.