View Javadoc

1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package com.tomgibara.pronto.state.impl;
20  
21  import com.tomgibara.pronto.state.StateTransition;
22  import com.tomgibara.pronto.util.Arguments;
23  
24  public class StateTransitionImpl<S, L> implements StateTransition<S, L> {
25  
26      // fields
27  
28      private final S source;
29  
30      private final L label;
31  
32      private final S target;
33  
34      private final int hashCode;
35  
36      // constructors
37  
38      public StateTransitionImpl(final StateTransition<S, L> transition) {
39          this(transition.getSource(), transition.getLabel(), transition.getTarget());
40      }
41  
42      public StateTransitionImpl(final S source, final L label, final S target) {
43          Arguments.notNull(source, "source");
44          Arguments.notNull(label, "label");
45          Arguments.notNull(target, "target");
46  
47          this.source = source;
48          this.label = label;
49          this.target = target;
50  
51          hashCode = source.hashCode() ^ label.hashCode() ^ 7 * target.hashCode();
52      }
53  
54      // transition methods
55  
56      public S getSource() {
57          return source;
58      }
59  
60      public L getLabel() {
61          return label;
62      }
63  
64      public S getTarget() {
65          return target;
66      }
67  
68      // object methods
69  
70      @SuppressWarnings("unchecked")
71      @Override
72      public boolean equals(final Object obj) {
73          if (obj == this) return true;
74          if (!(obj instanceof StateTransitionImpl)) return false;
75          StateTransitionImpl<S, L> that = (StateTransitionImpl<S, L>) obj;
76          if (!this.source.equals(that.source)) return false;
77          if (!this.label.equals(that.label)) return false;
78          if (!this.target.equals(that.target)) return false;
79          return true;
80      }
81  
82      @Override
83      public int hashCode() {
84          return hashCode;
85      }
86  
87      @Override
88      public String toString() {
89          return "[transition from " + source + " to " + target + " via " + label + "]";
90      }
91  
92  }