View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.object;
2   
3   import java.util.Objects;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.core.gtu.GtuType;
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
9   
10  /**
11   * Simple implementation of perceived GTU's which stores the information. This class does not support the behavioral component.
12   * <p>
13   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public class PerceivedGtuSimple extends PerceivedObjectBase implements PerceivedGtu
21  {
22  
23      /** GTU type. */
24      private final GtuType gtuType;
25  
26      /** GTU width. */
27      private final Length width;
28  
29      /** Signals. */
30      private final Signals signals;
31  
32      /** Maneuver. */
33      private final Maneuver maneuver;
34  
35      /**
36       * Constructor.
37       * @param id GTU id
38       * @param gtuType GTU type
39       * @param length length of the GTU
40       * @param width width of the GTU
41       * @param kinematics kinematics
42       * @param signals signals
43       * @param maneuver maneuver
44       * @throws NullPointerException when any input argument is {@code null}
45       */
46      public PerceivedGtuSimple(final String id, final GtuType gtuType, final Length length, final Length width,
47              final Kinematics kinematics, final Signals signals, final Maneuver maneuver)
48      {
49          super(id, ObjectType.GTU, length, kinematics);
50          this.gtuType = Throw.whenNull(gtuType, "gtuType");
51          this.width = Throw.whenNull(width, "width");
52          this.signals = Throw.whenNull(signals, "signals");
53          this.maneuver = Throw.whenNull(maneuver, "maneuver");
54      }
55  
56      @Override
57      public Length getWidth()
58      {
59          return this.width;
60      }
61  
62      @Override
63      public GtuType getGtuType()
64      {
65          return this.gtuType;
66      }
67  
68      @Override
69      public Signals getSignals()
70      {
71          return this.signals;
72      }
73  
74      @Override
75      public Maneuver getManeuver()
76      {
77          return this.maneuver;
78      }
79  
80      @Override
81      public Behavior getBehavior()
82      {
83          throw new UnsupportedOperationException("HeadwayGtuSimple does not support behavior in HeadwyaGtu.");
84      }
85  
86      /**
87       * Returns perceived GTU with given kinematics, but without {@code Behavior}.
88       * @param gtu GTU that is perceived
89       * @param kinematics kinematics for the vehicle
90       * @return perceived view of the GTU
91       */
92      public static PerceivedGtuSimple of(final LaneBasedGtu gtu, final Kinematics kinematics)
93      {
94          return new PerceivedGtuSimple(gtu.getId(), gtu.getType(), gtu.getLength(), gtu.getWidth(), kinematics, Signals.of(gtu),
95                  Maneuver.of(gtu));
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public int hashCode()
101     {
102         final int prime = 31;
103         int result = super.hashCode();
104         result = prime * result + Objects.hash(this.gtuType, this.maneuver, this.signals, this.width);
105         return result;
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public boolean equals(final Object obj)
111     {
112         if (this == obj)
113         {
114             return true;
115         }
116         if (!super.equals(obj))
117         {
118             return false;
119         }
120         if (getClass() != obj.getClass())
121         {
122             return false;
123         }
124         PerceivedGtuSimple other = (PerceivedGtuSimple) obj;
125         return Objects.equals(this.gtuType, other.gtuType) && Objects.equals(this.maneuver, other.maneuver)
126                 && Objects.equals(this.signals, other.signals) && Objects.equals(this.width, other.width);
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public String toString()
132     {
133         return "PerceivedGtuSimple [id=" + getId() + ", gtuType=" + this.gtuType + "]";
134     }
135 
136 }