Jump to content

generalize a class/function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
I have a set of files with arraylists in it, in the arraylists are objects

I have a file with an arraylist based on the class Car for example

but I also have files with an arraylist based on the class Driver, and other files like that

I use following code to load such an arraylist:

import java.io.*;

import java.util.*;


public class FileInterface {


    private ArrayList<Car> cars;


    // The name of the file where the highcar will be saved

    private static final String FILE = "cars.dat";


    //Initialising an in and outputStream for working with the file

    ObjectOutputStream outputStream = null;

    ObjectInputStream inputStream = null;


    public FileInterface() {

        //initialising the car-arraylist

        cars = new ArrayList<Car>();

    }


    public void loadScoreFile() {

        try {

            inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));

            cars = (ArrayList<Car>) inputStream.readObject();

        } catch (FileNotFoundException e) {

            System.out.println("[Load] FNF Error: " + e.getMessage());

        } catch (IOException e) {

            System.out.println("[Load] IO Error: " + e.getMessage());

        } catch (ClassNotFoundException e) {

            System.out.println("[Load] CNF Error: " + e.getMessage());

        } finally {

            try {

                if (outputStream != null) {

                    outputStream.flush();

                    outputStream.close();

                }

            } catch (IOException e) {

                System.out.println("[Laod] IO Error: " + e.getMessage());

            }

        }

    }

}

is there a way that I can make the loadfunction work for every type of arraylist? (ArrayList<Driver>, ArrayList<Car>, ...)

it seems stupid to put that piece of code multiple times just because it has to load a different kind of ArrayList

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
class ArrayHandler<T> {
       privare ArrayList<T> t;
}

Look into generics. This is what this is. :)

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Yes, you make the loading class generic. In fact, you don't even have to use ArrayList's specifically, you can set it up to use any list that may be saved in the data file. Your constructor should first accept a string as the file needing to be opened, and you'll need to set it's generic arguments:
import java.io.*;
import java.util.*;

public class FileInterface<T>
{
    private List<T> cars;

    // The name of the file where the highcar will be loaded
    private String FILE;

    public List<T> getList()
    {
        return cars;
    }

    public FileInterface(String fileName)
    {
        // set the FILE string to the input string.
        FILE = fileName;
    }

    public void loadScoreFile() throws IOException, FileNotFoundException
    {
        try
        {
            // What was HIGHSCORE_FILE?
            ObjectInputStream inputStream
                        = new ObjectInputStream(new FileInputStream(FILE));
            // I wouldn't do this at all... I'd allocate the List as a new ArrayList, then pull
            // out each T object individually. Also readObject can throw other errors.
            cars = (List<T>) inputStream.readObject();
        }
        catch (ClassNotFoundException e)
        {
            // FIXME: Do something better with this... like at least return NULL.
            System.out.println("[Load] CNF Error: " + e.getMessage());
        }
        finally
        {
            // Unless you find you REALLY need a try statement in a
            // finally block, don't do it.
            if (inputStream != null)
            {
                inputStream.close();
            }
        }
    }
}
I'll admit I didn't look it over too well, but that's the gist of what I'd do. You can improve the class a bit, too. That should work for both Cars and Drivers, like so:
FileInterface<Cars> myCars = new FileInterface<Cars>("cars.dat");
FileInterface<Drivers> myDrivers = new FileInterface<Drivers>("drivers.dat);
List<Cars> carList = myCars.getList();
List<Drivers> driverList = myDrivers.getList();

Wow I changed my sig!