Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__com_google_guava_guava_19_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__com_squareup_okhttp3_okhttp_3_3_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__com_squareup_okio_okio_1_8_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_jdom_jdom2_2_0_5.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions CountMostImport/CountMostImport.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
</component>
</module>
21 changes: 21 additions & 0 deletions CountMostImport/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.air</groupId>
<artifactId>CountMostImport</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

</dependencies>

</project>
104 changes: 104 additions & 0 deletions CountMostImport/src/main/java/CountMostImport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Created by KL on 2016/6/29.
*/

import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Ordering;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.primitives.Ints.min;
import static java.lang.Math.PI;

public class CountMostImport {
//Match files with suffix java
private static final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java");
//Most N imported classes
private static final int mostN = 1000;
private List<File> javaFiles = new ArrayList<File>();
//Scan path for java files
Path path = Paths.get("E:\\code\\work\\campus2016");


private void getJavaFiles() {
SimpleFileVisitor<Path> javaFinder = new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(matcher.matches(file))
javaFiles.add(file.toFile());
return super.visitFile(file, attrs);
}
};
try {
java.nio.file.Files.walkFileTree(path, javaFinder);
System.out.println("files = " + javaFiles);
} catch (IOException e) {
e.printStackTrace();
}
}

public List<Map.Entry<String, Long>> countMostImportClass() {
Map<String, Long> result = new HashMap<>();
getJavaFiles();

for (File f : javaFiles) {
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(f))) { // Java7 try with resources
String line = null;
while ((line = bufferedReader.readLine()) != null) {
//process perline
String trimedLine = line.trim();
if(!trimedLine.startsWith("import"))
continue;
//import statement
List<String> pieces = Splitter.on(CharMatcher.anyOf(" ;"))
.trimResults()
.omitEmptyStrings()
.splitToList(trimedLine);
if (pieces.get(pieces.size() - 1).endsWith("*"))
continue;
else if(pieces.size() == 3) {
//Static import
String methodName = pieces.get(pieces.size() - 1);
String classQualifiedName = methodName.substring(0, methodName.lastIndexOf('.'));
result.put(classQualifiedName, result.containsKey(classQualifiedName) ? result.get(classQualifiedName) + 1 : 1);
} else if(pieces.size() == 2) {
// None static
String classQualifiedName = pieces.get(pieces.size() - 1);
result.put(classQualifiedName, result.containsKey(classQualifiedName) ? result.get(classQualifiedName) + 1 : 1);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// for end

//Guava Ordering based on map.value
Ordering<Map.Entry<String, Long>> valueOrdering = Ordering.natural().onResultOf(
new Function<Map.Entry<String, Long>, Long>() {
@Override
public Long apply(Map.Entry<String, Long> entry) {
return entry.getValue();
}
}).reverse();

List< Map.Entry<String, Long>> l = valueOrdering.sortedCopy(result.entrySet());
return l.subList(0, min(l.size(), mostN));
}


public static void main(String[] args) {
System.out.println(PI);
List<Map.Entry<String, Long>> l = new CountMostImport().countMostImportClass();
System.out.println(l);
}
}
15 changes: 15 additions & 0 deletions EffectiveLines/EffectiveLines.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
24 changes: 24 additions & 0 deletions EffectiveLines/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.air</groupId>
<artifactId>EffectiveLines</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>


</project>
58 changes: 58 additions & 0 deletions EffectiveLines/src/main/java/EffectiveLines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.*;

/**
* Created by lqs on 2016/6/25.
*/
public class EffectiveLines {

/**
* @param path Java file path in string
* @return Effective lines in the java file
*/
public static long countEffectiveLines(String path) {
File sourceFile = new File(path);
return countEffectiveLines(sourceFile);
}

/**
* @param file Java file path
* @return Effective lines
*/
public static long countEffectiveLines(File file) {
long effectiveLines = 0L;

try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { // Java7 try with resources
String line = null;
while ((line = bufferedReader.readLine()) != null) {
//process perline
String trimedLine = line.trim();
if(trimedLine.equals("") || trimedLine.startsWith("//"))
continue;
else
effectiveLines++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
}
return effectiveLines;
}

public static final void usage() {
System.out.println("Usage:\n" +
"EffectiveLines filename\n" +
"Output: effective lines in the java file\n" +
"A line is considered effective if it is not a blank line nor a comment line\n" +
"Multi line comment is not in consideration");
}

public static void main(String[] args) {
if(args.length < 1) {
usage();
return;
}
System.out.println(EffectiveLines.countEffectiveLines(args[0]));
}
}
22 changes: 22 additions & 0 deletions ExchangeRate/ExchangeRate.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.3.1" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.8.0" level="project" />
<orderEntry type="library" name="Maven: net.sourceforge.htmlcleaner:htmlcleaner:2.6.1" level="project" />
<orderEntry type="library" name="Maven: org.jdom:jdom2:2.0.5" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi:3.14" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
</component>
</module>
Loading