/*
 * UniTime 3.1 (University Timetabling Application)
 * Copyright (C) 2008, UniTime.org, and individual contributors
 * as indicated by the @authors tag.
 * 
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.unitime.commons;

import java.util.Collection;

public class MultiComparable implements Comparable<MultiComparable>{
    private Comparable[] iCmp;
    
    public MultiComparable(Comparable[] cmp) {
        iCmp = cmp;
    }
    
    public MultiComparable(Collection cmp) {
        iCmp = new Comparable[cmp.size()];
        int idx = 0;
        for (Object c : cmp)
            iCmp[idx++] = (Comparable)c;
    }
    
    public MultiComparable(Comparable a) {
        iCmp = new Comparable[] {a};
    }
    
    public MultiComparable(Comparable a, Comparable b) {
        iCmp = new Comparable[] {a, b};
    }

    public MultiComparable(Comparable a, Comparable b, Comparable c) {
        iCmp = new Comparable[] {a, b, c};
    }

    public MultiComparable(Comparable a, Comparable b, Comparable c, Comparable d) {
        iCmp = new Comparable[] {a, b, c, d};
    }

    public MultiComparable(Comparable a, Comparable b, Comparable c, Comparable d, Comparable e) {
        iCmp = new Comparable[] {a, b, c, d, e};
    }
    
    public MultiComparable(Comparable a, Comparable b, Comparable c, Comparable d, Comparable e, Comparable f) {
        iCmp = new Comparable[] {a, b, c, d, e, f};
    }

    public MultiComparable(Comparable a, Comparable b, Comparable c, Comparable d, Comparable e, Comparable f, Comparable g) {
        iCmp = new Comparable[] {a, b, c, d, e, f, g};
    }

    public int compareTo(MultiComparable mc) {
        for (int i=0;i<Math.min(iCmp.length,mc.iCmp.length);i++) {
            if (iCmp[i]==null) {
                if (mc.iCmp[i]==null) continue;
                return -1;
            }
            if (mc.iCmp[i]==null) return 1;
            int cmp = iCmp[i].compareTo(mc.iCmp[i]);
            if (cmp!=0) return cmp;
        }
        if (iCmp.length>mc.iCmp.length) return 1;
        if (iCmp.length<mc.iCmp.length) return -1;
        return 0;
    }
}

@@ -933,16 +933,45 @@
 lection;
+%0Aimport java.util.Comparator;
 %0A%0Apublic
@@ -1060,24 +1060,90 @@
 ble%5B%5D iCmp;%0A
+    private Comparator iStringCmp = new NaturalOrderComparator();%0A
     %0A    pub
@@ -2678,16 +2678,233 @@
 nt cmp =
+ 0;%0A            if (iCmp%5Bi%5D!=null && iCmp%5Bi%5D instanceof String && mc.iCmp%5Bi%5D!=null && mc.iCmp%5Bi%5D instanceof String)%0A                cmp = iStringCmp.compare(iCmp%5Bi%5D, mc.iCmp%5Bi%5D);%0A            else%0A                cmp =
  iCmp%5Bi%5D

use NaturalOrderComparator for string comparisons