Categories: code reformat

This commit is contained in:
Alex Golub 2023-01-20 12:55:56 +02:00
parent cce505f381
commit 46ac54b723
3 changed files with 29 additions and 32 deletions

View File

@ -1,17 +1,17 @@
package com.baeldung.category;
package com.baeldung.category
class BaeldungCategory {
public static String capitalize(String self) {
String capitalizedStr = self;
static String capitalize(String self) {
String capitalizedStr = self
if (self.size() > 0) {
capitalizedStr = self.substring(0, 1).toUpperCase() + self.substring(1);
capitalizedStr = self.substring(0, 1).toUpperCase() + self.substring(1)
}
return capitalizedStr
}
public static double toThePower(Number self, Number exponent) {
return Math.pow(self, exponent);
}
static double toThePower(Number self, Number exponent) {
return Math.pow(self, exponent)
}
}

View File

@ -1,17 +1,15 @@
package com.baeldung.category;
import groovy.lang.Category
package com.baeldung.category
@Category(Number)
class NumberCategory {
public Number cube() {
return this*this*this
Number cube() {
return this**3
}
public int divideWithRoundUp(BigDecimal divisor, boolean isRoundUp) {
int divideWithRoundUp(BigDecimal divisor, boolean isRoundUp) {
def mathRound = isRoundUp ? BigDecimal.ROUND_UP : BigDecimal.ROUND_DOWN
return (int)new BigDecimal(this).divide(divisor, 0, mathRound)
return (int) new BigDecimal(this).divide(divisor, 0, mathRound)
}
}

View File

@ -1,17 +1,17 @@
package com.baeldung.category
import groovy.time.*
import groovy.time.TimeCategory
import groovy.xml.DOMBuilder
import groovy.xml.QName
import groovy.xml.dom.DOMCategory
import java.text.SimpleDateFormat
import groovy.xml.*
import groovy.xml.dom.*
import com.baeldung.category.BaeldungCategory
import com.baeldung.category.NumberCategory
class CategoryUnitTest extends GroovyTestCase {
void test_whenUsingTimeCategory_thenOperationOnDate() {
def jan_1_2019 = new Date("01/01/2019")
use (TimeCategory) {
use(TimeCategory) {
assert jan_1_2019 + 10.seconds == new Date("01/01/2019 00:00:10")
assert jan_1_2019 + 20.minutes == new Date("01/01/2019 00:20:00")
@ -30,7 +30,7 @@ class CategoryUnitTest extends GroovyTestCase {
void test_whenUsingTimeCategory_thenOperationOnNumber() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy")
use (TimeCategory) {
use(TimeCategory) {
assert sdf.format(5.days.from.now) == sdf.format(new Date() + 5.days)
sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss")
@ -57,7 +57,7 @@ class CategoryUnitTest extends GroovyTestCase {
def root = baeldungArticlesDom.documentElement
use (DOMCategory) {
use(DOMCategory) {
assert root.article.size() == 2
def articles = root.article
@ -75,27 +75,26 @@ class CategoryUnitTest extends GroovyTestCase {
assert root.article[2].title.text() == "Metaprogramming in Groovy"
}
}
void test_whenUsingBaeldungCategory_thenCapitalizeString() {
use (BaeldungCategory) {
use(BaeldungCategory) {
assert "norman".capitalize() == "Norman"
}
}
}
void test_whenUsingBaeldungCategory_thenOperationsOnNumber() {
use (BaeldungCategory) {
use(BaeldungCategory) {
assert 50.toThePower(2) == 2500
assert 2.4.toThePower(4) == 33.1776
}
}
void test_whenUsingNumberCategory_thenOperationsOnNumber() {
use (NumberCategory) {
use(NumberCategory) {
assert 3.cube() == 27
assert 25.divideWithRoundUp(6, true) == 5
assert 120.23.divideWithRoundUp(6.1, true) == 20
assert 150.9.divideWithRoundUp(12.1, false) == 12
}
}
}