Java tutorial
/*************************************************************************** **** * Copyright (c) 2013 GigaSpaces Technologies Ltd. and 123Completed Ltd. All rights reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **************************************************************************** ***/ package com.c123.billbuddy.dal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openspaces.remoting.RemoteResultReducer; import org.openspaces.remoting.SpaceRemotingInvocation; import org.openspaces.remoting.SpaceRemotingResult; import com.c123.billbuddy.model.Merchant; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * {@link RemoteResultReducer} object used to reduce the results * returned from the space. * * @author 123Completed */ public class Top5MerchantFeeAmountReducer implements RemoteResultReducer<Merchant[], Merchant[]> { private final Log log = LogFactory.getLog(Top5MerchantFeeAmountReducer.class); public Merchant[] reduce(SpaceRemotingResult<Merchant[]>[] results, SpaceRemotingInvocation remotingInvocation) throws Exception { log.info("Starting Top5MerchantFeeAmountReducer"); List<Merchant> merchants = new ArrayList<Merchant>(); // Each result is an array of events. Each result is from a single partition. for (SpaceRemotingResult<Merchant[]> result : results) { if (result.getException() != null) { // just log the fact that there was an exception log.error("Executor Remoting Exception [" + result.getException() + "]"); continue; } Collections.addAll(merchants, result.getResult()); } Collections.sort(merchants, new Comparator<Merchant>() { public int compare(Merchant p1, Merchant p2) { return p2.getFeeAmount().compareTo(p1.getFeeAmount()); } }); // If the number of results needed is less than the number of events that were reduced, then // return a sublist. Otherwise, return the entire list of events. Merchant[] top5Merchant; if (merchants.size() < 5) { top5Merchant = new Merchant[merchants.size()]; merchants.toArray(top5Merchant); } else { top5Merchant = new Merchant[5]; merchants.subList(0, 5).toArray(top5Merchant); } return top5Merchant; } }