Roman
Roman

Reputation: 8231

Transform collections to client side in GWT

The following code is a GWT RPC serlvet implementation , the transformed collection fails in the client obviously since the since it is not GWT compatible.

Any soutions inside Guava that I am missing ?

@Singleton
public class DataServiceImpl extends RemoteServiceServlet implements
        DataService {

    @Inject
    ApplicationDao dao;

    @Inject
    DtoUtil dtoUtil;

    public Collection<GoalDto> getAllConfiguredGoals() {
        return Collections2.transform(dao.getAllGoals(), new Function<Goal, GoalDto>() {
            public GoalDto apply(@Nullable Goal goal) {
                return dtoUtil.toGoalDto(goal);
            }
        });
    }

}

I am looking for a native guava solution , not some hand written translation code.

Upvotes: 0

Views: 497

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47608

The problem with guava in this case is that it uses Lazy-evaluation (which is often good, but not here) and the collection is backed-up by the original collection. The only way out would be to force a new collection which is not backed-up by original objects and where all evaluations have been performed. Something like this should do the trick (assuming that GoalDto is GWT-serializable):

return new ArrayList<GoalDto>(Collections2.transform(dao.getAllGoals(), new Function<Goal, GoalDto>() {
        public GoalDto apply(@Nullable Goal goal) {
            return dtoUtil.toGoalDto(goal);
        }
    }));

Upvotes: 4

Related Questions