001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/api/src/java/org/theospi/portfolio/presentation/model/PresentationItemComparator.java $
003: * $Id:PresentationItemComparator.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.presentation.model;
021:
022: import java.util.Comparator;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.sakaiproject.metaobj.shared.model.Id;
027:
028: public class PresentationItemComparator implements Comparator {
029: protected final transient Log logger = LogFactory
030: .getLog(getClass());
031:
032: /**
033: * Compares its two arguments for order. Returns a negative integer,
034: * zero, or a positive integer as the first argument is less than, equal
035: * to, or greater than the second.<p>
036: * <p/>
037: * The implementor must ensure that <tt>sgn(compare(x, y)) ==
038: * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
039: * implies that <tt>compare(x, y)</tt> must throw an exception if and only
040: * if <tt>compare(y, x)</tt> throws an exception.)<p>
041: * <p/>
042: * The implementor must also ensure that the relation is transitive:
043: * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
044: * <tt>compare(x, z)>0</tt>.<p>
045: * <p/>
046: * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
047: * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
048: * <tt>z</tt>.<p>
049: * <p/>
050: * It is generally the case, but <i>not</i> strictly required that
051: * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
052: * any comparator that violates this condition should clearly indicate
053: * this fact. The recommended language is "Note: this comparator
054: * imposes orderings that are inconsistent with equals."
055: *
056: * @param o1 the first object to be compared.
057: * @param o2 the second object to be compared.
058: * @return a negative integer, zero, or a positive integer as the
059: * first argument is less than, equal to, or greater than the
060: * second.
061: * @throws ClassCastException if the arguments' types prevent them from
062: * being compared by this Comparator.
063: */
064: public int compare(Object o1, Object o2) {
065: PresentationItemDefinition q1 = (PresentationItemDefinition) o1;
066: PresentationItemDefinition q2 = (PresentationItemDefinition) o2;
067:
068: Id id1 = q1.getId() == null ? q1.getNewId() : q1.getId();
069: Id id2 = q2.getId() == null ? q2.getNewId() : q2.getId();
070:
071: if (o1 == null && o2 == null)
072: return 0;
073: else if (o1 == null)
074: return -1;
075: else if (o2 == null)
076: return 1;
077: else if (id1 == null && id2 != null)
078: return -1;
079: else if (id1 != null && id2 == null)
080: return 1;
081: else if (id1.equals(id2))
082: return 0; //if the ids are the same, should be the same object
083:
084: long ord1 = q1.getNewSequence();
085: long ord2 = q2.getNewSequence();
086:
087: if (ord1 < ord2)
088: return -1;
089: if (ord1 > ord2)
090: return 1;
091:
092: // they are equal, return opposite of order
093: ord1 = q1.getSequence();
094: ord2 = q2.getSequence();
095:
096: if (ord1 < ord2)
097: return 1;
098: if (ord1 > ord2)
099: return -1;
100:
101: return 0;
102: }
103: }
|