diff --git a/common/source/java/ch/systemsx/cisd/common/collections/CollectionUtils.java b/common/source/java/ch/systemsx/cisd/common/collections/CollectionUtils.java index 74439fed8d79f5bda2a34a5b6664c2e16bdf2046..2c3c4d7d145d145da19cbd35f29d3a0193c6ca1d 100644 --- a/common/source/java/ch/systemsx/cisd/common/collections/CollectionUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/collections/CollectionUtils.java @@ -37,9 +37,8 @@ public final class CollectionUtils /** * Abbreviates a given array of <code>Object</code>. * <p> - * By default it shows the number of items left, - * {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are - * used. + * By default it shows the number of items left, {@link CollectionStyle#DEFAULT} and + * {@link ToStringDefaultConverter} are used. * </p> * * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then @@ -53,9 +52,8 @@ public final class CollectionUtils /** * Abbreviates a given <code>Collection</code>. * <p> - * By default it shows the number of items left, - * {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are - * used. + * By default it shows the number of items left, {@link CollectionStyle#DEFAULT} and + * {@link ToStringDefaultConverter} are used. * </p> * * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then @@ -108,8 +106,7 @@ public final class CollectionUtils public final static <T> String abbreviate(final Collection<T> collection, final int maxLength, final IToStringConverter<? super T> converter) { - return abbreviate(collection, maxLength, converter, - CollectionStyle.DEFAULT); + return abbreviate(collection, maxLength, converter, CollectionStyle.DEFAULT); } /** @@ -130,8 +127,7 @@ public final class CollectionUtils /** * Abbreviates a given array of <code>Object</code>. * <p> - * By default {@link CollectionStyle#DEFAULT} and - * {@link ToStringDefaultConverter} are used. + * By default {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are used. * </p> * * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then @@ -146,8 +142,7 @@ public final class CollectionUtils /** * Abbreviates a given <code>Collection</code>. * <p> - * By default {@link CollectionStyle#DEFAULT} and - * {@link ToStringDefaultConverter} are used. + * By default {@link CollectionStyle#DEFAULT} and {@link ToStringDefaultConverter} are used. * </p> * * @param maxLength the maximum number of items that should be shown. If <code>-1</code> then @@ -171,8 +166,7 @@ public final class CollectionUtils public final static <T> String abbreviate(final T[] objects, final int maxLength, final boolean showLeft, final IToStringConverter<? super T> converter) { - return abbreviate(objects, maxLength, showLeft, converter, - CollectionStyle.DEFAULT); + return abbreviate(objects, maxLength, showLeft, converter, CollectionStyle.DEFAULT); } /** @@ -199,8 +193,7 @@ public final class CollectionUtils public final static <T> String abbreviate(final Collection<T> collection, final int maxLength, final boolean showLeft, final IToStringConverter<? super T> converter) { - return abbreviate(collection, maxLength, showLeft, converter, - CollectionStyle.DEFAULT); + return abbreviate(collection, maxLength, showLeft, converter, CollectionStyle.DEFAULT); } /** @@ -298,4 +291,21 @@ public final class CollectionUtils } return result; } + + public static interface CollectionMappingFunction<K, V> + { + K map(V element); + } + + /** Transforms one list into another by converting each element with the specified function. */ + public static final <K, V> List<K> map(Collection<V> list, + CollectionMappingFunction<K, V> mapping) + { + List<K> mapped = new ArrayList<K>(); + for (V elem : list) + { + mapped.add(mapping.map(elem)); + } + return mapped; + } } \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/test/AssertionUtil.java b/common/source/java/ch/systemsx/cisd/common/test/AssertionUtil.java index 4c8b4a9b1797758da1e7c2de6fa13247113218d9..7cda65c69b9d60ea974fe06f1f3331e29281b248 100644 --- a/common/source/java/ch/systemsx/cisd/common/test/AssertionUtil.java +++ b/common/source/java/ch/systemsx/cisd/common/test/AssertionUtil.java @@ -16,7 +16,8 @@ package ch.systemsx.cisd.common.test; -import org.testng.AssertJUnit; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; /** * Utilities for making assertions in unit tests. @@ -30,7 +31,7 @@ public class AssertionUtil { String errorMsg = String.format("String '%s' was expected to start with '%s'.", text, expectedPrefix); - AssertJUnit.assertTrue(errorMsg, text.startsWith(expectedPrefix)); + assertTrue(errorMsg, text.startsWith(expectedPrefix)); } /** asserts that given text ends with expectedSubstring */ @@ -38,7 +39,7 @@ public class AssertionUtil { String errorMsg = String.format("String '%s' was expected to end with '%s'.", text, expectedSuffix); - AssertJUnit.assertTrue(errorMsg, text.endsWith(expectedSuffix)); + assertTrue(errorMsg, text.endsWith(expectedSuffix)); } /** asserts that given text contains expectedSubstring */ @@ -47,7 +48,7 @@ public class AssertionUtil String errorMsg = String.format("String '%s' was expected to be a substring of '%s'.", expectedSubstring, text); - AssertJUnit.assertTrue(errorMsg, text.contains(expectedSubstring)); + assertTrue(errorMsg, text.contains(expectedSubstring)); } /** asserts that given text contains expectedSubstring. Comparision is case insensitive. */ @@ -55,4 +56,25 @@ public class AssertionUtil { assertContains(expectedSubstring.toUpperCase(), text.toUpperCase()); } + + /** asserts that two int arrays are equal **/ + public static void assertArraysEqual(int[] a1, int[] a2) + { + assertEquals(a1.length, a2.length); + for (int i = 0; i < a1.length; i++) + { + assertEquals("Different elements at position, " + i, a1[i], a2[i]); + } + } + + /** asserts that two float arrays are equal **/ + public static void assertArraysEqual(float[] a1, float[] a2) + { + assertEquals(a1.length, a2.length); + for (int i = 0; i < a1.length; i++) + { + assertEquals("Different elements at position, " + i, a1[i], a2[i]); + } + } + }