Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am making a GWT application and have to prevent client code from using null values in collections. I found an answer but GWT doesn't support Queue and its implementations as the GWT documentation says.

So, I had to make my own implementation.

import java.io.Serializable;
import java.util.ArrayList;

/**
 * This implementation of {@link ArrayList} does not allow client code add null values
 * 
 * @author Maxim Dmitriev
 * 
 * @param <E>
 */
public class NonNullArrayList<E> extends ArrayList<E> implements Serializable {

    private static final String NULL_VALUES_ARE_PROHIBITED = "Null values are prohibited";
    private static final String CLONING_IS_UNSUPPORTED = "Cloning is unsupported. Please support if necessary";

    public NonNullArrayList() {
        // For serialization. super() is called automatically.
    }

    public NonNullArrayList(int initialCapacity) {
        super(initialCapacity);
    }

    /**
     * 
     */
    private static final long serialVersionUID = 7716772103636691126L;

    public boolean add(E e) {
        if (e == null) {
            throw new IllegalArgumentException(NULL_VALUES_ARE_PROHIBITED);
        }
        return super.add(e);
    };

    public void add(int index, E element) {
        if (element == null) {
            throw new IllegalArgumentException(NULL_VALUES_ARE_PROHIBITED);
        }
        super.add(index, element);
    };

    public E set(int index, E element) {
        if (element == null) {
            throw new IllegalArgumentException(NULL_VALUES_ARE_PROHIBITED);
        }
        return super.set(index, element);
    };

    @Override
    public Object clone() {
        return new CloneNotSupportedException(CLONING_IS_UNSUPPORTED);
    }

}

Besides I want to prevent client code from using the clone() method because I just don't need it.

Is the implementation good or not?

Update #1

addAll()'s and the ArrayList(Collection<? extends E> c) constructor don't need to throw NPEs

Update #2

Updated according to JohnMark13's and tintinmj's suggestions.

Update #3

CloneNotSupportedException is not included in GWT. Supported classes.

@Override
public Object clone() {
    return new UnsupportedOperationException(CLONING_IS_UNSUPPORTED);
}
share|improve this question
 
Don't change the code. If you want to update the code, post it again without changing the previous code blocks. How can it be done?. –  tintinmj Sep 2 '13 at 13:47
 
@tintinmj, thank you for the link. I also read Markdown help and Collapsible code blocks? I haven't found how to make a collpasible code block here. I tried Gist and liked it. –  Maksim Dmitriev Sep 3 '13 at 6:15
 
If I where you, I could go for a decoractor (en.wikipedia.org/wiki/Decorator_pattern) pattern. –  The eternal newbie Oct 30 '13 at 12:45
add comment

2 Answers

up vote 6 down vote accepted
  • Throwing NullPointerException from methods like add, set are OK, cause the element to be added can't be null. But throwing NPE from addAll method is not cool. From Javadoc

    Throws: NullPointerException - if the specified collection is null

    that means if there exists a null value in the Collection throwing NPE doesn't make sense. You can throw IllegalArgumentException or a custom exception like FoundNullValueInCollectionException(I think this is better).

    In your defense you can point towards the Map.containsKey Javadoc, where it throws NPE if the specified key is null and this map does not permit null keys (optional).


share|improve this answer
 
thanks. But any exception can be caught. Errors can't be caught. –  Maksim Dmitriev Sep 2 '13 at 13:15
 
@RedPlanet and when did I say anything about Errors? –  tintinmj Sep 2 '13 at 13:43
2  
@RedPlanet of course Errors can be caught –  bowmore Sep 2 '13 at 13:46
add comment

In your case I think that you should consider the use of IllegalArgumentException it will save you a headache in the long run. When it comes to looking at code and trying to work out what broke and why NPEs are very well understood and very useful in debugging, but IAE describes your situation better.

The bottom line is that you have asked a question with no right or wrong answer and you will find very strong opinions form both camps - here is a good example of a short debate with many convincing opinions on StackOverflow

I'm not sure why the conversation around the clone method, if you want to extend ArrayList without clone you should throw the standard CloneNotSupportedException.

A similar discussion was had the other day, are you sure that you want to extend ArrayList at all, would you be better off having a private ArrayList member variable, delegating the methods that you care about to it and not exposing the others.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.