Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #46 from Safe0/next
[JENKINS-31221] Resolved issue - Implement access to comments count
  • Loading branch information
mixalturek committed Nov 3, 2015
2 parents a4c8ffc + 3a12ffe commit de3d4b1
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 44 deletions.
41 changes: 38 additions & 3 deletions src/main/java/hudson/plugins/sloccount/SloccountDiff.java
Expand Up @@ -19,6 +19,12 @@ public abstract class SloccountDiff implements Comparable<SloccountDiff> {

/** Difference of files count between current and previous report. */
private final int fileCountDelta;

/** Comments count in the newer report. */
private final int commentCount;

/** Difference of comments count between current and previous report. */
private final int commentCountDelta;

/**
* Constructor.
Expand All @@ -28,18 +34,25 @@ public abstract class SloccountDiff implements Comparable<SloccountDiff> {
* @param lineCountDelta
* difference of lines count between current and previous
* report
* @param fileCount files
* count in the newer report
* @param fileCount
* files count in the newer report
* @param fileCountDelta
* difference of files count between current and previous
* report
* @param commentCount
* comments count in the newer report
* @param commentCountDelta
* difference of comments count between current and previous
* report
*/
public SloccountDiff(int lineCount,
int lineCountDelta, int fileCount, int fileCountDelta) {
int lineCountDelta, int fileCount, int fileCountDelta, int commentCount, int commentCountDelta) {
this.lineCount = lineCount;
this.lineCountDelta = lineCountDelta;
this.fileCount = fileCount;
this.fileCountDelta = fileCountDelta;
this.commentCount = commentCount;
this.commentCountDelta = commentCountDelta;
}

/**
Expand Down Expand Up @@ -84,6 +97,14 @@ public int getLineCount() {
public int getLineCountDelta() {
return lineCountDelta;
}

public int getCommentCount() {
return commentCount;
}

public int getCommentCountDelta() {
return commentCountDelta;
}

public int getFileCount() {
return fileCount;
Expand All @@ -106,6 +127,20 @@ public String getLineCountDeltaString() {
String result = StringUtil.grouping(lineCountDelta);
return (lineCountDelta > 0) ? "+" + result : result;
}

public String getCommentCountString() {
return StringUtil.grouping(commentCount);
}

public String getCommentCountDeltaString() {
if(commentCountDelta == 0) {
return "";
}

// Negative prefix '-' is added automatically
String result = StringUtil.grouping(commentCountDelta);
return (commentCountDelta > 0) ? "+" + result : result;
}

public String getFileCountString() {
return StringUtil.grouping(fileCount);
Expand Down
Expand Up @@ -26,10 +26,15 @@ public class SloccountDiffLanguage extends SloccountDiff {
* @param fileCountDelta
* difference of files count between current and previous
* report
* @param commentCount
* comments count in the newer report
* @param commentCountDelta
* difference of comments count between current and previous
* report
*/
public SloccountDiffLanguage(String name, int lineCount,
int lineCountDelta, int fileCount, int fileCountDelta) {
super(lineCount, lineCountDelta, fileCount, fileCountDelta);
int lineCountDelta, int fileCount, int fileCountDelta, int commentCount, int commentCountDelta) {
super(lineCount, lineCountDelta, fileCount, fileCountDelta, commentCount, commentCountDelta);
this.name = name;
}

Expand Down
29 changes: 21 additions & 8 deletions src/main/java/hudson/plugins/sloccount/SloccountDiffSummary.java
Expand Up @@ -33,12 +33,16 @@ public class SloccountDiffSummary extends SloccountDiff {
* @param fileCountDelta
* difference of files count between current and previous
* report
*
* @param commentCount
* comments count in the newer report
* @param commentCountDelta
* difference of comments count between current and previous
* report
* @see #getDiffSummary(SloccountReportStatistics, SloccountReportStatistics)
*/
private SloccountDiffSummary(List<SloccountDiffLanguage> languageDiffs,
int lineCount, int lineCountDelta, int fileCount, int fileCountDelta) {
super(lineCount, lineCountDelta, fileCount, fileCountDelta);
int lineCount, int lineCountDelta, int fileCount, int fileCountDelta, int commentCount, int commentCountDelta) {
super(lineCount, lineCountDelta, fileCount, fileCountDelta, commentCount, commentCountDelta);

// No copy, can be called only internally
this.languageDiffs = languageDiffs;
Expand Down Expand Up @@ -70,6 +74,8 @@ public static SloccountDiffSummary getDiffSummary(
int lineCountDelta = 0;
int fileCount = 0;
int fileCountDelta = 0;
int commentCount = 0;
int commentCountDelta = 0;

for(String language: languages) {
// Quadratic complexity can be optimized, but languages count is small
Expand All @@ -80,16 +86,21 @@ public static SloccountDiffSummary getDiffSummary(
curStats.getLineCount(),
curStats.getLineCount() - prevStats.getLineCount(),
curStats.getFileCount(),
curStats.getFileCount() - prevStats.getFileCount()));
curStats.getFileCount() - prevStats.getFileCount(),
curStats.getCommentCount(),
curStats.getCommentCount() - prevStats.getCommentCount()));

lineCount += curStats.getLineCount();
lineCountDelta += curStats.getLineCount() - prevStats.getLineCount();
fileCount += curStats.getFileCount();
fileCountDelta += curStats.getFileCount() - prevStats.getFileCount();
commentCount += curStats.getCommentCount();
commentCountDelta += curStats.getCommentCount() - prevStats.getCommentCount();
}

return new SloccountDiffSummary(result, lineCount, lineCountDelta,
fileCount, fileCountDelta);
fileCount, fileCountDelta,
commentCount, commentCountDelta);
}

