Skip to content

Commit 79d81c9

Browse files
committed
Added Copyable interface
1 parent 11873c2 commit 79d81c9

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

lib/jpt2.jar

677 Bytes
Binary file not shown.

src/org/cicirello/permutations/Permutation.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030
import java.math.BigInteger;
3131

3232
import org.cicirello.math.rand.RandomIndexer;
33+
import org.cicirello.util.Copyable;
3334

3435
/**
3536
* Representation of a permutation of the integers from 0 to N-1, inclusive.
3637
* This class provides the functionality to generate random permutations, and to
3738
* manipulate permutations in a variety of ways.
3839
*
3940
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
40-
* @version 7.26.2019
41+
* @version 9.23.2019
4142
* @since 1.0
4243
*/
43-
public final class Permutation implements Serializable, Iterable<Permutation>
44-
{
44+
public final class Permutation implements Serializable, Iterable<Permutation>, Copyable {
4545

4646
private static final long serialVersionUID = 1L;
4747

@@ -207,6 +207,11 @@ public Permutation(Permutation p, int length) {
207207
}
208208
}
209209

210+
@Override
211+
public Permutation copy() {
212+
return new Permutation(this);
213+
}
214+
210215

211216
/**
212217
* Generates a unique integer representing the permutation. Maps the permutations of the integers, 0..(N-1), to
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
*
4+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
5+
*
6+
* JavaPermutationTools is free software: you can
7+
* redistribute it and/or modify it under the terms of the GNU
8+
* General Public License as published by the Free Software
9+
* Foundation, either version 3 of the License, or (at your
10+
* option) any later version.
11+
*
12+
* JavaPermutationTools is distributed in the hope
13+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
14+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
15+
* PARTICULAR PURPOSE. See the GNU General Public License for more
16+
* details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
package org.cicirello.util;
24+
25+
/**
26+
* A simple interface for objects that can be copied.
27+
*
28+
* @since 2.0
29+
*
30+
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>,
31+
* <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
32+
* @version 9.23.2019
33+
*/
34+
public interface Copyable {
35+
36+
/**
37+
* Creates an identical copy of this object.
38+
* @return an identical copy of this object.
39+
*/
40+
Copyable copy();
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2019 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
*
4+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
5+
*
6+
* JavaPermutationTools is free software: you can
7+
* redistribute it and/or modify it under the terms of the GNU
8+
* General Public License as published by the Free Software
9+
* Foundation, either version 3 of the License, or (at your
10+
* option) any later version.
11+
*
12+
* JavaPermutationTools is distributed in the hope
13+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
14+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
15+
* PARTICULAR PURPOSE. See the GNU General Public License for more
16+
* details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
/**
24+
* This package includes classes and interfaces that serve some useful utility purpose.
25+
* @version 9.23.2019
26+
* @since 2.0
27+
*/
28+
package org.cicirello.util;

tests/org/cicirello/permutations/PermutationTestCases.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ public void testPermutationCopyConstructor() {
240240
}
241241
}
242242

243+
@Test
244+
public void testPermutationCopyMethod() {
245+
for (int n = 1; n <= 10; n++) {
246+
for (int i = 0; i < 10; i++) {
247+
Permutation p = new Permutation(n);
248+
Permutation copy = p.copy();
249+
assertEquals("copy should create an identical copy", p, copy);
250+
assertTrue("copy should be a new object", p != copy);
251+
}
252+
}
253+
}
254+
243255
@Test
244256
public void testPermutationConstructorCopyPartial() {
245257
for (int n = 1; n <= 10; n++) {

0 commit comments

Comments
 (0)