/*
 * Bytecode Analysis Framework
 * Copyright (C) 2003, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.daveho.ba.bcp;

import java.util.*;

/**
 * A ByteCodePattern is a pattern matching a sequence of bytecode instructions.
 *
 * @see PatternElement
 * @see PatternMatcher
 * @author David Hovemeyer
 */
public class ByteCodePattern {
	private PatternElement first, last;
	private int interElementWild;
	private int dummyVariableCount;

	/**
	 * Add a PatternElement to the end of the pattern.
	 * @param element the PatternElement
	 * @return this object
	 */
	public ByteCodePattern add(PatternElement element) {
		if (first != null)
			addInterElementWild();
		addElement(element);
		return this;
	}

	/**
	 * Set number of inter-element wildcards to create between
	 * explicit PatternElements.  By default, no implicit wildcards
	 * are created.
	 * @param numWild the number of wildcard instructions which
	 *   may be matched between explicit PatternElements
	 * @return this object
	 */
	public ByteCodePattern setInterElementWild(int numWild) {
		this.interElementWild = numWild;
		return this;
	}

	/**
	 * Get the first PatternElement in the pattern.
	 */
	public PatternElement getFirst() { return first; }

	/**
	 * Get a dummy variable name.
	 * The name returned will begin with the <code>'$'</code> character,
	 * and will be different than any previous dummy variable name allocated
	 * by this object.  Dummy variable names are useful for creating
	 * PatternElements where you don't care whether the value it uses
	 * is the same as one used by another PatternElement.
	 */
	public String dummyVariable() {
		StringBuffer buf = new StringBuffer();
		buf.append("$_");
		buf.append(dummyVariableCount++);
		return buf.toString();
	}

	private void addInterElementWild() {
		if (interElementWild > 0)
			addElement(new Wild(interElementWild));
	}

	private void addElement(PatternElement element) {
		if (first == null) {
			first = last = element;
		} else {
			last.setNext(element);
			last = element;
		}
	}
}

// vim:ts=4

/*
 * Bytecode Analysis Framework
 * Copyright (C) 2003, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.daveho.ba.bcp;

import java.util.*;

/**
 * A ByteCodePattern is a pattern matching a sequence of bytecode instructions.
 *
 * @see PatternElement
 * @see PatternMatcher
 * @author David Hovemeyer
 */
public class ByteCodePattern {
	private PatternElement first, last;
	private int interElementWild;
	private int numElements;
	private int dummyVariableCount;

	/**
	 * Add a PatternElement to the end of the pattern.
	 * @param element the PatternElement
	 * @return this object
	 */
	public ByteCodePattern add(PatternElement element) {
		if (first != null)
			addInterElementWild();
		addElement(element);
		return this;
	}

	/**
	 * Set number of inter-element wildcards to create between
	 * explicit PatternElements.  By default, no implicit wildcards
	 * are created.
	 * @param numWild the number of wildcard instructions which
	 *   may be matched between explicit PatternElements
	 * @return this object
	 */
	public ByteCodePattern setInterElementWild(int numWild) {
		this.interElementWild = numWild;
		return this;
	}

	/**
	 * Get the first PatternElement in the pattern.
	 */
	public PatternElement getFirst() { return first; }

	/**
	 * Get a dummy variable name.
	 * The name returned will begin with the <code>'$'</code> character,
	 * and will be different than any previous dummy variable name allocated
	 * by this object.  Dummy variable names are useful for creating
	 * PatternElements where you don't care whether the value it uses
	 * is the same as one used by another PatternElement.
	 */
	public String dummyVariable() {
		StringBuffer buf = new StringBuffer();
		buf.append("$_");
		buf.append(dummyVariableCount++);
		return buf.toString();
	}

	private void addInterElementWild() {
		if (interElementWild > 0)
			addElement(new Wild(interElementWild));
	}

	private void addElement(PatternElement element) {
		element.setIndex(numElements++);
		if (first == null) {
			first = last = element;
		} else {
			last.setNext(element);
			last = element;
		}
	}
}

// vim:ts=4

Set index of added PatternElements, for better debugging.