Skip to content

Commit 4f97eed

Browse files
E000391dedece35
authored andcommitted
clean code
1 parent feec689 commit 4f97eed

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118
package fr.greencodeinitiative.java.checks;
219

320
import org.sonar.check.Rule;
@@ -6,15 +23,12 @@
623
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
724
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
825
import org.sonar.plugins.java.api.tree.Tree;
9-
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
10-
1126
import javax.annotation.Nonnull;
1227
import java.util.Collections;
1328
import java.util.List;
1429
import java.util.Objects;
1530

16-
@Rule(key = "XXX")
17-
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "XXX")
31+
@Rule(key = "EC1369")
1832
public class UseOptionalOrElseGetVsOrElse extends IssuableSubscriptionVisitor {
1933

2034
private static final String MESSAGE_RULE = "Use optional orElseGet instead of orElse.";
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118
class UseOptionalOrElseGetVsOrElse {
219

320
public static final String name = Optional.of("ecoCode").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
421

5-
public static final String name = Optional.of("ecoCode").orElseGet(getUnpredictedMethod()); // Compliant
22+
public static final String name = Optional.of("ecoCode").orElseGet(() -> getUnpredictedMethod()); // Compliant
623

7-
public static final String name = randomClass.orElse(); // Compliant
24+
public static final String name = randomClass.orElse(getUnpredictedMethod()); // Compliant
825
}

src/test/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118
package fr.greencodeinitiative.java.checks;
219

320
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)