Sanlover

Untitled

Nov 15th, 2021
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package ru.perveev.container.node;
  2.  
  3. import java.util.Objects;
  4.  
  5. /**
  6.  * Class, implementing the double linked list node with generic stored data type
  7.  * <p>
  8.  *
  9.  * @author Sanlovty
  10.  * @version 1.1
  11.  * @since 1.0
  12.  */
  13. public class Node<T> {
  14.     /**
  15.      * Stored data
  16.      */
  17.     public T data;
  18.  
  19.     /**
  20.      * Pointer to the previous Node
  21.      */
  22.     public Node<T> previous;
  23.  
  24.     /**
  25.      * Pointer to the next Node
  26.      */
  27.     public Node<T> next;
  28.  
  29.     /**
  30.      * Creates an instance of Node with the specified data.
  31.      *
  32.      * @param data The data stored inside of Node.
  33.      */
  34.     public Node(T data) {
  35.         this.data = data;
  36.         previous = next = null;
  37.     }
  38.  
  39.     /**
  40.      * Creates an instance of Node with the specified data, next and previous nodes (pointers to)
  41.      *
  42.      * @param data     The data stored inside of Node.
  43.      * @param next     The pointer to the next Node.
  44.      * @param previous The pointer to the previous Node.
  45.      */
  46.     public Node(T data, Node<T> next, Node<T> previous) {
  47.         this.data = data;
  48.         this.previous = previous;
  49.         this.next = next;
  50.     }
  51.  
  52.     /**
  53.      * Overriding of equals method from Object class to let correctly check equality
  54.      *
  55.      * @param obj Object, we are trying to compare with
  56.      */
  57.     @Override
  58.     public boolean equals(Object obj) {
  59.         if (obj == this) {
  60.             return true;
  61.         }
  62.  
  63.         if (obj == null) {
  64.             return false;
  65.         }
  66.  
  67.         if (obj.getClass() != this.getClass()) {
  68.             return false;
  69.         }
  70.  
  71.         final Node<?> other = (Node<?>) obj;
  72.         return Objects.equals(this.data, other.data);
  73.     }
  74.  
  75.     /**
  76.      * Overriding of hashCode method from Object class because we override equals and we need to follow the rules Object.hashCode()
  77.      *
  78.      * @return hash of instance
  79.      */
  80.     @Override
  81.     public int hashCode() {
  82.         return 53 * 3 + (this.data != null ? this.data.hashCode() : 0);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment