001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.util;
025    
026    public class Sortable<C extends Comparable<C>, O> implements Comparable<Sortable<C, O>> {
027    
028            private C comparable = null;
029    
030            private O object = null;
031    
032            private boolean compareObject = false;
033    
034            public Sortable(C comparable, O object) {
035                    this.comparable = comparable;
036                    this.object = object;
037            }
038    
039            public Sortable(C comparable, O object, boolean compareObject) {
040                    this.comparable = comparable;
041                    this.object = object;
042                    this.compareObject = compareObject;
043            }
044    
045            public C getComparable() {
046                    return comparable;
047            }
048    
049            public void setComparable(C comparable) {
050                    this.comparable = comparable;
051            }
052    
053            public O getObject() {
054                    return object;
055            }
056    
057            public void setObject(O object) {
058                    this.object = object;
059            }
060    
061            public String toString() {
062                    return "" + comparable + ": " + object;
063            }
064    
065            @SuppressWarnings("unchecked")
066            public int compareTo(Sortable<C, O> s) {
067                    if (comparable == null) {
068                            return Integer.MIN_VALUE;
069                    }
070                    int compObjectEqual = comparable.compareTo(s.comparable);
071                    if (!compareObject || !(compObjectEqual == 0)) {
072                            return compObjectEqual;
073                    }
074                    if (object == null) {
075                            return Integer.MIN_VALUE;
076                    }
077                    return ((Comparable<O>) object).compareTo(s.object);
078            }
079    
080            public boolean equals(Object obj) {
081                    if (obj instanceof Sortable) {
082                            Sortable<?, ?> s = (Sortable<?, ?>) obj;
083                            Comparable<?> c = s.getComparable();
084                            Object o = s.getObject();
085                            if (comparable == null && c != null) {
086                                    return false;
087                            }
088                            if (comparable.equals(c)) {
089                                    return false;
090                            }
091                            if (object == null && o != null) {
092                                    return false;
093                            }
094                            if (object.equals(o)) {
095                                    return false;
096                            }
097                            return true;
098                    }
099                    return false;
100            }
101    
102            public int hashCode() {
103                    int hash = 0;
104                    if (object != null) {
105                            hash += object.hashCode();
106                    }
107                    return hash;
108            }
109    
110    }