/**
Expand All @@ -107,16 +118,18 @@ private static SloccountDiffSummary getDiffSummary(SloccountReportStatistics cur
List<SloccountDiffLanguage> result = new ArrayList<SloccountDiffLanguage>();
int lineCount = 0;
int fileCount = 0;
int commentCount = 0;

for(SloccountLanguageStatistics language: current.getStatistics()) {
result.add(new SloccountDiffLanguage(language.getName(),
language.getLineCount(), 0, language.getFileCount(), 0));
language.getLineCount(), 0, language.getFileCount(), 0, language.getCommentCount(), 0));

lineCount += language.getLineCount();
fileCount += language.getFileCount();
commentCount += language.getCommentCount();
}

return new SloccountDiffSummary(result, lineCount, 0, fileCount, 0);
return new SloccountDiffSummary(result, lineCount, 0, fileCount, 0, commentCount, 0);
}

/**
Expand All @@ -127,7 +140,7 @@ private static SloccountDiffSummary getDiffSummary(SloccountReportStatistics cur
*/
private static SloccountDiffSummary getDiffSummary() {
return new SloccountDiffSummary(
Collections.<SloccountDiffLanguage>emptyList(), 0, 0, 0, 0);
Collections.<SloccountDiffLanguage>emptyList(), 0, 0, 0, 0, 0, 0);
}

/**
Expand Down
Expand Up @@ -74,7 +74,7 @@ protected Object readResolve() {

for(Language language : report.getLanguages()){
languages.add(new SloccountLanguageStatistics(language.getName(),
language.getLineCount(), language.getFileCount()));
language.getLineCount(), language.getFileCount(), language.getCommentCount()));
}

statistics = new SloccountReportStatistics(languages);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/hudson/plugins/sloccount/model/File.java
Expand Up @@ -17,13 +17,14 @@ public class File implements Countable, Serializable {
/** The module. */
private final String module;

private final int lineCount;
private final int lineCount, commentCount;

