commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Simon Kitching (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-238) [lang] Add equals(type[]) to NumberUtils
Date Tue, 02 Jan 2007 04:16:27 GMT

    [ http://issues.apache.org/jira/browse/LANG-238?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12461664
] 

Simon Kitching commented on LANG-238:
-------------------------------------

I would agree with Stephen that an "allEquals(a)" method isn't likely to be widely used. And
besides, it can be implemented for non-floating-point as:
  Set<integer> aSet = new HashSet<Integer>(Arrays.asList(a));
  boolean allEq = (aSet.size() <= 1);
which doesn't seem to me sufficiently complicated to deserve inclusion in lang.

A "containsOnly(a,b)" method would be more generally useful but can be implemented as:
  Set<Integer> aSet = new HashSet<Integer>(Arrays.asList(a));
  Set<Integer> bSet = new HashSet<Integer>(Arrays.asList(b));
  // or if b has one element, bSet = Collections.singleton(val);
  boolean contained = bSet.containsAll(aSet);
which again doesn't seem terribly complicated.

However if a more generic method is being considered, then how about:
  T[] notIn(T[] a, T[] b);
which returns the elements in a that are not in b? I'm not a mathematician but I believe this
is written in set operations as "a - b", or in words as "the complement of a with respect
to b", so this method could also be called:
  T[] subtract(T[] a, T[] b);
or
  T[] complement(T[] a, T[] b);
though I think "notIn" is more obvious for most users.

Actually, this operation is the array equivalent of Collections.removeAll, so that's an alternative
name:
  T[] removeAll(T[] a, T[] b);

The test "allEquals(a, b)" is then:
  boolean allEq = NumberUtils.notIn(a, b).length == 0;
and the test "allEquals(a)" is:
  boolean allEq = NumberUtils.notIn(a, new Integer[] {a[0]}).length == 0;

BTW, the "a" parameter is really an array representation of a bag, not a set so maybe this
set terminology could be misleading.

If such a set/bag operation is provided, then intersection and union could also reasonably
be provided for arrays:
  T[] intersection(T[] a, T[] b);
  T[] union(T[] a, T[] b);
Not sure whether union should treat its params as bags (in which case union would be equivalent
to concat) or sets (only one occurrence for elements in both a and b).

At this point, this appears to be bordering on commons-collections or commons-math functionality
but as this is only for arrays I think a good argument could be made for including this in
lang..

> [lang] Add equals(type[]) to NumberUtils
> ----------------------------------------
>
>                 Key: LANG-238
>                 URL: http://issues.apache.org/jira/browse/LANG-238
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: Nightly Builds
>         Environment: Operating System: other
> Platform: Other
>            Reporter: Paul Benedict
>            Priority: Minor
>             Fix For: 3.0
>
>
> It would be useful to add an equals() method like the current min and max
> methods which take an array type and determine if all the values are equal.
> I have found myself in need of this often. I have to retrieve objects from
> multiple data sources in parallel to build an array of complex object. To ensure
> validity, I always compare that my sub-retrievals returned the same number of
> objects as expected.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message