Repository: TheAlgorithms/Java
Pull request #5717 (2024-10-11 09:35:41)

Add algo for BooleanGateslogic
Adding algorithm logic for Logicgates like or and #5680 
<!--
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] -->

- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [ ] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [ ] 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/5717?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 `0%` with `24 lines` in your changes missing coverage. Please review.
> Project coverage is 62.83%. Comparing base [(`2338428`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/233842857838c9be50c8f2f4c6b34a86c07a1583?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`e5dae59`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e5dae59ceaf2996c47566063e883c88a188a1092?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/5717?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...lgorithms/bitmanipulation/BooleanAlgebraGates.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5717?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fbitmanipulation%2FBooleanAlgebraGates.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9iaXRtYW5pcHVsYXRpb24vQm9vbGVhbkFsZ2VicmFHYXRlcy5qYXZh) | 0.00% | [24 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5717?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    #5717      +/-   ##
============================================
- Coverage     62.93%   62.83%   -0.11%     
+ Complexity     4044     4043       -1     
============================================
  Files           567      568       +1     
  Lines         16030    16054      +24     
  Branches       3084     3092       +8     
============================================
- Hits          10089    10087       -2     
- Misses         5525     5550      +25     
- Partials        416      417       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5717?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).

saahil-mahato: Please add tests and use proper javadoc standard found [here](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html)
VANSH3104: @alxkm not able to understand why i got clang format issue 
alxkm: > @saahil-mahato @alxkm not able to understand why i got clang format issue

You can see details by clicking to the job details: https://github.com/TheAlgorithms/Java/actions/runs/11296030042/job/31420026439?pr=5717
Looks like there are redundant whitespaces.

But you added test descriptions to javadocs, which is wrong.
You should have added standard Java tests for the test cases I described in the review comment.

This is the implementation:

```
package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate;
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;

class BooleanAlgebraGatesTest {

    @ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}")
    @MethodSource("provideAndGateTestCases")
    void testANDGate(List<Boolean> inputs, boolean expected) {
        BooleanGate gate = new ANDGate();
        assertEquals(expected, gate.evaluate(inputs));
    }

    @ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}")
    @MethodSource("provideOrGateTestCases")
    void testORGate(List<Boolean> inputs, boolean expected) {
        BooleanGate gate = new ORGate();
        assertEquals(expected, gate.evaluate(inputs));
    }

    @ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}")
    @CsvSource({
            "true, false",
            "false, true"
    })
    void testNOTGate(boolean input, boolean expected) {
        NOTGate gate = new NOTGate();
        assertEquals(expected, gate.evaluate(input));
    }

    @ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}")
    @MethodSource("provideXorGateTestCases")
    void testXORGate(List<Boolean> inputs, boolean expected) {
        BooleanGate gate = new XORGate();
        assertEquals(expected, gate.evaluate(inputs));
    }

    @ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}")
    @MethodSource("provideNandGateTestCases")
    void testNANDGate(List<Boolean> inputs, boolean expected) {
        BooleanGate gate = new NANDGate();
        assertEquals(expected, gate.evaluate(inputs));
    }

    @ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}")
    @MethodSource("provideNorGateTestCases")
    void testNORGate(List<Boolean> inputs, boolean expected) {
        BooleanGate gate = new NORGate();
        assertEquals(expected, gate.evaluate(inputs));
    }

    // Helper methods to provide test data for each gate

    static Stream<Object[]> provideAndGateTestCases() {
        return Stream.of(
                new Object[]{Arrays.asList(true, true, true), true},
                new Object[]{Arrays.asList(true, false, true), false},
                new Object[]{Arrays.asList(false, false, false), false},
                new Object[]{Collections.emptyList(), true} // AND over no inputs is true
        );
    }

    static Stream<Object[]> provideOrGateTestCases() {
        return Stream.of(
                new Object[]{Arrays.asList(true, false, false), true},
                new Object[]{Arrays.asList(false, false, false), false},
                new Object[]{Arrays.asList(true, true, true), true},
                new Object[]{Collections.emptyList(), false} // OR over no inputs is false
        );
    }

    static Stream<Object[]> provideXorGateTestCases() {
        return Stream.of(
                new Object[]{Arrays.asList(true, false, true), false}, // XOR over odd true
                new Object[]{Arrays.asList(true, false, false), true},  // XOR over single true
                new Object[]{Arrays.asList(false, false, false), false},// XOR over all false
                new Object[]{Arrays.asList(true, true), false}          // XOR over even true
        );
    }

    static Stream<Object[]> provideNandGateTestCases() {
        return Stream.of(
                new Object[]{Arrays.asList(true, true, true), false},   // NAND of all true is false
                new Object[]{Arrays.asList(true, false), true},         // NAND with one false is true
                new Object[]{Arrays.asList(false, false), true},        // NAND of all false is true
                new Object[]{Collections.emptyList(), false}            // NAND over no inputs is false (negation of AND)
        );
    }

    static Stream<Object[]> provideNorGateTestCases() {
        return Stream.of(
                new Object[]{Arrays.asList(false, false), true},        // NOR of all false is true
                new Object[]{Arrays.asList(false, true), false},        // NOR with one true is false
                new Object[]{Arrays.asList(true, true), false},         // NOR of all true is false
                new Object[]{Collections.emptyList(), true}             // NOR over no inputs is true (negation of OR)
        );
    }
}
```

It may be necessary to correct some input data and the import order.
VANSH3104: @alxkm github action told that there is error I don't understand I check it in my local setup it don't show any error

Patch:
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java b/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java
new file mode 100644
index 00000000..86946632
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java
@@ -0,0 +1,111 @@
+package com.thealgorithms.bitmanipulation;
+
+import java.util.List;
+
+/**
+ * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR)
+ */
+public final class BooleanAlgebraGates {
+
+    private BooleanAlgebraGates() {
+        // Prevent instantiation
+    }
+
+    /**
+     * Represents a Boolean gate that takes multiple inputs and returns a result.
+     */
+    interface BooleanGate {
+        /**
+         * Evaluates the gate with the given inputs.
+         *
+         * @param inputs The input values for the gate.
+         * @return The result of the evaluation.
+         */
+        boolean evaluate(List<Boolean> inputs);
+    }
+
+    /**
+     * AND Gate implementation.
+     * Returns true if all inputs are true; otherwise, false.
+     */
+    static class ANDGate implements BooleanGate {
+        @Override
+        public boolean evaluate(List<Boolean> inputs) {
+            for (boolean input : inputs) {
+                if (!input) {
+                    return false;
+                }
+            }
+            return true;
+        }
+    }
+
+    /**
+     * OR Gate implementation.
+     * Returns true if at least one input is true; otherwise, false.
+     */
+    static class ORGate implements BooleanGate {
+        @Override
+        public boolean evaluate(List<Boolean> inputs) {
+            for (boolean input : inputs) {
+                if (input) {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+
+    /**
+     * NOT Gate implementation (Unary operation).
+     * Negates a single input value.
+     */
+    static class NOTGate {
+        /**
+         * Evaluates the negation of the input.
+         *
+         * @param input The input value to be negated.
+         * @return The negated value.
+         */
+        public boolean evaluate(boolean input) {
+            return !input;
+        }
+    }
+
+    /**
+     * XOR Gate implementation.
+     * Returns true if an odd number of inputs are true; otherwise, false.
+     */
+    static class XORGate implements BooleanGate {
+        @Override
+        public boolean evaluate(List<Boolean> inputs) {
+            boolean result = false;
+            for (boolean input : inputs) {
+                result ^= input;
+            }
+            return result;
+        }
+    }
+
+    /**
+     * NAND Gate implementation.
+     * Returns true if at least one input is false; otherwise, false.
+     */
+    static class NANDGate implements BooleanGate {
+        @Override
+        public boolean evaluate(List<Boolean> inputs) {
+            return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND
+        }
+    }
+
+    /**
+     * NOR Gate implementation.
+     * Returns true if all inputs are false; otherwise, false.
+     */
+    static class NORGate implements BooleanGate {
+        @Override
+        public boolean evaluate(List<Boolean> inputs) {
+            return !new ORGate().evaluate(inputs); // Equivalent to negation of OR
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java
new file mode 100644
index 00000000..8737d05e
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java
@@ -0,0 +1,101 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate;
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate;
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate;
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate;
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate;
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate;
+import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class BooleanAlgebraGatesTest {
+
+    @ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}")
+    @MethodSource("provideAndGateTestCases")
+    void testANDGate(List<Boolean> inputs, boolean expected) {
+        BooleanGate gate = new ANDGate();
+        assertEquals(expected, gate.evaluate(inputs));
+    }
+
+    @ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}")
+    @MethodSource("provideOrGateTestCases")
+    void testORGate(List<Boolean> inputs, boolean expected) {
+        BooleanGate gate = new ORGate();
+        assertEquals(expected, gate.evaluate(inputs));
+    }
+
+    @ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}")
+    @CsvSource({"true, false", "false, true"})
+    void testNOTGate(boolean input, boolean expected) {
+        NOTGate gate = new NOTGate();
+        assertEquals(expected, gate.evaluate(input));
+    }
+
+    @ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}")
+    @MethodSource("provideXorGateTestCases")
+    void testXORGate(List<Boolean> inputs, boolean expected) {
+        BooleanGate gate = new XORGate();
+        assertEquals(expected, gate.evaluate(inputs));
+    }
+
+    @ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}")
+    @MethodSource("provideNandGateTestCases")
+    void testNANDGate(List<Boolean> inputs, boolean expected) {
+        BooleanGate gate = new NANDGate();
+        assertEquals(expected, gate.evaluate(inputs));
+    }
+
+    @ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}")
+    @MethodSource("provideNorGateTestCases")
+    void testNORGate(List<Boolean> inputs, boolean expected) {
+        BooleanGate gate = new NORGate();
+        assertEquals(expected, gate.evaluate(inputs));
+    }
+
+    // Helper methods to provide test data for each gate
+
+    static Stream<Object[]> provideAndGateTestCases() {
+        return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE},
+            new Object[] {Collections.emptyList(), Boolean.TRUE} // AND over no inputs is true
+        );
+    }
+
+    static Stream<Object[]> provideOrGateTestCases() {
+        return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE},
+            new Object[] {Collections.emptyList(), Boolean.FALSE} // OR over no inputs is false
+        );
+    }
+
+    static Stream<Object[]> provideXorGateTestCases() {
+        return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // XOR over odd true
+            new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // XOR over single true
+            new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, // XOR over all false
+            new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE} // XOR over even true
+        );
+    }
+
+    static Stream<Object[]> provideNandGateTestCases() {
+        return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NAND of all true is false
+            new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE), Boolean.TRUE}, // NAND with one false is true
+            new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NAND of all false is true
+            new Object[] {Collections.emptyList(), Boolean.FALSE} // NAND over no inputs is false (negation of AND)
+        );
+    }
+
+    static Stream<Object[]> provideNorGateTestCases() {
+        return Stream.of(new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NOR of all false is true
+            new Object[] {Arrays.asList(Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // NOR with one true is false
+            new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NOR of all true is false
+            new Object[] {Collections.emptyList(), Boolean.TRUE} // NOR over no inputs is true (negation of OR)
+        );
+    }
+}