rspec/rules/S1319/rule.adoc

40 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-06-30 12:47:33 +02:00
The purpose of the Java Collections API is to provide a well defined hierarchy of interfaces in order to hide implementation details.
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
Implementing classes must be used to instantiate new collections, but the result of an instantiation should ideally be stored in a variable whose type is a Java Collection interface.
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
This rule raises an issue when an implementation class:
* is returned from a ``++public++`` method.
* is accepted as an argument to a ``++public++`` method.
* is exposed as a ``++public++`` member.
2020-06-30 12:47:33 +02:00
== Noncompliant Code Example
----
public class Employees {
private HashSet<Employee> employees = new HashSet<Employee>(); // Noncompliant - "employees" should have type "Set" rather than "HashSet"
public HashSet<Employee> getEmployees() { // Noncompliant
return employees;
}
}
----
== Compliant Solution
----
public class Employees {
private Set<Employee> employees = new HashSet<Employee>(); // Compliant
public Set<Employee> getEmployees() { // Compliant
return employees;
}
}
----