Rohit Bajaj
asked on
Avoiding code duplicacy
Hi,
I have the following functions in my code :
The following piece of code is duplicated in it :
what way i can avoid this duplicacy ?
Thanks
I have the following functions in my code :
public ListenableFuture<MessagesAroundSidResponse> messagesAroundSid(final MessagesAroundSidRequest request)
{
final SettableFuture<MessagesAroundSidResponse> settableFuture = SettableFuture.create();
_httpClient.processRequest(new HttpRequest(request.getRequestBody(), request.getRequestSpecificPath(), request.getUrlParams(), request.isMethodPost(), request.getTenantVersion(), new IResponseCallback()
{
@Override
public void onSuccess(String responseBody, int statusCode)
{
if (statusCode >= 200 && statusCode <= 299)
{
System.out.println(responseBody);
settableFuture.set(request.consumeResponse(responseBody));
}
else
{
settableFuture.setException(request.consumeError(statusCode, responseBody));
}
}
@Override
public void onFailure(Throwable throwable)
{
settableFuture.setException(throwable);
}
}));
return settableFuture;
}
public ListenableFuture<PreviousFetchResponse> PreviousChats(final PreviousFetchRequest request)
{
final SettableFuture<PreviousFetchResponse> settableFuture = SettableFuture.create();
_httpClient.processRequest(new HttpRequest(request.getRequestBody(), request.getRequestSpecificPath(), request.getUrlParams(), request.isMethodPost(), request.getTenantVersion(), new IResponseCallback()
{
@Override
public void onSuccess(String responseBody, int statusCode)
{
if (statusCode >= 200 && statusCode <= 299)
{
System.out.println("Iniside PreviousChat");
settableFuture.set(request.consumeResponse(responseBody));
}
else
{
settableFuture.setException(request.consumeError(statusCode, responseBody));
}
}
@Override
public void onFailure(Throwable throwable)
{
settableFuture.setException(throwable);
}
}));
return settableFuture;
}
The following piece of code is duplicated in it :
@Override
public void onSuccess(String responseBody, int statusCode)
{
if (statusCode >= 200 && statusCode <= 299)
{
System.out.println("Iniside PreviousChat");
settableFuture.set(request.consumeResponse(responseBody));
}
else
{
settableFuture.setException(request.consumeError(statusCode, responseBody));
}
}
@Override
public void onFailure(Throwable throwable)
{
settableFuture.setException(throwable);
}
}));
return settableFuture;
what way i can avoid this duplicacy ?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
That removed code duplicacy to a great extent. But now i am left with the following piece of code which also seems like duplicacy
:
Open in new window
I tried reducing the above all code to the following function :
Open in new window
Although the above code is running which is bit surprise to me as
how is the following line getting instantiated :
final SettableFuture<T> settableFuture = SettableFuture.create();
As i dont provide the value of T in the parameters or anywhere.
I executed the code with the following line :
ListenableFuture<FetchMess