خرید بک لینک

Vote count: 0

I am new to Vertx.

I am playing with the API and I am trying to write a FileSizeHandler. I don't know if it is the correct way to do it but I would like to have your opinions.

In my code I would like to use the handler like this :

    public class MyVerticle extends AbstractVerticle {

            @Override
            public void start() throws Exception {
                getFileSize("./my_file.txt", event -> {
                 if(event.succeeded()){
                       Long result = event.result();
                       System.out.println("FileSize is " + result);
                } else {
                     System.out.println(event.cause().getLocalizedMessage());
                }
        });

     }  

    private void getFileSize(String filepath, Handler<AsyncResult<Long>> resultHandler){
        resultHandler.handle(new FileSizeHandler(filepath));
    }
}

Here is my FileSizeHandler class :

public class FileSizeHandler implements AsyncResult<Long> {

    private boolean isSuccess;
    private Throwable cause;
    private Long result;

    public FileSizeHandler(String filePath){
        cause = null;
        isSuccess = false;
        result = 0L;

        try {
            result = Files.size(Paths.get(filePath));
            isSuccess = !isSuccess;
        } catch (IOException e) {
            cause = e;
        }

    }

    @Override
    public Long result() {
        return result;
    }

    @Override
    public Throwable cause() {
        return cause;
    }

    @Override
    public boolean succeeded() {
        return isSuccess;
    }

    @Override
    public boolean failed() {
        return !isSuccess;
    }
}

What bothers me in the handler, is that I have to do it in the constructor of the class. Is there a better way to do it?

asked 7 secs ago

برچسب: how to write a check,how to write a cover letter,how to write a resume,how to write a letter,how to write a song,how to write a book,how to write a business plan,how to write an abstract,how to write a poem,how to write an essay, نویسنده: استخدام کار تاريخ: دوشنبه 25 مرداد 1395 ساعت: 18:40

صفحه بندی