All Downloads are FREE. Search and download functionalities are using the official Maven repository.

arjuna.lib.value.AbstractValueList Maven / Gradle / Ivy

Go to download

Arjuna-Java is the client implementation in Java for development of test automation using Arjuna. It uses TestNG as the test engine. With minor tweaks, it can be used with any other test engine or custom test automation implementations. Arjuna is a Python based test automation framework developed by Rahul Verma (www.rahulverma.net)

The newest version!
package arjuna.lib.value;

import java.util.ArrayList;
import java.util.List;

import arjuna.tpi.value.Value;

public class AbstractValueList implements RWValueList{
	
	private List values = null;
	
	public AbstractValueList() {
		this.values = new ArrayList();
	}
	
	public AbstractValueList(Object[] values) {
		this();
		this.addAllObjects(values);
	}

	public AbstractValueList(List objects) {
		this();
		this.addAllObjects(objects);
	}

	@Override
	public List values() throws Exception {
		return this.values;
	}

	protected int maxIndex() {
		return values.size() - 1;
	}
	
	private void validateIndex(int index) throws Exception {
		if (values.size() == 0){
			throw new EmptyValueListLookUpException(index);			
		} else {
			if (index > maxIndex()) {
				throw new ValueListLookUpException(index, maxIndex());
			}
		}
	}

	private void validateIndices(List indices) throws Exception {
		for (int index: indices) {
			validateIndex(index);
		}
	}
	
	@Override
	public List values(List indices) throws Exception {
		validateIndices(indices);
		List outValues = new ArrayList();
		for (int i=0; i < this.values.size(); i++) {
			if (indices.contains(i)) {
				outValues.add(this.values.get(i));
			}
		}
		
		return outValues;
	}
	
	private List convertToStringList(List inValues){
		List outValues = new ArrayList();
		for (Value value: inValues) {
			outValues.add(value.toString());
		}
		
		return outValues;		
	}

	@Override
	public List strings() throws Exception {
		return this.convertToStringList(this.values);
	}

	@Override
	public List strings(List indices) throws Exception {
		validateIndices(indices);
		List inValues = this.values(indices);
		return this.convertToStringList(inValues);
	}

	@Override
	public void addAll(List valueList) {
		this.values.addAll(valueList);
	}
	
	@Override
	public void addAllObjects(Object[] objects) {
		for (Object obj : objects) {
			this.addObject(obj);
		}
	}
	
	@Override
	public void addAllObjects(List objects) {
		for (Object obj : objects) {
			this.addObject(obj);
		}
	}

	@Override
	public void addAll(ValueList list) throws Exception {
		this.addAll(list.values());
	}

	@Override
	public void add(Value value) {
		this.values.add(value);
	}

	@Override
	public void addObject(Object obj) {
		this.add(new AnyRefValue(obj));
	}

	@Override
	public boolean hasIndex(int index)  throws Exception {
		return index < values.size();
	}

	@Override
	public Value value(int index) throws Exception {
		validateIndex(index);
		return this.values.get(index);
	}
	
	@Override
	public String strValue(int index) throws Exception {
		validateIndex(index);
		return value(index).toString();
	}

	@Override
	public Object object(int index) throws Exception {
		validateIndex(index);
		return value(index).object();
	}
	
	@Override
	public boolean isEmpty() throws Exception {
		return this.values.size() == 0;
	}

}