Add null check to merge methods, refactor code a bit.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1455703 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-03-12 20:46:23 +00:00
parent e1f1860894
commit c664e4d33c
1 changed files with 15 additions and 6 deletions

View File

@ -25,7 +25,9 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.MathUtils;
/** /**
* Maintains a frequency distribution. * Maintains a frequency distribution.
@ -499,11 +501,15 @@ public class Frequency implements Serializable {
* by the counts represented by other. * by the counts represented by other.
* *
* @param other the other {@link Frequency} object to be merged * @param other the other {@link Frequency} object to be merged
* @throws NullArgumentException if {@code other} is null
* @since 3.1 * @since 3.1
*/ */
public void merge(Frequency other) { public void merge(final Frequency other) throws NullArgumentException {
for (Iterator<Map.Entry<Comparable<?>, Long>> iter = other.entrySetIterator(); iter.hasNext();) { MathUtils.checkNotNull(other, LocalizedFormats.NULL_NOT_ALLOWED);
Map.Entry<Comparable<?>, Long> entry = iter.next();
final Iterator<Map.Entry<Comparable<?>, Long>> iter = other.entrySetIterator();
while (iter.hasNext()) {
final Map.Entry<Comparable<?>, Long> entry = iter.next();
incrementValue(entry.getKey(), entry.getValue()); incrementValue(entry.getKey(), entry.getValue());
} }
} }
@ -514,11 +520,14 @@ public class Frequency implements Serializable {
* by the counts represented by each of the others. * by the counts represented by each of the others.
* *
* @param others the other {@link Frequency} objects to be merged * @param others the other {@link Frequency} objects to be merged
* @throws NullArgumentException if the collection is null
* @since 3.1 * @since 3.1
*/ */
public void merge(Collection<Frequency> others) { public void merge(final Collection<Frequency> others) throws NullArgumentException {
for (Iterator<Frequency> iter = others.iterator(); iter.hasNext();) { MathUtils.checkNotNull(others, LocalizedFormats.NULL_NOT_ALLOWED);
merge(iter.next());
for (final Frequency freq : others) {
merge(freq);
} }
} }