Created
June 27, 2018 14:31
-
-
Save Bios-Marcel/13cdfd814e0264072d76ddd4434b7daf to your computer and use it in GitHub Desktop.
Recursively counts the lines of all java files in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Objects; | |
/** | |
* @author msc | |
* @since 18.06.2018 | |
*/ | |
public class CheckLineCounts | |
{ | |
public static void main( final String[] args ) | |
{ | |
final File currentDir = new File( "Path..." ); | |
listLineCounts( Arrays.asList( currentDir.listFiles() ) ); | |
} | |
private static void listLineCounts( final Collection<File> files ) | |
{ | |
files | |
.stream() | |
//Get all files recursively | |
.map( file -> recursiveFiles( file ) ) | |
//Combine all lists | |
.reduce( ( filesOne, filesTwo ) -> | |
{ | |
filesOne.addAll( filesTwo ); | |
return filesOne; | |
} ) | |
//Since we can't be arsed to check for null or so | |
.orElse( new ArrayList<>() ) | |
//Stream of the combined list | |
.stream() | |
//Only java files | |
.filter( file -> file.getName().endsWith( ".java" ) ) | |
//Get line count of every file | |
.map( CheckLineCounts::getLineInfo ) | |
//Filter out null values | |
.filter( Objects::nonNull ) | |
//distinct entries only | |
.distinct() | |
//sort from smallest to biggest | |
.sorted( ( pairOne, pairTwo ) -> pairOne.getSize().compareTo( pairTwo.getSize() ) ) | |
//print out information to console | |
.forEach( System.out::println ); | |
} | |
private static List<File> recursiveFiles( final File file ) | |
{ | |
if ( not( file.isDirectory() ) ) | |
{ | |
return Arrays.asList( file ); | |
} | |
final List<File> files = new ArrayList<>(); | |
for ( final File childFile : file.listFiles() ) | |
{ | |
files.addAll( recursiveFiles( childFile ) ); | |
} | |
return files; | |
} | |
private static boolean not( final boolean b ) | |
{ | |
return !b; | |
} | |
private static LongFile getLineInfo( final File file ) | |
{ | |
try | |
{ | |
return new LongFile( file.getName(), Files.readAllLines( file.toPath() ).size() ); | |
} | |
catch ( @SuppressWarnings( "unused" ) final IOException exception ) | |
{ | |
return null; | |
} | |
} | |
private static class LongFile | |
{ | |
private final String file; | |
private final Integer size; | |
public LongFile( final String file, final Integer size ) | |
{ | |
super(); | |
this.file = file; | |
this.size = size; | |
} | |
@Override | |
public boolean equals( final Object obj ) | |
{ | |
return file.equals( ((LongFile) obj).file ); | |
} | |
/** | |
* @return the size | |
*/ | |
public Integer getSize() | |
{ | |
return size; | |
} | |
@Override | |
public String toString() | |
{ | |
return size + " lines in " + file; | |
} | |
@Override | |
public int hashCode() | |
{ | |
return super.hashCode(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment