package org.activityinfo.legacy.shared.impl.pivot.order; /* * #%L * ActivityInfo Server * %% * Copyright (C) 2009 - 2013 UNICEF * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import org.activityinfo.legacy.shared.reports.content.DimensionCategory; import org.activityinfo.legacy.shared.reports.content.PivotTableData; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class DefinedCategoryComparator implements Comparator<PivotTableData.Axis> { private final Map<DimensionCategory, Integer> orderMap; public DefinedCategoryComparator(List<DimensionCategory> order) { orderMap = new HashMap<DimensionCategory, Integer>(); for (int i = 0; i != order.size(); ++i) { orderMap.put(order.get(i), i); } } @Override public int compare(PivotTableData.Axis a1, PivotTableData.Axis a2) { Integer o1 = orderMap.get(a1.getCategory()); Integer o2 = orderMap.get(a2.getCategory()); if (o1 == null) { o1 = Integer.MAX_VALUE; } if (o2 == null) { o2 = Integer.MAX_VALUE; } return o1.compareTo(o2); } }