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

org.dihedron.patterns.visitor.nodes.UnmodifiableArrayElementNode Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2012-2014, Andrea Funto'. All rights reserved.
 * 
 * This file is part of the Dihedron Common Utilities library ("Commons").
 *
 * "Commons" 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 3 of the License, or (at your option) 
 * any later version.
 *
 * "Commons" 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 "Commons". If not, see .
 */
package org.dihedron.patterns.visitor.nodes;

import org.dihedron.patterns.visitor.VisitorException;

/**
 * @author Andrea Funto'
 */
public class UnmodifiableArrayElementNode extends AbstractNode {
	
	/**
	 * The array to which this element belongs.
	 */
	protected Object[] array;
	
	/**
	 * The index of this object in the array.
	 */
	protected int index;
	
	/**
	 * Constructor.
	 * 
	 * @param name
	 *   the pseudo-OGNL path of the node.
	 * @param array
	 *   the array to which the node belongs.
	 * @param int index
	 *   the index at which the element is located in the array.
	 */
	public UnmodifiableArrayElementNode(String name, Object[] array, int index) {
		super(name);
		this.array = array.clone();
		this.index = index;
	}

	/**
	 * @see org.dihedron.patterns.visitor.nodes.AbstractNode#getValue()
	 */
	public Object getValue() throws VisitorException {
		// TODO: I might want to improve performances by caching the result; this
		// is OK under two assumptions: the iterator will reuse the nodes list 
		// it creates the first time for further iterations, and the visit is read 
		// only otherwise I would end up caching uselessly, or providing possibly
		// stale values to the user.
		return array[index];
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy