HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Generic callback object, but I need the type parameter inside methods

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
generictheneedbutcallbacktypemethodsobjectparameterinside

Problem

Inside my android app, I currently have methods that look like below code.
Since they all need a callback object that basically does the same thing and I would like to try to eliminate the duplicated code seen below.

The object that gets posted via the mBus.post(response); statement needs to be the specific type.

@Subscribe
public void onLeave(LeaveRequest event) {
    mRailsApi.leave(event.getEmail(), event.getPassword(),
            new Callback() {
                @Override
                public void failure(RetrofitError arg0) {
                    LeaveResponse response = new LeaveResponse();
                    response.setSuccessful(false);
                    mBus.post(response);
                }

                @Override
                public void success(LeaveResponse leaveResponse,
                        Response arg1) {
                    // need to create one since the api just returns a
                    // header with no body , hence the response is null
                    LeaveResponse response = new LeaveResponse();
                    response.setSuccessful(true);
                    mBus.post(response);
                }
            });
}

@Subscribe
public void onGetUsers(GetUsersRequest event) {
    mRailsApi.getUsers(mApp.getToken(), new Callback() {
        @Override
        public void failure(RetrofitError arg0) {
            GetUsersResponse response = (GetUsersResponse) arg0.getBody();
            response.setSuccessful(false);
            mBus.post(response);
        }

        @Override
        public void success(GetUsersResponse getUsersResponse, Response arg1) {
            getUsersResponse.setSuccessful(true);
            mBus.post(getUsersResponse);
        }
    });
}


Here is what I have come up with so far. It seems to work but I am wondering if there is a better solution.
One thing that bothers me is, that I have both the type parameter for the class and I am passing in the class to the constructor. It

Solution

One thing that bothers me is, that I have both the type parameter for the class and I am passing in the class to the constructor. It seems like I should not need to pass in the same info in two different ways.

The reason for why you have to do that is because of Type Erasure. Simply put, the generic class is only known during compile-time.

  • This call is unnecessary in your constructor: super();



  • All your fields should be marked with final.



  • Class mType; should be Class mType;



-
I believe the mType.cast is unnecessary here:

mBus.post(mType.cast(response));


simply mBus.post(response); is enough. The eventbus will automatically detect the class by calling obj.getClass() and invoke the appropriate listeners.

In fact, your response variable is already declared as T response; so I don't see what good casting it will do.

Code Snippets

mBus.post(mType.cast(response));

Context

StackExchange Code Review Q#51084, answer score: 6

Revisions (0)

No revisions yet.