PatternSet.java :  » Parser » chaperon-3.0 » net » sourceforge » chaperon » model » extended » Java Open Source

Java Open Source » Parser » chaperon 3.0 
chaperon 3.0 » net » sourceforge » chaperon » model » extended » PatternSet.java
/*
 *  Copyright (C) Chaperon. All rights reserved.
 *  -------------------------------------------------------------------------
 *  This software is published under the terms of the Apache Software License
 *  version 1.1, a copy of which has been included  with this distribution in
 *  the LICENSE file.
 */

package net.sourceforge.chaperon.model.extended;

public class PatternSet
{
  private PatternSetEntry first = null;

  public PatternSet() {}

  public PatternSet(PatternIterator pattern)
  {
    while (pattern.hasNext())
      addPattern(pattern.next());
  }

  public boolean addPattern(Pattern pattern)
  {
    if (pattern==null)
      throw new NullPointerException();

    for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
      if (entry.pattern==pattern)
        return false;

    first = new PatternSetEntry(pattern, first);
    return true;
  }

  public boolean addPattern(PatternSet set)
  {
    boolean modified = false;
    for (PatternSetEntry entry = set.first; entry!=null; entry = entry.next)
      modified |= addPattern(entry.pattern);

    return modified;
  }

  public PatternIterator getPattern()
  {
    return new PatternSetEntryIterator(first);
  }

  public boolean contains(Pattern pattern)
  {
    for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
      if (entry.pattern==pattern)
        return true;

    return false;
  }

  public int getPatternCount()
  {
    int count = 0;
    for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
      count++;

    return count;
  }

  public void clear()
  {
    first = null;
  }

  public boolean equals(Object o)
  {
    if (o instanceof PatternSet)
    {
      PatternSet set = (PatternSet)o;

      if (set.getPatternCount()!=getPatternCount())
        return false;

      for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
        for (PatternSetEntry foreignentry = set.first; foreignentry!=null;
             foreignentry = foreignentry.next)
        {
          if (entry.pattern==foreignentry.pattern)
            break;

          if (foreignentry.next==null)
            return false;
        }

      return true;
    }

    return false;
  }

  public String toString()
  {
    StringBuffer buffer = new StringBuffer();

    buffer.append("{");
    for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
    {
      if (entry!=first)
        buffer.append(",");

      buffer.append(entry.pattern.toString());
    }

    buffer.append("}");
    return buffer.toString();
  }

  private class PatternSetEntry
  {
    public final Pattern pattern;
    public final PatternSetEntry next;

    private PatternSetEntry(Pattern pattern, PatternSetEntry next)
    {
      this.pattern = pattern;
      this.next = next;
    }
  }

  public class PatternSetEntryIterator implements PatternIterator
  {
    private PatternSetEntry entry = null;

    private PatternSetEntryIterator(PatternSetEntry entry)
    {
      this.entry = entry;
    }

    public boolean hasNext()
    {
      return entry!=null;
    }

    public Pattern next()
    {
      Pattern pattern = entry.pattern;
      this.entry = entry.next;
      return pattern;
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.