| 1 | /* |
| 2 | * $Id: DepthComparator.java 562 2010-11-11 00:28:13Z PSpeed $ |
| 3 | * |
| 4 | * The Filament BSD license. |
| 5 | * |
| 6 | * Copyright (c) 2009-2010, the original author or authors |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1) Redistributions of source code must retain the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer. |
| 14 | * 2) Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3) Neither the names "Filament", "fgraph.org", "filamentgraph.org", nor the |
| 18 | * names of its contributors may be used to endorse or promote products |
| 19 | * derived from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | package org.fgraph.steps; |
| 35 | |
| 36 | import java.io.Serializable; |
| 37 | import java.util.Comparator; |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Compares the depth of two Steps for sorting in |
| 42 | * ascending order (breadth-first style) or descending |
| 43 | * order (depth-first style). |
| 44 | * |
| 45 | * @version $Revision: 562 $ |
| 46 | * @author Paul Speed |
| 47 | */ |
| 48 | public class DepthComparator<T extends Step> implements Comparator<T>, Serializable |
| 49 | { |
| 50 | static final long serialVersionUID = 1L; |
| 51 | |
| 52 | private static final DepthComparator DEPTH_FIRST = new DepthComparator(false); |
| 53 | private static final DepthComparator BREADTH_FIRST = new DepthComparator(true); |
| 54 | |
| 55 | private int sign = 1; |
| 56 | |
| 57 | /** |
| 58 | * Creates a new depth comparator that sorts ascending |
| 59 | * or descending depending on the specified flag. |
| 60 | */ |
| 61 | protected DepthComparator( boolean ascending ) |
| 62 | { |
| 63 | sign = ascending ? 1 : -1; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * A DepthComparator that sorts Steps by depth where the deepest |
| 68 | * depths are sorted earlier. In other words, for a traverser, all of |
| 69 | * the deepest depths would be processed first. |
| 70 | */ |
| 71 | @SuppressWarnings("unchecked") |
| 72 | public static <T extends Step> DepthComparator<T> depthFirst() |
| 73 | { |
| 74 | return (DepthComparator<T>)DEPTH_FIRST; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * A DepthComparator that sorts Steps by depth where the shallowest |
| 79 | * depths are sorted earlier. In other words, for a traverser, all of |
| 80 | * the shallowest depths would be processed first. |
| 81 | */ |
| 82 | @SuppressWarnings("unchecked") |
| 83 | public static <T extends Step> DepthComparator<T> breadthFirst() |
| 84 | { |
| 85 | return (DepthComparator<T>)BREADTH_FIRST; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | */ |
| 91 | public int compare( Step s1, Step s2 ) |
| 92 | { |
| 93 | int result = s1.getDepth() - s2.getDepth(); |
| 94 | return result * sign; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * {@inheritDoc} |
| 99 | */ |
| 100 | public String toString() |
| 101 | { |
| 102 | if( sign < 0 ) |
| 103 | return "DepthComparator[DEPTH_FIRST]"; |
| 104 | return "DepthComparator[BREADTH_FIRST]"; |
| 105 | } |
| 106 | } |
| 107 | |