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.

Another exercise of Java generics:

import java.util.Collection;

/**
 * Created by IDEA on 16/11/14.
 */
public class Size {
    public static <T, I extends Iterable<T>> int size(I data) {
        final int size;
        if (data instanceof Collection) {
            size = ((Collection<T>) data).size();
        } else {
            int counter = 0;
            for(Object i : data) {
                counter++;
            }
            size = counter;
        }
        return size;
    }

    public static int size(int[] data) {
        return data.length;
    }

    public static int size(double[] data) {
        return data.length;
    }

    public static int size(String[] data) {
        return data.length;
    }
}




import java.util.ArrayList;
import java.util.List;

/**
 * Created by IDEA on 16/11/14.
 */
public class TestSize {
    public static void main(String[] args) {
        int[] x = {1, 2, 3};
        List<Integer> y = new ArrayList<>();
        y.add(1);
        y.add(2);
        System.out.println(Size.size(x));
        System.out.println(Size.size(y));
    }
}
share|improve this question
    
Follow-up question –  200_success Nov 17 '14 at 1:35

1 Answer 1

up vote 2 down vote accepted

For arrays, you are missing a generic int size(Object[] data) method, which can be written as:

public static <E> int size(E[] data) {
     return data.length;
}

This will apply to String[] as well. You will still need one for each kind of primitive though (boolean, short, int, long, float, double, did I miss anything?)

As for your other method...

public static <T, I extends Iterable<T>> int size(I data) {

This one can be cleaned up significantly.

You don't need the <T> here. In fact, you don't need the <I> either. It doesn't matter what kind of iterable it is so you can use Iterable<?>.

As you're not defining a variable of type I anywhere in your method, nor is using that type in another obscure way, all you're really interested in is that it is an Iterable<T>. So:

public static <T> int size(Iterable<T> data) {

Will have the same effect.

Now, are you using the type <T> ? No. In fact, you are iterating over Object so all you're interested in is that it is Iterable<? extends Object>. Which is the same as Iterable<?> since all classes extends Object.

So now we are left with this:

public static int size(Iterable<?> data) {
    final int size;
    if (data instanceof Collection) {
        size = ((Collection<?>) data).size();
    } else {
        int counter = 0;
        for (Object i : data) {
            counter++;
        }
        size = counter;
    }
    return size;
}

And when introducing early return, this:

public static int size(Iterable<?> data) {
    if (data instanceof Collection) {
        return ((Collection<?>) data).size();
    }
    int counter = 0;
    for (Object i : data) {
        counter++;
    }
    return counter;
}

Now that's a method.


Side note: The only method you have that is actually useful is int size(Iterable<?> data). Otherwise it would be easier to just use data.length or data.size();. However, I understand that this is just an exercise in which case it's fine.

share|improve this answer
    
What I am aiming at is an unified interface of get the size of an array-like object. Thanks for the review! A question, why can't T[] cover int[], short[] etc? –  qed Nov 16 '14 at 0:09
1  
@qed T[] is an array of some kind of objects. int[] is an array of ints. An int is not an object. An int is a primitive. –  Simon André Forsberg Nov 16 '14 at 0:43
    
@qed I don't really see a big benefit of having a unified interface for that. When you're using an array, you know that it's an array so you use .length. When you have a Collection, you know that it's a collection so you use .size(). When you have an Iterable, then you're most often not interested in counting the number of items in it, only iterating through them. –  Simon André Forsberg Nov 16 '14 at 0:46

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.