// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.tasktrees.treeimpl; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; /** *
* this is the default implementation of the interface {@link ITaskInstance}. It * does not do anything fancy except implementing the interface. *
* * @author Patrick Harms */ class TaskInstance implements ITaskInstance { /** ** default serial version UID *
*/ private static final long serialVersionUID = 1L; /** ** used as a counter to generate new ids for each newly created task instance. May overflow. *
*/ private static int temporalId = 0; /** ** the task instantiated by this task instance *
*/ private ITask task; /** ** the id of the task instance (unique throughout the system as long as {@link #temporalId} * does not overflow. *
*/ private int id; /** ** the children of this task instance which are task instances, as well *
*/ private List* instantiated the task instance with the task that is instantiated by the instance. It also * assigns a unique id to the instance using {@link #getNewId()}. *
*/ TaskInstance(ITask task) { this.task = task; id = getNewId(); } /** ** creates a new id for a task instance using {@link #temporalId} by incrementing it an * returning its current value. Resets the counter if {@link Integer.MAX_VALUE} is reached. *
* * @return a new unique id for a task instance as long as {@link #temporalId} does not overflow */ private static synchronized int getNewId() { if (temporalId == Integer.MAX_VALUE) { temporalId = 0; } return temporalId++; } /* (non-Javadoc) * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getTask() */ @Override public ITask getTask() { return task; } /* (non-Javadoc) * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getChildren() */ public synchronized List* used to add a child to this task instance *
* * @param child the new child of this instance */ synchronized void addChild(ITaskInstance child) { if (children == null) { children = new LinkedList* used to add a child to this task instance at a specific position *
* * @param index the position of the new child in the list of children * @param child the new child of this instance */ synchronized void addChild(int index, ITaskInstance child) { if (children == null) { children = new LinkedList* removes a child from this task instance at a specific position *
* * @param index the position of the child to be removed * * @return the child remove from the children of this instance */ synchronized ITaskInstance removeChild(int index) { if (children != null) { return children.remove(index); } else { throw new IllegalArgumentException ("this task instance does not have children that can be removed"); } } /** ** used to update the task represented through this instance *
* * @param task the task to set */ void setTask(ITask task) { this.task = task; } }