| /******************************************************************************* |
| * Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.core.simple.tempdata; |
| |
| /** |
| * Temporary data is valid as long as the state counter of the model |
| * (incremented at all model changes) is equal to the state when this temporary |
| * data was created. In other words, temporary data is a dependent information |
| * on the VPM model. |
| * |
| * @author Andras Schmidt |
| * |
| */ |
| public class TemporaryData<E> { |
| /** |
| * The state when this data is valid. |
| */ |
| int createdAtState; |
| |
| /** |
| * The data stored in this container. |
| */ |
| E o; |
| |
| /** |
| * Instantiate a container |
| * |
| * @param state |
| * The state when this data is valid. |
| * @param o |
| * The data stored in this container. |
| */ |
| TemporaryData(int state, E o) { |
| createdAtState = state; |
| this.o = o; |
| } |
| |
| /** |
| * Instantiate a container with data never valid |
| */ |
| TemporaryData() { |
| createdAtState = -1; |
| o = null; |
| } |
| |
| /** |
| * Check whether the data is valid at curren state. |
| * |
| * @param currentState |
| * the state of the VPM model |
| * @return true is data is still valid |
| */ |
| public boolean isValid(int currentState) { |
| if (currentState == createdAtState) |
| return o != null; |
| else { |
| o = null; |
| return false; |
| } |
| } |
| |
| /** |
| * The data stored in this container |
| * |
| * @param currentState |
| * the state of VPM model |
| * @return The data stored in this container or null if data is not valid |
| */ |
| public E getObject(int currentState) { |
| if (isValid(currentState)) |
| return o; |
| else { |
| o = null; |
| return null; |
| } |
| } |
| |
| /** |
| * Set the new value of the data |
| * |
| * @param state |
| * The state when this data is valid. |
| * @param o |
| * The data stored in this container. |
| */ |
| public void setObject(int currentState, E o) { |
| //this.discard(); // experimental |
| createdAtState = currentState; |
| this.o = o; |
| } |
| |
| /** |
| * Clear this tempdata object |
| * |
| * @return 1 if this container stored a value. 0 if not. |
| */ |
| public int discard() { |
| Object oldo = o; |
| o = null; |
| if (oldo != null) |
| return 1; |
| else |
| return 0; |
| } |
| } |