diff --git a/src/main/java/com/vssekorin/cactoosmath/matrix/Element.java b/src/main/java/com/vssekorin/cactoosmath/matrix/Element.java new file mode 100644 index 000000000..067216654 --- /dev/null +++ b/src/main/java/com/vssekorin/cactoosmath/matrix/Element.java @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2022 Vseslav Sekorin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.vssekorin.cactoosmath.matrix; + +import com.vssekorin.cactoosmath.Matrix; +import org.cactoos.scalar.ScalarEnvelope; + +/** + * The element of a matrix. + * + * @param Type of element. + * @since 0.2 + */ +public final class Element extends ScalarEnvelope { + + /** + * Ctor. + * @param matrix Matrix + * @param row Row + * @param column Column + */ + public Element(final Matrix matrix, final int row, final int column) { + super(() -> matrix.asArray()[row][column]); + } +} diff --git a/src/test/java/com/vssekorin/cactoosmath/ElementTest.java b/src/test/java/com/vssekorin/cactoosmath/ElementTest.java new file mode 100644 index 000000000..207956dcb --- /dev/null +++ b/src/test/java/com/vssekorin/cactoosmath/ElementTest.java @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2022 Vseslav Sekorin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.vssekorin.cactoosmath; + +import com.vssekorin.cactoosmath.matrix.Element; +import com.vssekorin.cactoosmath.matrix.MatrixOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Element}. + * + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class ElementTest { + + @Test + public void value() throws Exception { + MatcherAssert.assertThat( + "Must return element by index [1, 1]", + new Element<>( + new MatrixOf<>( + new Integer[][]{ + {5, 7, 4}, + {-4, 6, 0}, + {2, 0, 3}, + } + ), + 1, 1 + ).value(), + Matchers.equalTo(6) + ); + } +}