/*
* 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;
/**
* This class represents a abstract list of pattern.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: PatternList.java,v 1.10 2004/01/08 11:30:52 benedikta Exp $
*/
public abstract class PatternList extends Pattern
{
private PatternListEntry first = null;
private PatternListEntry last = null;
private int count = 0;
/**
* Adds a pattern to this list
*
* @param element Pattern.
*/
public void addPattern(Pattern pattern)
{
if (first==null)
last = first = new PatternListEntry(pattern, null);
else
{
last.next = new PatternListEntry(pattern, last);
last = last.next;
}
count++;
}
/**
* Returns a pattern given by an index.
*
* @param index Index of the pattern
*
* @return Pattern
*/
public Pattern getPattern(int index)
{
int i = 0;
for (PatternListEntry entry = first; entry!=null; entry = entry.next, i++)
if (i==index)
return entry.pattern;
return null;
}
public PatternIterator getPattern()
{
return new PatternListEntryIterator(first);
}
public Pattern[] getPatternAsArray()
{
Pattern[] pattern = new Pattern[count];
int i = 0;
for (PatternListEntry entry = first; entry!=null; entry = entry.next, i++)
pattern[i] = entry.pattern;
return pattern;
}
/**
* Return the count of pattern in this list.
*
* @return Count of pattern.
*/
public int getPatternCount()
{
return count;
}
public boolean isNullable()
{
boolean nullable = true;
for (PatternListEntry entry = first; entry!=null; entry = entry.next)
nullable &= entry.pattern.isNullable();
return nullable;
}
public PatternSet getFirstSet()
{
PatternSet set = new PatternSet();
for (PatternListEntry entry = first; entry!=null; entry = entry.next)
{
set.addPattern(entry.pattern.getFirstSet());
if (!entry.pattern.isNullable())
break;
}
return set;
}
public PatternSet getLastSet()
{
PatternSet set = new PatternSet();
for (PatternListEntry entry = last; entry!=null; entry = entry.previous)
{
set.addPattern(entry.pattern.getLastSet());
if (!entry.pattern.isNullable())
break;
}
return set;
}
public void update()
{
for (PatternListEntry entry = first; entry!=null; entry = entry.next)
entry.pattern.update();
for (PatternListEntry entry = first; entry!=null; entry = entry.next)
{
for (PatternListEntry entry2 = entry.next; entry2!=null; entry2 = entry2.next)
{
for (PatternIterator i = entry.pattern.getLastSet().getPattern(); i.hasNext();)
{
Pattern lastPattern = i.next();
for (PatternIterator j = entry2.pattern.getFirstSet().getPattern(); j.hasNext();)
{
Pattern firstPattern = j.next();
lastPattern.addSuccessor(firstPattern);
}
}
if (!entry2.pattern.isNullable())
break;
}
}
}
public PatternSet getAllPattern()
{
PatternSet set = new PatternSet();
set.addPattern(this);
for (PatternListEntry entry = first; entry!=null; entry = entry.next)
set.addPattern(entry.pattern.getAllPattern());
return set;
}
public char[] getLimits()
{
return new char[0];
}
public boolean contains(char minimum, char maximum)
{
return false;
}
public boolean contains(char c)
{
return false;
}
public String getSymbol()
{
return null;
}
public String toString()
{
return toString(null, null);
}
public String toString(PatternSet previous, PatternSet next)
{
StringBuffer buffer = new StringBuffer();
if ((previous!=null) && (previous.contains(this)))
{
if ((next!=null) && (next.contains(this)))
buffer.append("."+toString()+".");
else
buffer.append(toString()+".");
}
else if ((next!=null) && (next.contains(this)))
buffer.append("."+toString());
if (count>1)
{
buffer.append("(");
for (PatternListEntry entry = first; entry!=null; entry = entry.next)
{
if (entry!=first)
buffer.append(" ");
buffer.append(entry.pattern.toString(previous, next));
}
buffer.append(")");
}
else if (count==1)
buffer.append(first.pattern.toString(previous, next));
return buffer.toString();
}
private class PatternListEntry
{
public final Pattern pattern;
public PatternListEntry previous = null;
public PatternListEntry next = null;
private PatternListEntry(Pattern pattern, PatternListEntry previous)
{
this.pattern = pattern;
this.previous = previous;
}
}
private class PatternListEntryIterator implements PatternIterator
{
private PatternListEntry entry;
private PatternListEntryIterator(PatternListEntry entry)
{
this.entry = entry;
}
public boolean hasNext()
{
return (entry!=null);
}
public Pattern next()
{
Pattern pattern = entry.pattern;
entry = entry.next;
return pattern;
}
}
}
|