MyBatis PropertyTokenizer

PropertyTokenizer

package org.apache.ibatis.reflection.property;
import java.util.Iterator;
/*
    例1: 参数: user[1].linkman.name
        children=linkman.name
        indexedName=user[1]
        name=user
        index=1
    例2: 参数: user
        children=null
        indexedName=user
        name=user
        index=null
    hasNext()判断是否有子属性,next()获取子属性。
*/
public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
  private String name; //名称
  private final String indexedName; //带索引的名称
  private String index; //索引
  private final String children; //子名称

  public PropertyTokenizer(String fullname) {
    int delim = fullname.indexOf('.');
    if (delim > -1) {
      name = fullname.substring(0, delim);
      children = fullname.substring(delim + 1);
    } else {
      name = fullname;
      children = null;
    }
    indexedName = name;
    delim = name.indexOf('[');
    if (delim > -1) {
      index = name.substring(delim + 1, name.length() - 1);
      name = name.substring(0, delim);
    }
  }


  @Override
  public boolean hasNext() {
    return children != null;
  }

  @Override
  public PropertyTokenizer next() {
    return new PropertyTokenizer(children);
  }

}

相关推荐