⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iclcomposite.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
package com.sri.oaa2.icl;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.ArrayList;
import java.util.zip.CRC32;

abstract class IclComposite extends IclTerm 
{
  abstract void addExtraCrcData(CRC32 inCrc);

  public IclComposite() 
  {
    super();
  }

  public IclComposite(int i) 
  {
    super(i);
  }

  public final boolean isAtomic() 
  {
    return false;
  }
  
  public final boolean isComposite() 
  {
    return true;
  }

  // deprecated, but eventually will be protected
  public final ArrayList toArrayList() 
  {
    // This should be overridden by those nodes that should never have
    // children.  You should probably use getTerm() or an
    // OaaPrologVisitor instead of doing this.  In the future, this
    // will probably be removed in favour of those other options.
    return getChildren();
  }

  public final int getNumChildren()
  {
    return this.getChildren().size();
  }

  final void setChildren(ArrayList c) 
  {
    checkWritable();
    children = c;
  }

  public void add(IclTerm n) throws UnsupportedOperationException
  {
    checkWritable();
    children.add(n);
  }

  public void add(int index, IclTerm n) throws UnsupportedOperationException
  {
    checkWritable();
    children.add(index, n);
  }

  public void addAll(IclTerm l) throws UnsupportedOperationException
  {
    checkWritable();
    ListIterator li = l.listIterator();
    if(li != null) {
      for(; li.hasNext();) {
	add((IclTerm)(li.next()));
      }
    }
  }

  public final void clearTerms() throws UnsupportedOperationException
  {
    checkWritable();
    children.clear();
  }

  protected final IclTerm getChild(int i) throws UnsupportedOperationException
  {
    return (IclTerm)children.get(i);
  }

  public final IclTerm getTerm(int i) throws UnsupportedOperationException
  {
    if((i < 0) || (i > (size() - 1))) {
      throw new IndexOutOfBoundsException("No such term at index " + i);
    }
    return getChild(i);
  }

  public final int size() 
  {
    return getNumChildren();
  }

  public final void removeAllUnifying(IclTerm n) throws UnsupportedOperationException
  {
    checkWritable();
    if(n != null) {
      ArrayList res = new ArrayList(getChildren().size());
      ListIterator li = getChildren().listIterator();
      IclTerm t;
      while(li.hasNext()) {
	t = (IclTerm)(li.next());
	if(Unifier.getInstance().unify(n, t) == null) {
	  res.add(t);
	}
      }
      setChildren(res);
    }
  }

  public final IclTerm removeUnifying(IclTerm n) throws UnsupportedOperationException
  {
    checkWritable();
    if(n != null) {
      ListIterator li = getChildren().listIterator();
      IclTerm t;
      while(li.hasNext()) {
	t = (IclTerm)(li.next());
	if(Unifier.getInstance().unify(n, t) != null) {
	  li.remove();
	  return t;
	}
      }
    }
    return null;
  }

  public final void removeElement(int i) throws UnsupportedOperationException
  {
    checkWritable();
    // ignore return value
    children.remove(i);
  }

  public final void replaceUnifying(IclTerm target, IclTerm replacement) throws UnsupportedOperationException
  {
    checkWritable();
    if(target == null) {
      return;
    }
    if(size() == 0) {
      return;
    }
    ListIterator li = getChildren().listIterator();
    IclTerm temp;
    while(li.hasNext()) {
      temp = (IclTerm)(li.next());
      if(Unifier.getInstance().unify(target, temp) != null) {
	li.set(replacement);
      }
    }
  }
  
  public final void replaceElement(int target, IclTerm replacement) throws IndexOutOfBoundsException, UnsupportedOperationException
  {
    checkWritable();
    if((target < 0) || (target > (size() - 1)))
    {
      throw new IndexOutOfBoundsException("No such element at " + target);
    }
    children.set(target, replacement);
    
  }

  public final Iterator iterator() throws UnsupportedOperationException
  {
    return listIterator();
  }

  public final ListIterator listIterator() throws UnsupportedOperationException
  {
    return GetListIterator.getInstance().from(this, null);
  }

  public boolean equals(Object o) 
  {
    if(o == this) {
      return true;
    }
    
    if(!(o instanceof IclComposite)) {
      return false;
    }
    
    IclComposite c = (IclComposite)o;
    if(c.hashCode() != this.hashCode()) {
      return false;
    }

    if(this.size() != c.size()) {
      return false;
    }
    
    Iterator rhs = c.iterator();
    Iterator lhs = this.iterator();
    IclTerm rht;
    IclTerm lht;
    while(lhs.hasNext()) {
      lht = (IclTerm)lhs.next();
      if(!rhs.hasNext()) {
        // this is actually a bigger error than this
        return false;
      }
      rht = (IclTerm)rhs.next();
      if(!lht.equals(rht)) {
        return false;
      }
    }
    return true;
  }

  public final int hashCode() 
  {
    CRC32 computedCrc = new CRC32();
    this.addExtraCrcData(computedCrc);
    Iterator it;
    IclTerm t;
    long v;
    for(it = this.iterator(); it.hasNext();) {
      t = (IclTerm)it.next();
      v = t.getCrc();
      computedCrc.update((byte)(0xff & (v >> 56)));
      computedCrc.update((byte)(0xff & (v >> 48)));
      computedCrc.update((byte)(0xff & (v >> 40)));
      computedCrc.update((byte)(0xff & (v >> 32)));
      computedCrc.update((byte)(0xff & (v >> 24)));
      computedCrc.update((byte)(0xff & (v >> 16)));
      computedCrc.update((byte)(0xff & (v >>  8)));
      computedCrc.update((byte)(0xff & v));
    }
    this.setCrc(computedCrc.getValue());
    return (int)this.getCrc();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -