Skip to content

Commit

Permalink
Merge pull request #113 from VsSekorin/rmn
Browse files Browse the repository at this point in the history
#112 RemoveNode
  • Loading branch information
vssekorin committed Mar 13, 2018
2 parents c7e322c + 2c6f255 commit 8a46e75
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/vssekorin/cactoosmath/graph/GraphOf.java
Expand Up @@ -37,7 +37,7 @@
* @param <T> Type of matrix
* @since 0.1
*/
public class GraphOf<T> extends GraphEnvelope<T> {
public final class GraphOf<T> extends GraphEnvelope<T> {

/**
* Ctor.
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/vssekorin/cactoosmath/graph/RemoveNode.java
@@ -0,0 +1,70 @@
/**
* MIT License
*
* Copyright (c) 2017-2018 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 NONINFRINGEMENT. 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.graph;

import com.vssekorin.cactoosmath.Graph;
import java.util.List;
import java.util.Map;
import org.cactoos.Scalar;
import org.cactoos.collection.Filtered;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.UncheckedScalar;

/**
* Graph without node.
*
* @author Vseslav Sekorin (vssekorin@gmail.com)
* @version $Id$
* @param <T> Type of matrix
* @since 0.1
*/
public final class RemoveNode<T> extends GraphEnvelope<T> {

/**
* Ctor.
* @param graph Origin graph
* @param node Scalar of node
*/
public RemoveNode(final Graph<T> graph, final Scalar<T> node) {
this(graph, new UncheckedScalar<>(node).value());
}

/**
* Ctor.
* @param graph Origin graph
* @param node Node
*/
public RemoveNode(final Graph<T> graph, final T node) {
super(() -> () -> {
final Map<T, List<T>> result = graph.asMap();
result.remove(node);
result.replaceAll(
(elem, list) -> new ListOf<>(
new Filtered<>(t -> !t.equals(node), list)
)
);
return result;
});
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/vssekorin/cactoosmath/graph/RemoveNodeTest.java
@@ -0,0 +1,55 @@
/**
* MIT License
*
* Copyright (c) 2017-2018 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 NONINFRINGEMENT. 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.graph;

import org.cactoos.list.ListOf;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

/**
* Test case for {@link RemoveNode}.
*
* @author Vseslav Sekorin (vssekorin@gmail.com)
* @version $Id$
* @since 0.1
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
*/
public final class RemoveNodeTest {

@Test
public void asMap() throws Exception {
MatcherAssert.assertThat(
new RemoveNode<>(
new GraphOf<>(
new ListOf<>(1, 2, 3),
node -> new ListOf<>(1, 2, 3)
),
2
).asMap().get(1),
CoreMatchers.hasItems(1, 3)
);
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/vssekorin/cactoosmath/graph/package-info.java
@@ -0,0 +1,32 @@
/**
* MIT License
*
* Copyright (c) 2017-2018 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 NONINFRINGEMENT. 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.
*/

/**
* Tests for graphs.
*
* @author Vseslav Sekorin (vssekorin@gmail.com)
* @version $Id$
* @since 0.1
*/
package com.vssekorin.cactoosmath.graph;

0 comments on commit 8a46e75

Please sign in to comment.