From d5f8ae7f259c848d518000b095ef65be8bb1f652 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Fri, 26 Oct 2007 19:16:19 +0000 Subject: [PATCH] add: - 'FileComparator.BY_TYPE' - 'CompositeComparator' SVN: 2265 --- .../common/utilities/CompositeComparator.java | 51 +++++++++++++++++++ .../cisd/common/utilities/FileComparator.java | 25 ++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/CompositeComparator.java diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/CompositeComparator.java b/common/source/java/ch/systemsx/cisd/common/utilities/CompositeComparator.java new file mode 100644 index 00000000000..4624fc767e4 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/CompositeComparator.java @@ -0,0 +1,51 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.utilities; + +import java.util.Comparator; + +/** + * Comparator that makes comparison using an ordered list of individual comparators. + * + * @author Christian Ribeaud + */ +public final class CompositeComparator<T> implements Comparator<T> +{ + private final Comparator<T>[] comparators; + + public CompositeComparator(final Comparator<T>... comparators) + { + this.comparators = comparators; + } + + // + // Comparator + // + + public final int compare(final T o1, final T o2) + { + for (Comparator<T> comparator : comparators) + { + int c = comparator.compare(o1, o2); + if (c != 0) + { + return c; + } + } + return 0; + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileComparator.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileComparator.java index 5e8a56c21f4..fdf32d90780 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/FileComparator.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileComparator.java @@ -44,8 +44,9 @@ public final class FileComparator // Comparator // - public int compare(File o1, File o2) + public final int compare(final File o1, final File o2) { + assert o1 != null && o2 != null; return (int) (o1.lastModified() - o2.lastModified()); } }; @@ -62,9 +63,29 @@ public final class FileComparator // Comparator // - public int compare(File o1, File o2) + public final int compare(final File o1, final File o2) { + assert o1 != null && o2 != null; return o1.getName().compareTo(o2.getName()); } }; + + /** + * A {@link File} <code>Comparator</code> implementation that sorts by type (file or directory): first the files + * are listed then come the directories. + * + * @author Christian Ribeaud + */ + public final static Comparator<File> BY_TYPE = new Comparator<File>() + { + // + // Comparator + // + + public final int compare(final File o1, final File o2) + { + assert o1 != null && o2 != null; + return o1.isFile() ? o2.isFile() ? 0 : -1 : +1; + } + }; } \ No newline at end of file -- GitLab