Szanownego Szanownego - 2 months ago 10
Java Question

How to manage immutable class with LinkedList as an instance field?

I have an immutable class called

Employees
like this:

public final class Employees {
private final List<Person> persons;

public Employees() {
persons = new LinkedList<Person>();
}

public List<Person> getPersons() {
return persons;
}
}


How do I keep this class immutable?

I made the field
private
and
final
, and I didn't provide a setter method. Is this enough to achieve immutability?

Answer

Answer edited to explain not only the case with a mutable version of Person, but also with an immutable version of Person.


Your class is mutable because you can do that:

Employees employees = new Employees();
employees.getPersons().add(new Person());

Note that not passing a list of persons to the constructor if you change your code to create an immutable class you will have a not useful class holding an ever empty list of persons, so I assume that is necessary to pass a List<Person> to the constructor.

Now there are two scenarios, with different implementations:

  • Person is immutable
  • Person is mutable

Scenario 1 - Person is immutable

You need only to create an immutable copy of the persons parameter in the constructor.

You need also to make final the class or at least the method getPersons to be sure that nobody provide a mutable overwritten version of the getPersons method.

public final class Employees {
    private final List<Persons> persons;

    public Employees(List<Person> persons) {
        persons =  Collections.unmodifiableList( persons);
    }

    public List<Employees> getPersons() {
        return persons;
    }
}

Scenario 2 - Person is mutable

You need to create a deep copy of persons in the getPersons method.

You need to create a deep copy of persons on the constructor.

You need also to make final the class or at least the method getPersons to be sure that nobody provide a mutable overwritten version of the getPersons method.

public final class Employees {
    private final List<Persons> persons;

    public Employees(List<Person> persons) {
        persons = new ArrayList<>();
        for (Person person : persons) {
            persons.add(deepCopy(person));   // If clone is provided 
                                           // and creates a deep copy of person
        }
    }

    public List<Employees> getPersons() {
        List<Person> temp = new ArrayList<>();
        for (Person person : persons) {
            temp.add(deepCopy(person)); // If clone is provided 
                                         // and creates a deep copy of person
        }  
        return temp;
    }

    public Person deepCopy(Person person) {
        Person copy = new Person();  
        // Provide a deep copy of person
        ...
        return copy;
    }
}

This part of the answer is to show why a not deep copy of the personsParameter passed to the constructor can create mutable versions of Employees:

List<Person> personsParameter = new ArrayList<>();
Person person = new Person();
person.setName("Pippo");
personsParameter.add(person);
Employees employees = new Employees(personsParameter);

// Prints Pippo    
System.out.println(employees.getPersons().get(0).getName()); 


employees.getPersons().get(0).setName("newName");

// Prints again Pippo    
System.out.println(employees.getPersons().get(0).getName()); 

// But modifiyng something reachable from the parameter 
// used in the constructor 
person.setName("Pluto");

// Now it prints Pluto, so employees has changed    
System.out.println(employees.getPersons().get(0).getName());