public File(String name, String language, String module, int lineCount){
public File(String name, String language, String module, int lineCount, int commentCount){
this.name = name;
this.language = language;
this.module = module;
this.lineCount = lineCount;
this.commentCount = commentCount;
}

public int getLineCount() {
Expand All @@ -34,6 +35,14 @@ public String getLineCountString() {
return StringUtil.grouping(getLineCount());
}

public int getCommentCount() {
return this.commentCount;
}

public String getCommentCountString() {
return StringUtil.grouping(getCommentCount());
}

public String getName() {
return this.name;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/hudson/plugins/sloccount/model/FileContainer.java
Expand Up @@ -17,6 +17,7 @@ public abstract class FileContainer implements Countable, Serializable {

private Map<String, File> files = new LinkedHashMap<String, File>();
private int lineCount = 0;
private int commentCount = 0;

public File getFile(String name){
return this.files.get(name);
Expand All @@ -37,6 +38,7 @@ public String getFileCountString() {
public void addFile(File file){
this.files.put(file.getName(), file);
this.lineCount += file.getLineCount();
this.commentCount += file.getCommentCount();
}

public int getLineCount(){
Expand All @@ -46,4 +48,12 @@ public int getLineCount(){
public String getLineCountString() {
return StringUtil.grouping(getLineCount());
}

public int getCommentCount(){
return this.commentCount;
}

public String getCommentCountString() {
return StringUtil.grouping(getCommentCount());
}
}
Expand Up @@ -23,6 +23,9 @@ public class SloccountLanguageStatistics implements Serializable {

/** Number of files containing this language. */
private final int fileCount;

/** Number of comments containing this language. */
private final int commentCount;

/**
* Constructor initializing members.
Expand All @@ -35,10 +38,11 @@ public class SloccountLanguageStatistics implements Serializable {
* number of files containing this language
*/
public SloccountLanguageStatistics(String languageName, int lineCount,
int fileCount){
int fileCount, int commentCount){
this.name = languageName;
this.lineCount = lineCount;
this.fileCount = fileCount;
this.commentCount = commentCount;
}

/**
Expand Down Expand Up @@ -70,4 +74,14 @@ public int getLineCount(){
public int getFileCount(){
return fileCount;
}

/**
* Get number of comments containing this language.
*
* @return the number of comments
*/
@Exported(name="comments")
public int getCommentCount(){
return commentCount;
}
}
Expand Up @@ -138,6 +138,6 @@ private void parseLine(String line, SloccountReportInterface report){
logger.println("module : " + moduleName);
}

report.add(filePath, languageName, moduleName, lineCount);
report.add(filePath, languageName, moduleName, lineCount, 0);
}
}
Expand Up @@ -34,7 +34,7 @@ public SloccountReportStatistics getStatistics(){

for(Map.Entry<String, LanguageStatistics> it : statistics.entrySet()){
ret.add(new SloccountLanguageStatistics(it.getKey(),
it.getValue().numLines, it.getValue().numFiles));
it.getValue().numLines, it.getValue().numFiles, it.getValue().numComments));
}

return new SloccountReportStatistics(ret);
Expand All @@ -59,7 +59,7 @@ public void addSourceFile(File sourceFile){
sourceFiles.add(new SlaveFile(sourceFile));
}

public void add(String filePath, String languageName, String moduleName, int lineCount){
public void add(String filePath, String languageName, String moduleName, int lineCount, int commentCount){
LanguageStatistics stat = statistics.get(languageName);

if(stat == null){
Expand All @@ -68,6 +68,7 @@ public void add(String filePath, String languageName, String moduleName, int lin
}

stat.numLines += lineCount;
stat.numComments += commentCount;
++stat.numFiles;
}

Expand All @@ -90,6 +91,9 @@ public static class LanguageStatistics implements Serializable {

/** The number of files. */
int numFiles = 0;

/** The number of comments. */
int numComments = 0;
}

/**
Expand Down
Expand Up @@ -34,18 +34,18 @@ public SloccountReport(SloccountReport old, FileFilter filter){
this();
for(File f : old.getFiles()){
if(filter.include(f)){
this.add(f.getName(), f.getLanguage(), f.getModule(), f.getLineCount());
this.add(f.getName(), f.getLanguage(), f.getModule(), f.getLineCount(), f.getCommentCount());
}
}
}

public void add(String filePath, String languageName, String moduleName, int lineCount){
public void add(String filePath, String languageName, String moduleName, int lineCount, int commentCount){
// Get rid of Microsoft's incompatibility once and forever
filePath = filePath.replace("\\", DIRECTORY_SEPARATOR);

String folderPath = extractFolder(filePath);

File file = new File(filePath, languageName, moduleName, lineCount);
File file = new File(filePath, languageName, moduleName, lineCount, commentCount);
this.addFile(file);

Folder folder = this.getFolder(folderPath);
Expand Down
Expand Up @@ -18,8 +18,10 @@ public interface SloccountReportInterface {
* the module name
* @param lineCount
* the line count
* @param commentCount
* the comment count
*/
void add(String filePath, String languageName, String moduleName, int lineCount);
void add(String filePath, String languageName, String moduleName, int lineCount, int commentCount);

/**
* Get the root folder.
Expand Down

0 comments on commit de3d4b1

Please sign in to comment.