package edu.kit.iti.formal.pse.worthwhile.model;

import java.util.HashMap;
import java.util.Map;

import edu.kit.iti.formal.pse.worthwhile.model.ast.visitor.IValueVisitor;

/**
 * Represents a value consisting of ordered sub-values (e.g. an array).
 * 
 * @param <T>
 *                The type of the sub-values.
 * 
 * @author Joachim
 * 
 */
public class CompositeValue<T extends Value> extends Value {

	/**
	 * The sub-values of this composite value.
	 */
	private final Map<Integer, T> subValues;

	/**
	 * Returns the sub-values of this composite value.
	 * 
	 * @return The sub-values of this composite value.
	 */
	public final Map<Integer, T> getSubValues() {
		return this.subValues;
	}

	/**
	 * Creates a new instance of the {@link CompositeValue} class.
	 * 
	 * @param subValues
	 *                The sub-values of this value.
	 */
	public CompositeValue(final Map<Integer, T> subValues) {
		this.subValues = subValues;
	}
	
	/**
	 * Creates a new instance of the {@link CompositeValue} class.
	 * 
	 * @param subValues
	 *                The sub-values of this value.
	 */
	public CompositeValue(final T[] subValues) {
		this.subValues = new HashMap<Integer,T>();
		for (int i = 0; i < subValues.length; i++) {
			this.subValues.put(i, subValues[i]);
		}
	}

	@Override
	public final void accept(final IValueVisitor visitor) {
		visitor.visitCompositeValue(this);
	}

	@Override
        public final boolean equals(final Object other) {
		if (other instanceof CompositeValue<?>) {
			return this.subValues.equals(((CompositeValue<?>) other).getSubValues());
		} else {
			return false;
		}
	}
	
	@Override
        public final int hashCode() {
		int result = 0;
		
		for (Integer index : this.subValues.keySet()) {
			result += this.subValues.get(index).hashCode();
		}
		
		return result;
	}

}

@@ -932,18 +932,17 @@
 ues;%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -1167,16 +1167,17 @@
 Integer,
+ 
 T%3E();%0A%09%09
@@ -1265,24 +1265,925 @@
 %5D);%0A%09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Returns a new instance of the %7B@link CompositeValue%7D class with the value at index i replaced by (or newly%0A%09 * set to) the specified value.%0A%09 * %0A%09 * @param index%0A%09 *                The index at which to set the new value.%0A%09 * @param newValue%0A%09 *                The new value to set.%0A%09 * @return a new instance of the %7B@link CompositeValue%7D class with the value at index i replaced by (or newly%0A%09 *         set to) the specified value.%0A%09 */%0A%09public CompositeValue%3CT%3E replaceValue(int index, T newValue) %7B%0A%09%09// Copy the current values to a new composite value%0A%09%09Map%3CInteger, T%3E newValues = new HashMap%3CInteger, T%3E();%0A%09%09%0A%09%09for (Integer idx : this.subValues.keySet()) %7B%0A%09%09%09newValues.put(idx, this.getSubValues().get(idx));%0A%09%09%7D%0A%09%09%0A%09%09// Put the new value at the specified index.%0A%09%09newValues.put(index, newValue);%0A%09%09%0A%09%09// Return a new composite value.%0A%09%09return new CompositeValue%3CT%3E(newValues);%0A%09%7D%0A%0A
 %09@Override%0A%09
@@ -2286,32 +2286,25 @@
 %0A%09@Override%0A
-        
+%09
 public final
@@ -2497,18 +2497,17 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09@Overri
@@ -2509,24 +2509,17 @@
 verride%0A
-        
+%09
 public f
@@ -2558,19 +2558,17 @@
 lt = 0;%0A
-%09%09
 %0A
+
 %09%09for (I
@@ -2664,18 +2664,16 @@
 ();%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur

Add replaceValue() method to CompositeValue