-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65325ac
commit 1104f8b
Showing
35 changed files
with
487 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/leap/AvoidConditionalLogic.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyzer.exercises.leap; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/avoid_conditional_logic.md">Markdown Template</a> | ||
*/ | ||
class AvoidConditionalLogic extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.leap.avoid_conditional_logic"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.ACTIONABLE; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package analyzer.exercises.leap; | ||
|
||
import analyzer.Analysis; | ||
import analyzer.Analyzer; | ||
import analyzer.comments.AvoidHardCodedTestCases; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.ImportDeclaration; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.expr.BinaryExpr; | ||
import com.github.javaparser.ast.expr.ConditionalExpr; | ||
import com.github.javaparser.ast.expr.IntegerLiteralExpr; | ||
import com.github.javaparser.ast.stmt.IfStmt; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class LeapAnalyzer extends VoidVisitorAdapter<Analysis> implements Analyzer { | ||
private static final Set<Integer> TEST_CASES = Set.of(1960, 1996, 2000, 2400); | ||
private static final Set<String> DISALLOWED_IMPORTS = Set.of( | ||
"java.time", | ||
"java.util.GregorianCalendar" | ||
); | ||
|
||
private final Set<Integer> intLiterals = new HashSet<>(); | ||
|
||
@Override | ||
public void analyze(List<CompilationUnit> compilationUnits, Analysis analysis) { | ||
for (CompilationUnit compilationUnit : compilationUnits) { | ||
compilationUnit.accept(this, analysis); | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(CompilationUnit node, Analysis analysis) { | ||
// Reset state for each compilation unit | ||
this.intLiterals.clear(); | ||
|
||
super.visit(node, analysis); | ||
} | ||
|
||
@Override | ||
public void visit(ImportDeclaration node, Analysis analysis) { | ||
if (isUsingBuiltInMethods(node)) { | ||
analysis.addComment(new NoBuiltInMethods()); | ||
} | ||
|
||
super.visit(node, analysis); | ||
} | ||
|
||
@Override | ||
public void visit(IntegerLiteralExpr node, Analysis analysis) { | ||
if (node.asNumber() instanceof Integer i) { | ||
this.intLiterals.add(i); | ||
} | ||
|
||
if (this.intLiterals.containsAll(TEST_CASES)) { | ||
analysis.addComment(new AvoidHardCodedTestCases()); | ||
} | ||
|
||
super.visit(node, analysis); | ||
} | ||
|
||
@Override | ||
public void visit(IfStmt node, Analysis analysis) { | ||
analysis.addComment(new AvoidConditionalLogic()); | ||
super.visit(node, analysis); | ||
} | ||
|
||
@Override | ||
public void visit(ConditionalExpr node, Analysis analysis) { | ||
analysis.addComment(new AvoidConditionalLogic()); | ||
super.visit(node, analysis); | ||
} | ||
|
||
@Override | ||
public void visit(MethodDeclaration node, Analysis analysis) { | ||
if (node.getNameAsString().equals("isLeapYear") && hasMoreThanThreeChecks(node)) { | ||
analysis.addComment(new UseMinimumNumberOfChecks()); | ||
} | ||
super.visit(node, analysis); | ||
} | ||
|
||
private static boolean isUsingBuiltInMethods(ImportDeclaration node) { | ||
var name = node.getNameAsString(); | ||
return DISALLOWED_IMPORTS.stream().anyMatch(name::contains); | ||
} | ||
|
||
private static boolean hasMoreThanThreeChecks(MethodDeclaration node) { | ||
var booleanOperators = node.findAll(BinaryExpr.class, | ||
x -> x.getOperator() == BinaryExpr.Operator.AND || x.getOperator() == BinaryExpr.Operator.OR); | ||
|
||
return booleanOperators.size() > 2; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/leap/NoBuiltInMethods.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyzer.exercises.leap; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/no_built_in_methods.md">Markdown Template</a> | ||
*/ | ||
class NoBuiltInMethods extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.leap.no_built_in_methods"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.ESSENTIAL; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/leap/UseMinimumNumberOfChecks.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyzer.exercises.leap; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/use_minimum_number_of_checks.md">Markdown Template</a> | ||
*/ | ||
class UseMinimumNumberOfChecks extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.leap.use_minimum_number_of_checks"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.ACTIONABLE; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"authors": [ | ||
"sonapraneeth-a" | ||
], | ||
"contributors": [ | ||
"jmrunkle", | ||
"lemoncurry", | ||
"msomji", | ||
"muzimuzhi", | ||
"sshine" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Leap.java" | ||
], | ||
"test": [ | ||
"src/test/java/LeapTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Leap.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Determine whether a given year is a leap year.", | ||
"source": "CodeRanch Cattle Drive, Assignment 3", | ||
"source_url": "https://coderanch.com/t/718816/Leap" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{"comments": [ | ||
{ | ||
"comment": "java.general.avoid_hard_coded_test_cases", | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.leap.use_minimum_number_of_checks", | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"type": "informative" | ||
} | ||
]} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Leap { | ||
boolean isLeapYear(int year) { | ||
return year == 1960 || year == 2000 || year == 2400 || year == 1996; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"authors": [ | ||
"sonapraneeth-a" | ||
], | ||
"contributors": [ | ||
"jmrunkle", | ||
"lemoncurry", | ||
"msomji", | ||
"muzimuzhi", | ||
"sshine" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Leap.java" | ||
], | ||
"test": [ | ||
"src/test/java/LeapTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Leap.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Determine whether a given year is a leap year.", | ||
"source": "CodeRanch Cattle Drive, Assignment 3", | ||
"source_url": "https://coderanch.com/t/718816/Leap" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Leap { | ||
boolean isLeapYear(int year) { | ||
return year % 400 == 0 || year % 100 != 0 && year % 4 == 0; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"authors": [ | ||
"sonapraneeth-a" | ||
], | ||
"contributors": [ | ||
"jmrunkle", | ||
"lemoncurry", | ||
"msomji", | ||
"muzimuzhi", | ||
"sshine" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Leap.java" | ||
], | ||
"test": [ | ||
"src/test/java/LeapTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Leap.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Determine whether a given year is a leap year.", | ||
"source": "CodeRanch Cattle Drive, Assignment 3", | ||
"source_url": "https://coderanch.com/t/718816/Leap" | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/leap/using-gregorian-calendar/expected_analysis.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{"comments": [ | ||
{ | ||
"comment": "java.leap.no_built_in_methods", | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"type": "informative" | ||
} | ||
]} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import java.util.Calendar; | ||
import java.util.GregorianCalendar; | ||
|
||
class Leap { | ||
boolean isLeapYear(int year) { | ||
var calendar = new GregorianCalendar(Calendar.ALL_STYLES); | ||
return calendar.isLeapYear(year); | ||
} | ||
} |
Oops, something went wrong.