Skip to content

Commit

Permalink
#297 Matrix element
Browse files Browse the repository at this point in the history
  • Loading branch information
vssekorin committed Aug 9, 2022
1 parent 85d8c3b commit 1796edd
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/vssekorin/cactoosmath/matrix/Element.java
Original file line number Diff line number Diff line change
@@ -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 <T> Type of element.
* @since 0.2
*/
public final class Element<T> extends ScalarEnvelope<T> {

/**
* Ctor.
* @param matrix Matrix
* @param row Row
* @param column Column
*/
public Element(final Matrix<T> matrix, final int row, final int column) {
super(() -> matrix.asArray()[row][column]);
}
}
58 changes: 58 additions & 0 deletions src/test/java/com/vssekorin/cactoosmath/ElementTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}

0 comments on commit 1796edd

Please sign in to comment.