rspec/rules/S4276/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

157 lines
4.9 KiB
Plaintext

== Why is this an issue?
The ``++java.util.function++`` package provides a large array of functional interface definitions for use in lambda expressions and method references. In general it is recommended to use the more specialised form to avoid auto-boxing. For instance ``++IntFunction<Foo>++`` should be preferred over ``++Function<Integer, Foo>++``.
This rule raises an issue when any of the following substitution is possible:
[frame=all]
[cols="^1,^1"]
|===
|Current Interface|Preferred Interface
|``++Function<Integer, R>++``|``++IntFunction<R>++``
|``++Function<Long, R>++``|``++LongFunction<R>++``
|``++Function<Double, R>++``|``++DoubleFunction<R>++``
|``++Function<Double,Integer>++`` | ``++DoubleToIntFunction++``
|``++Function<Double,Long>++`` | ``++DoubleToLongFunction++``
|``++Function<Long,Double>++`` | ``++LongToDoubleFunction++``
|``++Function<Long,Integer>++`` | ``++LongToIntFunction++``
|``++Function<R,Integer>++`` | ``++ToIntFunction<R>++``
|``++Function<R,Long>++`` | ``++ToLongFunction<R>++``
|``++Function<R,Double>++`` | ``++ToDoubleFunction<R>++``
|``++Function<T,T>++`` | ``++UnaryOperator<T>++``
|``++BiFunction<T,T,T>++`` | ``++BinaryOperator<T>++``
|``++Consumer<Integer>++``|``++IntConsumer++``
|``++Consumer<Double>++``|``++DoubleConsumer++``
|``++Consumer<Long>++``|``++LongConsumer++``
|``++BiConsumer<T,Integer>++`` | ``++ObjIntConsumer<T>++``
|``++BiConsumer<T,Long>++`` | ``++ObjLongConsumer<T>++``
|``++BiConsumer<T,Double>++`` | ``++ObjDoubleConsumer<T>++``
|``++Predicate<Integer>++``|``++IntPredicate++``
|``++Predicate<Double>++``|``++DoublePredicate++``
|``++Predicate<Long>++``|``++LongPredicate++``
|``++Supplier<Integer>++``|``++IntSupplier++``
|``++Supplier<Double>++``|``++DoubleSupplier++``
|``++Supplier<Long>++``|``++LongSupplier++``
|``++Supplier<Boolean>++`` | ``++BooleanSupplier++``
|``++UnaryOperator<Integer>++``|``++IntUnaryOperator++``
|``++UnaryOperator<Double>++``|``++DoubleUnaryOperator++``
|``++UnaryOperator<Long>++``|``++LongUnaryOperator++``
|``++BinaryOperator<Integer>++`` | ``++IntBinaryOperator++``
|``++BinaryOperator<Long>++`` | ``++LongBinaryOperator++``
|``++BinaryOperator<Double>++`` | ``++DoubleBinaryOperator++``
|``++Function<T, Boolean>++`` | ``++Predicate<T>++``
|``++BiFunction<T,U,Boolean>++`` | ``++BiPredicate<T,U>++``
|===
=== Noncompliant code example
[source,java]
----
public class Foo implements Supplier<Integer> { // Noncompliant
@Override
public Integer get() {
// ...
}
}
----
=== Compliant solution
[source,java]
----
public class Foo implements IntSupplier {
@Override
public int getAsInt() {
// ...
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor this code to use the more specialised Functional Interface 'xxxx'
=== Highlighting
* Primary: the name of the class to refactor
* Secondary: the interface to replace in the list of implemented interfaces.
** message: 'Replace this interface.'
'''
== Comments And Links
(visible only on this page)
=== on 17 Oct 2017, 10:05:31 Tibor Blenessy wrote:
There are few more substitutions possible
|| Current || Suggestion ||
|``++Function<Double,Integer>++`` | ``++DoubleToIntegerFunction++`` |
|``++Function<Double,Long>++`` | ``++DoubleToLongFunction++`` |
|``++Function<Long,Double>++`` | ``++LongToDoubleFunction++`` |
|``++Function<Long,Integer>++`` | ``++LongToIntFunction++`` |
|``++BiConsumer<T,Integer>++`` | ``++ObjIntConsumer++`` |
|``++BiConsumer<T,Long>++`` | ``++ObjLongConsumer++`` |
|``++BiConsumer<T,Double>++`` | ``++ObjDoubleConsumer++`` |
|``++Supplier<Boolean>++`` | ``++BooleanSupplier++`` |
|``++BinaryOperator<Integer>++`` | ``++IntBinaryOperator++`` |
|``++BinaryOperator<Long>++`` | ``++LongBinaryOperator++`` |
|``++BinaryOperator<Double>++`` | ``++DoubleBinaryOperator++`` |
|``++Function<R,Integer>++`` | ``++ToIntFunction<R>++`` |
|``++Function<R,Long>++`` | ``++ToLongFunction<R>++`` |
|``++Function<R,Double>++`` | ``++ToDoubleFunction<R>++`` |
|``++Function<T,T>++`` | ``++UnaryOperator<T>++`` |
|``++Function<T,T,T>++`` | ``++BinaryOperator<T>++`` |
=== on 9 Apr 2018, 10:05:04 Stylianos Agapiou wrote:
We drop the substitutions regarding the Optional class, since the rule is about Functional Interfaces and they make the rule much more complicated.
We are going to create a new rule to make the underline substitutions possible:
|``++Optional<Integer>++`` | ``++OptionalInt++``|
|``++Optional<Long>++`` | ``++OptionalLong++``|
|``++Optional<Double>++`` | ``++OptionalDouble++``|
=== on 11 Jul 2018, 17:34:28 Tibor Blenessy wrote:
\[~stylianos.agapiou] Did you create RSPEC for the new rule about ``++Optional++``? If not, can you start a discussion about this rule? If yes, can you link the new rule here?
=== on 12 Jul 2018, 09:29:09 Stylianos Agapiou wrote:
\[~tibor.blenessy] No, I didn't create an RSPEC for the rule. I will start a discussion about it. Thanks for the notice.
endif::env-github,rspecator-view[]