001    /*
002    @license.text@
003     */
004    package biz.hammurapi.legacy.review;
005    
006    import java.util.Comparator;
007    
008    /**
009     * @author Pavel Vlasov
010     * @version $Revision: 1.3 $
011     */
012    public class SourceMarkerComparator implements Comparator {
013    
014            public static int _compare(Object o1, Object o2) {
015                    if (o1==null) {
016                            return o2==null ? 0 : 1;
017                    } else if (o2==null) {
018                            return -1;
019                    } else if (o1 instanceof SourceMarker && o2 instanceof SourceMarker) {
020                            SourceMarker sm1=(SourceMarker) o1;
021                            SourceMarker sm2=(SourceMarker) o2;
022                            int compareIds=_compare(sm1.getSourceId(), sm2.getSourceId());
023                            if (compareIds==0) {
024                                    int compareSourceURLs=_compare(sm1.getSourceURL(), sm2.getSourceURL());
025                                    if (compareSourceURLs==0) {
026                                            return comparePosition(sm1, sm2);
027                                    } else {
028                                            return compareSourceURLs;
029                                    }
030                            } else {
031                                    return compareIds;
032                            }
033                    } else if (o1 instanceof Comparable) {
034                            return ((Comparable) o1).compareTo(o2);
035                    } else {
036                            throw new UnsupportedOperationException("Comparision of objects other than SourceMarker is not supported");
037                    }
038            }
039            
040    
041            private static int comparePosition(SourceMarker sm1, SourceMarker sm2) {
042                    if (sm1.getLine()==sm2.getLine()) {
043                            return sm1.getColumn()-sm2.getColumn();
044                    } else {
045                            return sm1.getLine()-sm2.getLine();
046                    }
047            }
048    
049            public int compare(Object o1, Object o2) {
050                    return _compare(o1, o2);
051            }
052    }