AttributeList类的实现

 AttributeList类:

前面说到过Attribute类,而现在是AttributeList类,有什么关系呢。我的理解是:把Attribute看成是一个一维数组,而AttributeList就是一个二维数组。其中两个比较常用的方法是:1,get(int id)通过检索号获得Attribute;2,get(string name)通过name获得Attribute.

代码清单如下:

  1. package com.heaton.bot;
  2. /**
  3.  * The AttributeList class is used to store list of
  4.  * Attribute classes.
  5.  */
  6. import java.util.*;
  7. public class AttributeList extends Attribute implements Cloneable {
  8.   /**
  9.    * An internally used Vector.  This vector contains
  10.    * the entire(整个) list of attributes.
  11.    */
  12.   protected Vector vec;
  13.   /**
  14.    * Make an exact copy of this object using the cloneable interface.
  15.    *
  16.    * @return A new object that is a clone of the specified object.
  17.    */
  18.   public Object clone()
  19.   {
  20.     int i;
  21.     AttributeList rtn = new AttributeList();
  22.     for ( i=0;i<vec.size();i++ )
  23.       rtn.add( (Attribute)get(i).clone() );
  24.     return rtn;
  25.   }
  26.   /**
  27.    * Create a new, empty, AttributeList.
  28.    */
  29.   public AttributeList()
  30.   {
  31.     super("","");
  32.     vec = new Vector();
  33.   }
  34.   /**
  35.    * Locate an attribute by index number.  Index numbers
  36.    * start at zero.
  37.    *
  38.    * @param id A zero based index that specifies the attribute
  39.    * to retrieve(检索).
  40.    * @return The Attribute object that was found.
  41.    */
  42.   synchronized public Attribute get(int id)
  43.   {
  44.     if ( id<vec.size() )
  45.       return(Attribute)vec.elementAt(id);
  46.     else
  47.       return null;
  48.   }
  49.   /**
  50.    * Locate an attribute by its name.  The search is
  51.    * case-insensitive(不区分大小写).
  52.    *
  53.    * @param id The attribute name to search for.
  54.    * @return The Attribute object that was located.
  55.    */
  56.   synchronized public Attribute get(String id)
  57.   {
  58.     int i=0;
  59.     while ( get(i)!=null ) {
  60.       if ( get(i).getName().equalsIgnoreCase(id) )
  61.         return get(i);
  62.       i++;
  63.     }
  64.     return null;
  65.   }
  66.   /**
  67.    * Add the specified attribute to the list of attributes.
  68.    *
  69.    * @param a An attribute to add to this AttributeList.
  70.    */
  71.   synchronized public void add(Attribute a)
  72.   {
  73.     vec.addElement(a);
  74.   }
  75.   /**
  76.    * Clear all attributes from this AttributeList and return it
  77.    * to a empty state.
  78.    */
  79.   synchronized public void clear()
  80.   {
  81.     vec.removeAllElements();
  82.   }
  83.   /**
  84.    * Returns true of this AttributeList is empty, with no attributes.
  85.    *
  86.    * @return True if this AttributeList is empty, false otherwise.
  87.    */
  88.   synchronized public boolean isEmpty()
  89.   {
  90.     return( vec.size()<=0);
  91.   }
  92.   /**
  93.    * Returns the number of Attributes in this AttributeList.
  94.    *
  95.    * @return The number of Attributes in this AttributeList.
  96.    */
  97.   synchronized public int length()
  98.   {
  99.     return vec.size();
  100.   }
  101.   /**
  102.    * If there is already an attribute with the specified name,
  103.    * then it will have its value changed to match the specified value.
  104.    * If there is no Attribute with the specified name, then one will
  105.    * be created.  This method is case-insensitive.
  106.    *
  107.    * @param name The name of the Attribute to edit or create.  Case-insensitive.
  108.    * @param value The value to be held in this attribute.
  109.    */
  110.   synchronized public void set(String name,String value)
  111.   {
  112.     if ( name==null )
  113.       return;
  114.     if ( value==null )
  115.       value="";
  116.     Attribute a = get(name);
  117.     if ( a==null ) {
  118.       a = new Attribute(name,value);
  119.       add(a);
  120.     } else
  121.       a.setValue(value);
  122.   }
  123. }

OK!it's simple

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值