krysalis-update/src/java/org/krysalis/depot/update/util/compare ComparatorSequence.java,NONE,1.1 ComparisonHelper.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:36 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/compare
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/compare
Added Files:
ComparatorSequence.java ComparisonHelper.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: ComparatorSequence.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.util.compare;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
/**
* @author arb_jack
*/
public class ComparatorSequence extends ArrayList implements Comparator {
// An set of all
public ComparatorSequence(List comparators) {
super(comparators);
}
/**
* An empty sequence
*
*/
public ComparatorSequence() {
super();
}
public void addComparator(final Comparator c) {
add(c);
}
/**
* Compare using sub comparators, in order, until
* a difference is found, or not.
*/
public int compare(Object o1, Object o2) {
int comparison = 0;
for (Iterator i = iterator();
i.hasNext() && (0 == comparison);) {
Comparator comparator = ((Comparator) i.next());
comparison = comparator.compare(o1, o2);
}
return comparison;
}
}
--- NEW FILE: ComparisonHelper.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.util.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import org.krysalis.depot.update.util.text.Messages;
import org.krysalis.depot.common.log.Logger;
/**
* @author arb_jack
*/
public class ComparisonHelper {
/**
* Sort plus uniqueify (i.e. drop duplicates)
*
* @param choices
* @param comparator
* @return
*/
public static List sortUnique(List choices, Comparator comparator) {
List sorted = new ArrayList();
sortUniqueInto(choices, comparator, sorted);
return sorted;
}
public static void sortUniqueInto(
List choices,
Comparator comparator,
List sorted) {
// Inefficient, no doubt for many reasons, but so a :TODO:
TreeSet set = new TreeSet(comparator);
for (Iterator i = choices.iterator(); i.hasNext();) {
Object choice = i.next();
try {
if (!set.contains(choice))
sorted.add(choice);
else
Logger.getLogger().debug(
Messages.getString(
"DUPLICATE",
new Object[] { comparator, choice }));
}
catch (Exception e) {
Logger.getLogger().error(
Messages.getString(
"COMPARATOR_CRASHED",
new Object[] { comparator, e }),
e);
}
}
Collections.sort(sorted, comparator);
}
}
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/