| 1 | /* |
| 2 | * $Id: DefaultStep.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 | |
| 38 | import com.google.common.base.Objects; |
| 39 | |
| 40 | /** |
| 41 | * A basic step implementation. |
| 42 | * |
| 43 | * @version $Revision: 562 $ |
| 44 | * @author Paul Speed |
| 45 | */ |
| 46 | public class DefaultStep<T> implements Step<T>, Serializable |
| 47 | { |
| 48 | static final long serialVersionUID = 1L; |
| 49 | |
| 50 | private Step parent; |
| 51 | private T value; |
| 52 | private int depth; |
| 53 | |
| 54 | /** |
| 55 | * Creates a step containing the specified parent |
| 56 | * and value. The parent is optional but if supplied |
| 57 | * will be used to calculate a new depth. Otherwise the |
| 58 | * depth is set to 0. |
| 59 | */ |
| 60 | protected DefaultStep( Step parent, T value ) |
| 61 | { |
| 62 | this.parent = parent; |
| 63 | this.value = value; |
| 64 | if( parent != null ) |
| 65 | this.depth = parent.getDepth() + 1; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a step containing the specified parent |
| 70 | * and value. The parent is optional but if supplied |
| 71 | * will be used to calculate a new depth. Otherwise the |
| 72 | * depth is set to 0. |
| 73 | */ |
| 74 | public static <T> DefaultStep<T> create( Step parent, T value ) |
| 75 | { |
| 76 | return new DefaultStep<T>( parent, value ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns true if the supplied object is a DefaultStep |
| 81 | * with the same depth and .equals() equivalent parent |
| 82 | * and values. |
| 83 | */ |
| 84 | @SuppressWarnings("unchecked") |
| 85 | public boolean equals( Object o ) |
| 86 | { |
| 87 | if( o == this ) |
| 88 | return true; |
| 89 | if( o == null || o.getClass() != getClass() ) |
| 90 | return false; |
| 91 | |
| 92 | // No warning-free way to do this |
| 93 | DefaultStep<T> other = (DefaultStep<T>)o; |
| 94 | if( !Objects.equal( other.parent, parent ) ) |
| 95 | return false; |
| 96 | if( !Objects.equal( other.value, value ) ) |
| 97 | return false; |
| 98 | return other.depth == depth; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * {@inheritDoc} |
| 103 | */ |
| 104 | public int hashCode() |
| 105 | { |
| 106 | return value == null ? 0 : value.hashCode(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * {@inheritDoc} |
| 111 | */ |
| 112 | public T getValue() |
| 113 | { |
| 114 | return value; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | public int getDepth() |
| 121 | { |
| 122 | return depth; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritDoc} |
| 127 | */ |
| 128 | public Step getParent() |
| 129 | { |
| 130 | return parent; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * {@inheritDoc} |
| 135 | */ |
| 136 | public String toString() |
| 137 | { |
| 138 | return "Step[" + value + " : " + getDepth() + "]"; |
| 139 | } |
| 140 | } |