zoukankan      html  css  js  c++  java
  • Effective Java 26 Favor generic types

    Use generic types to replace the object declaration

    1. Add one or more type parameters to its declaration.
    2. Replace all the uses of the type Object with the appropriate type parameter.
    3. Handle the new E[] errorwith two ways :
      1. Use the explicit type casting in the constructor. And use @SuppressWarnings("unchecked") to suppress warning.

        // The elements array will contain only E instances from push(E).

        // This is sufficient to ensure type safety, but the runtime

        // type of the array won't be E[]; it will always be Object[]!

        @SuppressWarnings("unchecked")

        public Stack() {

        elements = (E[]) new Object[DEFAULT_INITIAL_CAPACITY];

        }

           

      2. Change the type of the field elements from E[]to Object[] and explicitly cast the popped element type to E.

        private Object[] elements;

           

        public Stack() {

        elements = new Object[DEFAULT_INITIAL_CAPACITY];

        }

        // Appropriate suppression of unchecked warning

        public E pop() {

        if (size==0)

        throw new EmptyStackException();

        // push requires elements to be of type E, so cast is correct

        @SuppressWarnings("unchecked") E result = (E) elements[--size];

        elements[size] = null; // Eliminate obsolete reference

        return result;

        }

       

     

    /**

    * @author kaibo

    *

    */

    // Object-based collection - a prime candidate for generics

    public class Stack<E> {

    private E[] elements;

    private int size = 0;

    private static final int DEFAULT_INITIAL_CAPACITY = 16;

       

    public Stack() {

    elements = (E[]) new Object[DEFAULT_INITIAL_CAPACITY];

    }

       

    public void push(E e) {

    ensureCapacity();

    elements[size++] = e;

    }

       

    public E pop() {

    if (size == 0)

    throw new EmptyStackException();

    E result = elements[--size];

    elements[size] = null; // Eliminate obsolete reference

    return result;

    }

       

    // no changes in isEmpty or ensureCapacity

    private void ensureCapacity() {

    if (elements.length == size)

    elements = Arrays.copyOf(elements, 2 * size + 1);

    }

       

    public static void main(String[] args) {

    Stack<Integer> s = new Stack<Integer>();

    s.push(1);

    s.push(2);

    s.push(3);

    System.out.println(s);

    }

    }

       

    Note

    1. You should use boxed primitive types instead of primitive type for the Generic type parameter.
    2. There are some generic types that restrict the permissible values of their type parameters. For example, consider java.util.concurrent.DelayQueue, whose declaration looks like this:

      class DelayQueue<E extends Delayed> implements BlockingQueue<E>

      The type parameter E is known as a bounded type parameter. Note that the subtype relation is defined so that every type is a subtype of itself [JLS, 4.10], so it is legal to create a DelayQueue<Delayed>

    3. Generify your existing types as time permits. This will make life easier for new users of these types without breaking existing clients.

    Summary

    Generic types are safer and easier to use than types that require casts in client code. When you design new types, make sure that they can be used without such casts. This will often mean making the types generic. Generify your existing types as time permits. This will make life easier for new users of these types without breaking existing clients (Item 23).

       

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    motion
    Owe Her
    优于自己的过去
    eclipse中开发js会卡,去掉.project中的validate即可
    项目经理的责任
    话不说满_话不说绝_要给自己留后路_留余地
    create table repo_folder_operate_log_bak as select * from repo_folder_operate_log;
    Introduction to Mathematical Thinking
    coursera 《现代操作系统》 -- 第十三周 期末考试
    coursera 《现代操作系统》 -- 第十一周 IO系统
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-generic-types.html
Copyright © 2011-2022 走看看