Skip to content

Commit

Permalink
#12 colors done for CAMC and LCOM
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 19, 2017
1 parent d425c8a commit 6acbe78
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 5 deletions.
91 changes: 91 additions & 0 deletions src/main/java/org/jpeek/metrics/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.metrics;

import org.cactoos.Func;

/**
* Forward colors.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.3
*/
public final class Colors implements Func<Double, String> {

/**
* Low border.
*/
private final double low;

/**
* High border.
*/
private final double high;

/**
* Reverse?
*/
private final boolean reverse;

/**
* Ctor.
* @param left Low border
* @param right High border
*/
public Colors(final double left, final double right) {
this(left, right, false);
}

/**
* Ctor.
* @param left Low border
* @param right High border
* @param rev Reverse
*/
public Colors(final double left, final double right,
final boolean rev) {
this.low = left;
this.high = right;
this.reverse = rev;
}

@Override
public String apply(final Double cohesion) throws Exception {
final String color;
if (cohesion < this.low && !this.reverse
|| cohesion > this.high && this.reverse) {
color = "red";
} else if (cohesion > this.high && !this.reverse
|| cohesion < this.low && this.reverse) {
color = "green";
} else {
color = "yellow";
}
return color;
}

}
11 changes: 10 additions & 1 deletion src/main/java/org/jpeek/metrics/JavassistClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public final class JavassistClasses implements Metric {
*/
private final IoCheckedFunc<CtClass, Double> func;

/**
* Colors.
*/
private final IoCheckedFunc<Double, String> colors;

/**
* Javassist pool.
*/
Expand All @@ -75,11 +80,14 @@ public final class JavassistClasses implements Metric {
* Ctor.
* @param bse The base
* @param fnc Func
* @param clrs Colors
*/
public JavassistClasses(final Base bse, final Func<CtClass, Double> fnc) {
public JavassistClasses(final Base bse, final Func<CtClass, Double> fnc,
final Func<Double, String> clrs) {
this.base = bse;
this.pool = ClassPool.getDefault();
this.func = new IoCheckedFunc<>(fnc);
this.colors = new IoCheckedFunc<>(clrs);
}

@Override
Expand Down Expand Up @@ -148,6 +156,7 @@ private Map.Entry<String, Directives> metric(
.add("class")
.attr("id", ctc.getSimpleName())
.attr("value", String.format("%.4f", cohesion))
.attr("color", this.colors.apply(cohesion))
.up()
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/jpeek/metrics/cohesion/CAMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.cactoos.iterator.Mapped;
import org.jpeek.Base;
import org.jpeek.Metric;
import org.jpeek.metrics.Colors;
import org.jpeek.metrics.JavassistClasses;
import org.xembly.Directive;

Expand Down Expand Up @@ -68,7 +69,11 @@ public CAMC(final Base bse) {

@Override
public Iterable<Directive> xembly() throws IOException {
return new JavassistClasses(this.base, CAMC::cohesion).xembly();
return new JavassistClasses(
this.base, CAMC::cohesion,
// @checkstyle MagicNumberCheck (1 line)
new Colors(0.35d, 0.75d, true)
).xembly();
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/jpeek/metrics/cohesion/LCOM.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javassist.CtClass;
import org.jpeek.Base;
import org.jpeek.Metric;
import org.jpeek.metrics.Colors;
import org.jpeek.metrics.JavassistClasses;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
Expand Down Expand Up @@ -69,7 +70,11 @@ public LCOM(final Base bse) {

@Override
public Iterable<Directive> xembly() throws IOException {
return new JavassistClasses(this.base, LCOM::cohesion).xembly();
return new JavassistClasses(
this.base, LCOM::cohesion,
// @checkstyle MagicNumberCheck (1 line)
new Colors(5.0d, 30.0d, true)
).xembly();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/org/jpeek/jpeek.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ The MIT License (MIT)
<xs:complexType name="class">
<xs:attribute name="id" use="required" type="xs:string"/>
<xs:attribute name="value" use="optional" type="xs:float"/>
<xs:attribute name="color" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="green"/>
<xs:enumeration value="yellow"/>
<xs:enumeration value="red"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="package">
<xs:complexContent>
Expand Down
71 changes: 71 additions & 0 deletions src/test/java/org/jpeek/metrics/ColorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.metrics;

import org.cactoos.Func;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link Colors}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.3
* @checkstyle AbbreviationAsWordInNameCheck (5 lines)
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class ColorsTest {

@Test
public void mapsCohesionToColors() throws Exception {
final Func<Double, String> colors = new Colors(0.5d, 0.7d);
MatcherAssert.assertThat(
colors.apply(1.0d), Matchers.equalTo("green")
);
MatcherAssert.assertThat(
colors.apply(0.1d), Matchers.equalTo("red")
);
MatcherAssert.assertThat(
colors.apply(0.6d), Matchers.equalTo("yellow")
);
}

@Test
public void mapsCohesionToReverseColors() throws Exception {
final Func<Double, String> colors = new Colors(0.5d, 0.7d, true);
MatcherAssert.assertThat(
colors.apply(1.0d), Matchers.equalTo("red")
);
MatcherAssert.assertThat(
colors.apply(0.1d), Matchers.equalTo("green")
);
MatcherAssert.assertThat(
colors.apply(0.6d), Matchers.equalTo("yellow")
);
}

}
3 changes: 2 additions & 1 deletion src/test/java/org/jpeek/metrics/cohesion/CAMCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void createsXmlReportForFixtureClassA() throws IOException {
),
XhtmlMatchers.hasXPaths(
"/app/package[@id='']/class[@id='Foo']",
"//class[@id='Foo' and @value='0.6667']"
"//class[@id='Foo' and @value='0.6667']",
"//class[@id='Foo' and @color='yellow']"
)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/jpeek/metrics/cohesion/LCOMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void createsXmlReportForFixtureClassA() throws IOException {
),
XhtmlMatchers.hasXPaths(
"/app/package/class[@id='Foo']",
"//class[@id='Foo' and @value='1.0000']"
"//class[@id='Foo' and @value='1.0000']",
"//class[@id='Foo' and @color='green']"
)
);
}
Expand Down

0 comments on commit 6acbe78

Please sign in to comment.