-
Notifications
You must be signed in to change notification settings - Fork 0
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
Muntazir Fadhel
committed
Nov 11, 2024
1 parent
5bdc411
commit befa52e
Showing
16 changed files
with
563 additions
and
383 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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/hadii/striff/metrics/AfferentCouplingMetric.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,42 @@ | ||
package com.hadii.striff.metrics; | ||
|
||
import com.hadii.clarpse.reference.ComponentReference; | ||
import com.hadii.clarpse.sourcemodel.Component; | ||
import com.hadii.clarpse.sourcemodel.OOPSourceCodeModel; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Represents the Afferent Class Coupling metric, which is a measure of the | ||
* number of classes that reference the given class. | ||
*/ | ||
public class AfferentCouplingMetric implements Metric { | ||
|
||
private final Map<String, Double> references = new HashMap<>(); | ||
|
||
public AfferentCouplingMetric(OOPSourceCodeModel srcModel) { | ||
this.calculate(srcModel); | ||
} | ||
|
||
private void calculate(OOPSourceCodeModel srcModel) { | ||
for (Component tempCmp : srcModel.components().collect(Collectors.toList())) { | ||
if (tempCmp.componentType().isBaseComponent()) { | ||
for (ComponentReference cmpRef : tempCmp | ||
.references()) { | ||
references.put(cmpRef.invokedComponent(), | ||
references.getOrDefault(cmpRef.invokedComponent(), 0.0) + 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public double value(String cmpId) { | ||
return this.references.getOrDefault(cmpId, 0.0); | ||
} | ||
|
||
@Override | ||
public String acronym() { | ||
return "AC"; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,46 +1,46 @@ | ||
package com.hadii.striff.metrics; | ||
|
||
import com.hadii.clarpse.reference.ComponentReference; | ||
import com.hadii.clarpse.sourcemodel.Component; | ||
import com.hadii.clarpse.sourcemodel.OOPSourceCodeModel; | ||
import com.hadii.clarpse.sourcemodel.OOPSourceModelConstants; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Represents a Depth of Inheritance metric, which is a measure of how far down a class is declared in | ||
* the inheritance hierarchy. | ||
* Represents a Depth of Inheritance metric, which is a measure of how far down | ||
* a class is declared in the inheritance hierarchy. | ||
*/ | ||
public class DITMetric implements Metric { | ||
|
||
private final double value; | ||
private final OOPSourceCodeModel srcModel; | ||
|
||
public DITMetric(Component component, OOPSourceCodeModel srcModel) { | ||
public DITMetric(OOPSourceCodeModel srcModel) { | ||
this.srcModel = srcModel; | ||
this.value = calculateDit(component); | ||
} | ||
|
||
private int calculateDit(Component cmp) { | ||
if (cmp.componentType() == OOPSourceModelConstants.ComponentType.INTERFACE) { | ||
return 1; | ||
} | ||
|
||
int currentHighestScore = 1; | ||
|
||
for (ComponentReference cmpRef : cmp.references(OOPSourceModelConstants.TypeReferences.EXTENSION)) { | ||
Optional<Component> invkCmp = this.srcModel.getComponent(cmpRef.invokedComponent()); | ||
if (invkCmp.isPresent()) { | ||
int hierarchyScore = 1 + calculateDit(invkCmp.get()); | ||
if (hierarchyScore > currentHighestScore) { | ||
currentHighestScore = hierarchyScore; | ||
} | ||
} | ||
private Double calculateDit(String cmpId) { | ||
double currentHighestScore = 1; | ||
Optional<Component> componentOpt = this.srcModel.getComponent(cmpId); | ||
// Calculate the highest score if the component is present and not an interface | ||
if (componentOpt.isPresent() | ||
&& componentOpt.get().componentType() != OOPSourceModelConstants.ComponentType.INTERFACE) { | ||
currentHighestScore = componentOpt.get() | ||
.references(OOPSourceModelConstants.TypeReferences.EXTENSION).stream() | ||
.map(cmpRef -> this.srcModel.getComponent(cmpRef.invokedComponent())) | ||
.filter(Optional::isPresent) | ||
.mapToDouble(invkCmp -> 1.0 + calculateDit(invkCmp.get().uniqueName())) | ||
.max() | ||
.orElse(currentHighestScore); | ||
} | ||
return currentHighestScore; | ||
} | ||
|
||
public double value() { | ||
return this.value; | ||
public double value(String cmpUniqueName) { | ||
return this.calculateDit(cmpUniqueName); | ||
} | ||
|
||
@Override | ||
public String acronym() { | ||
return "DIT"; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/hadii/striff/metrics/EfferentCouplingMetric.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,33 @@ | ||
package com.hadii.striff.metrics; | ||
|
||
import com.hadii.clarpse.sourcemodel.Component; | ||
import com.hadii.clarpse.sourcemodel.OOPSourceCodeModel; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Represents the Efferent Class Coupling metric, which is a measure of the | ||
* number of other classes referenced by a class. | ||
*/ | ||
public class EfferentCouplingMetric implements Metric { | ||
|
||
private final OOPSourceCodeModel srcModel; | ||
|
||
public EfferentCouplingMetric(OOPSourceCodeModel srcModel) { | ||
this.srcModel = srcModel; | ||
} | ||
|
||
public double value(String cmpId) { | ||
Optional<Component> componentOpt = this.srcModel.getComponent(cmpId); | ||
if (componentOpt.isPresent()) { | ||
return componentOpt.get().references().size(); | ||
} else { | ||
throw new IllegalArgumentException("Could not find component: " + cmpId + "!"); | ||
} | ||
} | ||
|
||
@Override | ||
public String acronym() { | ||
return "EC"; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/hadii/striff/metrics/EncapsulationMetric.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,45 @@ | ||
package com.hadii.striff.metrics; | ||
|
||
import com.hadii.clarpse.sourcemodel.Component; | ||
import com.hadii.clarpse.sourcemodel.OOPSourceCodeModel; | ||
|
||
/** | ||
* The Encapsulation Ratio measures how well a class hides its data, | ||
* focusing on the accessibility of its fields and methods. A higher | ||
* encapsulation ratio suggests better data hiding, which is a core | ||
* principle of OOP. | ||
*/ | ||
public class EncapsulationMetric implements Metric { | ||
|
||
private OOPSourceCodeModel srcModel; | ||
|
||
public EncapsulationMetric(OOPSourceCodeModel srcModel) { | ||
this.srcModel = srcModel; | ||
} | ||
|
||
public double value(String cmpId) { | ||
if (srcModel.containsComponent(cmpId)) { | ||
int privateMembers = 0; | ||
Component currComponent = srcModel.getComponent(cmpId).get(); | ||
// Iterate over children to count private/protected modifiers | ||
for (String child : currComponent.children()) { | ||
if (srcModel.containsComponent(child)) { | ||
Component childCmp = srcModel.getComponent(child).get(); | ||
if (childCmp.modifiers().contains("private") || childCmp.modifiers().contains("protected")) { | ||
privateMembers++; | ||
} | ||
} | ||
} | ||
// Avoid division by zero if there are no children | ||
int totalChildren = currComponent.children().size(); | ||
return totalChildren > 0 ? (double) privateMembers / totalChildren : 0.0; | ||
} else { | ||
throw new IllegalArgumentException("Could not find component: " + cmpId + "!"); | ||
} | ||
} | ||
|
||
@Override | ||
public String acronym() { | ||
return "ENC"; | ||
} | ||
} |
Oops, something went wrong.