0

I've tried to make my code use generics, but I can't seem to get it to work using generics. Any help would be appreciated.

I have 3 classes: Classroom, Course, Teacher

I have the following working code 3 times: (With the small change of the class)

private ObservableList<Classroom> parseClassrooms() {
    // create new Observable List
    ObservableList<Classroom> classrooms = FXCollections.observableArrayList();

    // get lines from file;
    ArrayList<String> arrayList = fhClassroom.read();

    for (String line : arrayList) {
        classrooms.add(Classroom.fromString(line));
    }

    return classrooms;
}

Methods in my Classes:

@Override
public String toString() {
    return name;
}

public static Classroom fromString(String line) {
    return new Classroom(line);
}

Is it possible to make this method generic? and pass the class as parameter?

I would like something like the following:

private ObservableList<T> parseClassrooms(T, FileHelper fh) {
    // create new Observable List
    ObservableList<T> items = FXCollections.observableArrayList();

    // get lines from file;
    ArrayList<String> arrayList = fh.read();

    for (String line : arrayList) {
        items.add(T.fromString(line));
    }

    return items;
}
Halfacht
  • 924
  • 1
  • 12
  • 22
  • Is `Classroom.fromString(line)` a static method? – Eran Oct 22 '18 at 12:10
  • Possible duplicate of [How do I use reflection to call a generic method?](https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) – Sarah Oct 22 '18 at 12:17
  • The method fromString() in your Classroom class has to be static or else you have to instantiate inside the class you are using it. – Paris Karagiannopoulos Oct 22 '18 at 12:32
  • @Sarah I know there are many questions that look a lot like this one. But I don't get how to implement them, or the one you send, into my situation. – Halfacht Oct 22 '18 at 12:46
  • Define "I can't seem to get it to work" precisely. What are you doing? What do you expect to happen? What happens instead? Include every command and complete and exact error, if any. – JB Nizet Oct 22 '18 at 12:46
  • @JBNizet The method returns a ObservableArrayList of type Classroom. It reads the classrooms from a file and fills the array and then returns it. There are no errors, because I'm not using generics in this version, because I don't know how that works. – Halfacht Oct 22 '18 at 13:00

1 Answers1

2

My best attempt:

import java.util.ArrayList;
import java.util.function.Function;

public class Helper {

    public static <T> ObservableList<T> parseItems(Function<String, T> lineToItemFunction, FileHelper fh) {
        // create new Observable List
        ObservableList<T> items = FXCollections.observableArrayList();

        // get lines from file;
        ArrayList<String> arrayList = fh.read();

        for (String line : arrayList) {
            items.add(lineToItemFunction.apply(line));
        }

        return items;
    }
}

And you call it this way:

ObservableList<ClassRoom> classRooms = Helper.parseItems(ClassRoom::fromLine, fileHelper);
Benoit
  • 5,118
  • 2
  • 24
  • 43