Permalink
Browse files

GROOVY-5273: Provide contains as a DGM method for primitive arrays

  • Loading branch information...
1 parent e59d35b commit db1733a7ed270c8193727c0764005856bfcf7919 @paulk-asert paulk-asert committed Feb 1, 2012
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2011 the original author or authors.
+ * Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
import org.codehaus.groovy.runtime.dgmimpl.NumberNumberMultiply;
import org.codehaus.groovy.runtime.dgmimpl.NumberNumberPlus;
import org.codehaus.groovy.runtime.dgmimpl.arrays.*;
-import org.codehaus.groovy.runtime.metaclass.ClosureMetaClass;
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
import org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
@@ -11096,6 +11095,126 @@ public static boolean contains(CharSequence self, CharSequence text) {
}
/**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(int[] self, Object value) {
+ for (int next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(long[] self, Object value) {
+ for (long next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(short[] self, Object value) {
+ for (short next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(char[] self, Object value) {
+ for (char next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array within which we count the number of occurrences
+ * @param value the value being searched for
+ * @return the number of occurrences
+ * @since 1.6.4
+ */
+ public static boolean contains(boolean[] self, Object value) {
+ for (boolean next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(double[] self, Object value) {
+ for (double next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(float[] self, Object value) {
+ for (float next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Checks whether the array contains the given value.
+ *
+ * @param self the array we are searching
+ * @param value the value being searched for
+ * @return true if the array contains the value
+ * @since 2.0
+ */
+ public static boolean contains(byte[] self, Object value) {
+ for (byte next : self) {
+ if (DefaultTypeTransformation.compareEqual(value, next)) return true;
+ }
+ return false;
+ }
+
+ /**
* Count the number of occurrences of a substring.
*
* @param self a String
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2009 the original author or authors.
+ * Copyright 2003-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1110,6 +1110,36 @@ class GroovyMethodsTest extends GroovySwingTestCase {
assert clazz.isInstance( object.drop( 5 ) )
}
}
+
+ void testContainsForPrimitiveArrays() {
+ boolean[] bools = [false]
+ byte[] bytes = [1, 3]
+ short[] shorts = [1, 3]
+ int[] ints = [1, 3]
+ long[] longs = [1, 3]
+ float[] floats = [1.0f, 3.0f]
+ double[] doubles = [1.0d, 3.0d]
+ char[] chars = ['a' as char, 'c' as char]
+
+ assert bools.contains(false)
+ assert !bools.contains(true)
+ assert bytes.contains(3)
+ assert !bytes.contains(2)
+ assert shorts.contains(3)
+ assert !shorts.contains(2)
+ assert ints.contains(3)
+ assert !ints.contains(2)
+ assert longs.contains(3)
+ assert !longs.contains(2)
+ assert longs.contains(3)
+ assert !longs.contains(2)
+ assert floats.contains(3.0f)
+ assert !floats.contains(2.0f)
+ assert doubles.contains(3.0d)
+ assert !doubles.contains(2.0d)
+ assert chars.contains('c' as char)
+ assert !chars.contains('b' as char)
+ }
}
class WackyList extends LinkedList {

0 comments on commit db1733a

Please sign in to comment.