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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Exception from System.loadLibrary(smjavaagentapi) java.lang.UnsatisfiedLinkError: no smjavaagentapi in java.library.path
    find ip from hostname or find hostname from ip
    SAML 2.0 Profiles--wiki
    BEA WebLogic平台下J2EE调优攻略--转载
    jvm在存储区域
    自己主动瀑布流布局和实现代码加载
    java 它 引用(基本类型的包装,构造函数和析构函数c++不同)
    动态规划——最长非降顺序排列
    3.Swift翻译教程系列——Swift基础知识
    ffmpeg.c简单的结构功能分析(平局)
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-generic-types.html
Copyright © 2011-2022 走看看