Repository: TheAlgorithms/Java
Pull request #5981 (2024-10-24 06:39:44)

refactor: Enhance docs, add tests in HeapElement
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5981?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `40.00000%` with `3 lines` in your changes missing coverage. Please review.
> Project coverage is 68.08%. Comparing base [(`5246f63`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/5246f635797194f5f0d7ac93af61099a5764fa4f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`55ecc18`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/55ecc18f1a27fac4103e4508020b7116e5dfc0ff?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5981?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...healgorithms/datastructures/heaps/HeapElement.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5981?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdatastructures%2Fheaps%2FHeapElement.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9kYXRhc3RydWN0dXJlcy9oZWFwcy9IZWFwRWxlbWVudC5qYXZh) | 40.00% | [0 Missing and 3 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5981?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5981      +/-   ##
============================================
+ Coverage     67.98%   68.08%   +0.10%     
- Complexity     4586     4596      +10     
============================================
  Files           620      620              
  Lines         17116    17112       -4     
  Branches       3305     3304       -1     
============================================
+ Hits          11636    11651      +15     
+ Misses         5035     5012      -23     
- Partials        445      449       +4     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5981?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index cd511b4a..b880021a 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -865,6 +865,7 @@
             * heaps
               * [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
               * [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
+              * [HeapElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java)
               * [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
               * [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
               * [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java b/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java
index 7c457a34..20f33bd2 100644
--- a/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java
+++ b/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java
@@ -1,14 +1,24 @@
 package com.thealgorithms.datastructures.heaps;
 
 /**
- * Class for heap elements.<br>
+ * Class representing an element in a heap.
  *
  * <p>
- * A heap element contains two attributes: a key which will be used to build the
- * tree (int or double, either primitive type or object) and any kind of
- * IMMUTABLE object the user sees fit to carry any information he/she likes. Be
- * aware that the use of a mutable object might jeopardize the integrity of this
- * information.
+ * A heap element contains two attributes: a key used for ordering in the heap
+ * (which can be of type int or double, either as primitive types or as wrapper objects)
+ * and an additional immutable object that can store any supplementary information the user desires.
+ * Note that using mutable objects may compromise the integrity of this information.
+ * </p>
+ *
+ * <p>
+ * The key attribute is used to determine the order of elements in the heap,
+ * while the additionalInfo attribute can carry user-defined data associated with the key.
+ * </p>
+ *
+ * <p>
+ * This class provides multiple constructors to accommodate various key types and includes
+ * methods to retrieve the key and additional information.
+ * </p>
  *
  * @author Nicolas Renard
  */
@@ -19,9 +29,10 @@ public class HeapElement {
 
     // Constructors
     /**
-     * @param key : a number of primitive type 'double'
-     * @param info : any kind of IMMUTABLE object. May be null, since the
-     * purpose is only to carry additional information of use for the user
+     * Creates a HeapElement with the specified key and additional information.
+     *
+     * @param key  the key of the element (primitive type double)
+     * @param info any immutable object containing additional information, may be null
      */
     public HeapElement(double key, Object info) {
         this.key = key;
@@ -29,9 +40,10 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of primitive type 'int'
-     * @param info : any kind of IMMUTABLE object. May be null, since the
-     * purpose is only to carry additional information of use for the user
+     * Creates a HeapElement with the specified key and additional information.
+     *
+     * @param key  the key of the element (primitive type int)
+     * @param info any immutable object containing additional information, may be null
      */
     public HeapElement(int key, Object info) {
         this.key = key;
@@ -39,9 +51,10 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of object type 'Integer'
-     * @param info : any kind of IMMUTABLE object. May be null, since the
-     * purpose is only to carry additional information of use for the user
+     * Creates a HeapElement with the specified key and additional information.
+     *
+     * @param key  the key of the element (object type Integer)
+     * @param info any immutable object containing additional information, may be null
      */
     public HeapElement(Integer key, Object info) {
         this.key = key;
@@ -49,9 +62,10 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of object type 'Double'
-     * @param info : any kind of IMMUTABLE object. May be null, since the
-     * purpose is only to carry additional information of use for the user
+     * Creates a HeapElement with the specified key and additional information.
+     *
+     * @param key  the key of the element (object type Double)
+     * @param info any immutable object containing additional information, may be null
      */
     public HeapElement(Double key, Object info) {
         this.key = key;
@@ -59,7 +73,9 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of primitive type 'double'
+     * Creates a HeapElement with the specified key.
+     *
+     * @param key the key of the element (primitive type double)
      */
     public HeapElement(double key) {
         this.key = key;
@@ -67,7 +83,9 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of primitive type 'int'
+     * Creates a HeapElement with the specified key.
+     *
+     * @param key the key of the element (primitive type int)
      */
     public HeapElement(int key) {
         this.key = key;
@@ -75,7 +93,9 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of object type 'Integer'
+     * Creates a HeapElement with the specified key.
+     *
+     * @param key the key of the element (object type Integer)
      */
     public HeapElement(Integer key) {
         this.key = key;
@@ -83,7 +103,9 @@ public class HeapElement {
     }
 
     /**
-     * @param key : a number of object type 'Double'
+     * Creates a HeapElement with the specified key.
+     *
+     * @param key the key of the element (object type Double)
      */
     public HeapElement(Double key) {
         this.key = key;
@@ -92,46 +114,57 @@ public class HeapElement {
 
     // Getters
     /**
-     * @return the object containing the additional info provided by the user.
+     * Returns the object containing the additional information provided by the user.
+     *
+     * @return the additional information
      */
     public Object getInfo() {
         return additionalInfo;
     }
 
     /**
-     * @return the key value of the element
+     * Returns the key value of the element.
+     *
+     * @return the key of the element
      */
     public double getKey() {
         return key;
     }
 
     // Overridden object methods
+    /**
+     * Returns a string representation of the heap element.
+     *
+     * @return a string describing the key and additional information
+     */
+    @Override
     public String toString() {
-        return "Key: " + key + " - " + additionalInfo.toString();
+        return "Key: " + key + " - " + (additionalInfo != null ? additionalInfo.toString() : "No additional info");
     }
 
     /**
-     * @param otherHeapElement
-     * @return true if the keys on both elements are identical and the
-     * additional info objects are identical.
+     * Compares this heap element to another object for equality.
+     *
+     * @param o the object to compare with
+     * @return true if the keys and additional information are identical, false otherwise
      */
     @Override
     public boolean equals(Object o) {
-        if (o != null) {
-            if (!(o instanceof HeapElement)) {
-                return false;
-            }
-            HeapElement otherHeapElement = (HeapElement) o;
-            return ((this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo)));
+        if (o instanceof HeapElement otherHeapElement) {
+            return this.key == otherHeapElement.key && (this.additionalInfo != null ? this.additionalInfo.equals(otherHeapElement.additionalInfo) : otherHeapElement.additionalInfo == null);
         }
         return false;
     }
 
+    /**
+     * Returns a hash code value for the heap element.
+     *
+     * @return a hash code value for this heap element
+     */
     @Override
     public int hashCode() {
-        int result = 0;
-        result = 31 * result + (int) key;
-        result = 31 * result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
+        int result = 31 * (int) key;
+        result += (additionalInfo != null) ? additionalInfo.hashCode() : 0;
         return result;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java b/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java
new file mode 100644
index 00000000..d04a9de8
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java
@@ -0,0 +1,55 @@
+package com.thealgorithms.datastructures.heaps;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.junit.jupiter.api.Test;
+
+class HeapElementTest {
+
+    @Test
+    void testConstructorAndGetters() {
+        HeapElement element = new HeapElement(5.0, "Info");
+        assertEquals(5.0, element.getKey());
+        assertEquals("Info", element.getInfo());
+    }
+
+    @Test
+    void testConstructorWithNullInfo() {
+        HeapElement element = new HeapElement(10);
+        assertEquals(10, element.getKey());
+        assertNull(element.getInfo());
+    }
+
+    @Test
+    void testToString() {
+        HeapElement element = new HeapElement(7.5, "TestInfo");
+        assertEquals("Key: 7.5 - TestInfo", element.toString());
+
+        HeapElement elementWithoutInfo = new HeapElement(3);
+        assertEquals("Key: 3.0 - No additional info", elementWithoutInfo.toString());
+    }
+
+    @Test
+    void testEquals() {
+        HeapElement element1 = new HeapElement(2.5, "Data");
+        HeapElement element2 = new HeapElement(2.5, "Data");
+        HeapElement element3 = new HeapElement(3.0, "DifferentData");
+
+        assertEquals(element1, element2); // Same key and info
+        assertNotEquals(element1, element3); // Different key
+        assertNotEquals(null, element1); // Check for null
+        assertNotEquals("String", element1); // Check for different type
+    }
+
+    @Test
+    void testHashCode() {
+        HeapElement element1 = new HeapElement(4, "HashMe");
+        HeapElement element2 = new HeapElement(4, "HashMe");
+        HeapElement element3 = new HeapElement(4, "DifferentHash");
+
+        assertEquals(element1.hashCode(), element2.hashCode()); // Same key and info
+        assertNotEquals(element1.hashCode(), element3.hashCode()); // Different info
+    }
+}