001/* 002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved. 003 * 004 * This program and the accompanying materials are made available under the terms of the 005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at 006 * http://www.eclipse.org/legal/epl-v20.html 007 */ 008package net.mdatools.modelant.core.operation.model.order; 009 010import java.util.ArrayList; 011import java.util.List; 012 013import javax.jmi.reflect.RefBaseObject; 014 015import net.mdatools.modelant.core.api.Order; 016 017/** 018 * A composite comparator to order the model elements defined by the order of the nested comparators. 019 * This way any objects that are equal according to comparator i will be additionally compared with comparator i+1 020 * <pre> 021 * Example: 022 * <oderBy> 023 * <orderByClass>...</orderByClass> 024 * <orderByQualifedName>...</orderByQualifedName> 025 * </orderBy> 026 * </pre> 027 * @author Rusi Popov (popovr@mdatools.net) 028 */ 029public class CompositeOrder implements Order { 030 031 private final List<Order> nested = new ArrayList<Order>(); 032 033 /** 034 * List the nested comparators. Their order is significant - the earlier listed comparators 035 * will be applied first. 036 * @param next non-null order to apply 037 */ 038 public void add(Order next) { 039 nested.add( next ); 040 } 041 042 public int compare(RefBaseObject o1, RefBaseObject o2) { 043 int result; 044 045 result = 0; 046 for (int i =0; i < nested.size() && result == 0; i++) { 047 result = nested.get( i ).compare( o1, o2 ); 048 } 049 return result; 050 } 051}