user3448331
user3448331

Reputation: 63

Java CallBacks - Grabbing the argument

I have

public interface CallBack<T> {

public interface Callback<T> {
   public void callback(T t);
}

public void isRegistered(final String username, final Callback<Boolean> callback){
PreparedStatement perparedStatement = connection.prepareStatement("SELECT name FROM " + table + "     WHERE name = ?");
                    perparedStatement.setString(1, username);
                    if(perparedStatement.executeQuery().next()){
                        perparedStatement.close();
                        registeredCache.put(username, true);
                    }else{
                        perparedStatement.close();
                        registeredCache.put(username, false);
                    }
                    usingMySQL--;
                    if(usingMySQL == 0){
                        closeConnection();
                    }
                    callback.callback(registeredCache.get(registeredCache.get(username)));

But I'm falling to find out how to access the argument. First time with call backs.

    Lobby.instance.MySQL.isRegistered(sender.getName(), new CallBack<Boolean>(){

                        @Override
                        public void CallBack(){

                        }
                    });

My buddy attempted to explain it but he said he's too bad at explaining it and asked me to ask here. The interface is in a different class to the public void method.

Upvotes: 0

Views: 48

Answers (1)

Ashley Frieze
Ashley Frieze

Reputation: 5443

If the interface is:

public interface Callback<T> {
   public void callback(T t);
}

Then the only way to implement it would be to produce a class which implements the callback method. Let's say that the type parameter were Boolean, then the override would be

Callback<Boolean> callback = new Callback<Boolean>() {
    @Override
    public void callback(Boolean parameter) { 
        // your code here
    }
};

Upvotes: 2

Related Questions