Iterative mode MyBatis framework

Iterator pattern, has not used and will not use. MyBatis framework happens to use an iterative mode, and it appears simple, in future work, we can mimic it if necessary to dry routine.

Directly on the code

import java.util.Iterator;

/ ** 
 * @author Clinton the Begin
  * / 
public  class  PropertyTokenizer  the implements the Iterator <PropertyTokenizer> {
   Private String name;
   Private  Final String IndexedName;
   Private String index;
   Private Final String children; // relationship through two iterations before and after this the children property

  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);
    }
  }

  public String getName() {
    return name;
  }

  public String getIndex() {
    return index;
  }

  public String getIndexedName() {
    return indexedName;
  }

  public String getChildren() {
    return children;
  }

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

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

  @Override
  public void remove() {
    throw new UnsupportedOperationException("Remove is not supported, as it has no meaning in the context of properties.");
  }
}

 

Iterator interface is very easy to achieve an iterator come up, then you can use the method hasNext and next.

Business logic we do not control, just need to know when you call the next method, new PropertyTokenizer an example, and this example has a children property, hasNext way is through the children attribute to determine whether the air as the end of an iteration of the judgment conditions.

 

Regardless of the specific implementation, we only need to understand two things: 1. next needs doing; 2. hasNext How can I tell?

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12216021.html