Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-4276

Functional Interfaces should be as specialised as possible

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Refactor this code to use the more specialised Functional Interface 'xxxx'
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Irrelevant for Languages:
      ABAP, C#, C, C++, Cobol, CSS, Flex, HTML, JavaScript, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      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:

      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

      public class Foo implements Supplier<Integer> {  // Noncompliant
          @Override
          public Integer get() {
            // ...
          }
      }
      

      Compliant Solution

      public class Foo implements IntSupplier {
      
        @Override
        public int getAsInt() {
          // ...
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                jeanchristophe.collet Jean-Christophe Collet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: