简体   繁体   中英

What is the concrete class that has implemented the hasMoreElements() that belongs to the Enumeration Interface?

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

class FileEnumerator implements Enumeration<InputStream> {

    private Enumeration<String> files;

    public FileEnumerator(Vector<String> files) {
        this.files = files.elements();
    }

    @Override
    public boolean hasMoreElements() {
        return this.files.hasMoreElements(); //  ------- >>>> This Method ??
    }

    @Override
    public InputStream nextElement() {
        try {
            return new FileInputStream(this.files.nextElement().toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

public class App {
    public static void main(String args[]) throws IOException {
        Vector<String> files = new Vector<>();
        files.add("file1.txt");
        files.add("file2.txt");
        files.add("file3.txt");
        SequenceInputStream sequenceInputStream = new SequenceInputStream(
                new FileEnumerator(files));
        int c;
        while ((c = sequenceInputStream.read()) != -1) {
            System.out.print((char)c);
        }
        sequenceInputStream.close();
    }
}

I am totally confused .. I don't know where the hasMoreElement() method in Enumeration interface has been implemented to return true or false .. since in this program didn't include it's only concrete class which is StringTTokenizer http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html

Also there is not such a method stand alone in Vector class ..

I just don't get it ...

Here is the overridden method in Vector class.

public boolean hasMoreElements() {
            return count < elementCount;
}

You called files.elements(); The method that I mentioned above, is in this file.elements() method.

Hope this helps

I believe you're questioning this,

@Override
public boolean hasMoreElements() {
    return this.files.hasMoreElements(); //  ------- >>>> This Method ??
}

Seems like an example of the Decorator pattern on the Vector#elements() Enumeration (and that is the concrete implementation you are looking for).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM