source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskInstance.java @ 1891

Last change on this file since 1891 was 1891, checked in by pharms, 9 years ago
  • remove support for tasks contexts in value measurements
File size: 4.8 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
16
17import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceVisitor;
20
21/**
22 * <p>
23 * this is the default implementation of the interface {@link ITaskInstance}. It
24 * does not do anything fancy except implementing the interface.
25 * </p>
26 *
27 * @author Patrick Harms
28 */
29class TaskInstance implements ITaskInstance {
30   
31    /**
32     * <p>
33     * default serial version UID
34     * </p>
35     */
36    private static final long serialVersionUID = 1L;
37
38    /**
39     * <p>
40     * used as a counter to generate new ids for each newly created task instance. May overflow.
41     * </p>
42     */
43    private static int temporalId = 0;
44
45    /**
46     * <p>
47     * the task instantiated by this task instance
48     * </p>
49     */
50    private ITask task;
51   
52    /**
53     * <p>
54     * the id of the task instance (unique throughout the system as long as {@link #temporalId}
55     * does not overflow.
56     * </p>
57     */
58    private int id;
59
60    /**
61     * <p>
62     * instantiated the task instance with the task that is instantiated by the instance. It also
63     * assigns a unique id to the instance using {@link #getNewId()}.
64     * </p>
65     */
66    TaskInstance(ITask task) {
67        this.task = task;
68        id = getNewId();
69    }
70
71    /**
72     * <p>
73     * creates a new id for a task instance using {@link #temporalId} by incrementing it an
74     * returning its current value. Resets the counter if {@link Integer.MAX_VALUE} is reached.
75     * </p>
76     *
77     * @return a new unique id for a task instance as long as {@link #temporalId} does not overflow
78     */
79    private static synchronized int getNewId() {
80        if (temporalId == Integer.MAX_VALUE) {
81            temporalId = 0;
82        }
83
84        return temporalId++;
85    }
86
87    /* (non-Javadoc)
88     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#getTask()
89     */
90    @Override
91    public ITask getTask() {
92        return task;
93    }
94
95    /*
96     * (non-Javadoc)
97     *
98     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeNode#equals(TaskTreeNode)
99     */
100    @Override
101    public boolean equals(ITaskInstance taskInstance) {
102        // task instances are only equal if they are identical or if they have the same id
103        // (may happen, if they are cloned)
104        return (this == taskInstance) ||
105            ((taskInstance != null) && (this.hashCode() == taskInstance.hashCode()));
106    }
107
108    /* (non-Javadoc)
109     * @see java.lang.Object#hashCode()
110     */
111    @Override
112    public synchronized int hashCode() {
113        return id;
114    }
115
116    /* (non-Javadoc)
117     * @see java.lang.Object#toString()
118     */
119    @Override
120    public synchronized String toString() {
121        StringBuffer result = new StringBuffer();
122        result.append(task.getType());
123        result.append(" #");
124        result.append(task.getId());
125       
126        if (task.getDescription() != null) {
127            result.append(" (");
128            result.append(task.getDescription());
129            result.append(')');
130        }
131       
132        /*if (children != null) {
133            result.append(", ");
134            result.append(children.size());
135            result.append(" children");
136        }*/
137       
138        return result.toString();
139    }
140
141    /* (non-Javadoc)
142     * @see java.lang.Object#clone()
143     */
144    @Override
145    public synchronized ITaskInstance clone() {
146        TaskInstance clone = null;
147        try {
148            clone = (TaskInstance) super.clone();
149        }
150        catch (CloneNotSupportedException e) {
151            // this should never happen. Therefore simply dump the exception
152            e.printStackTrace();
153        }
154
155        return clone;
156    }
157
158    /* (non-Javadoc)
159     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance#accept(ITaskInstanceVisitor)
160     */
161    @Override
162    public void accept(ITaskInstanceVisitor visitor) {
163        visitor.visit(this);
164    }
165
166    /**
167     * <p>
168     * used to update the task represented through this instance
169     * </p>
170     *
171     * @param task the task to set
172     */
173    void setTask(ITask task) {
174        this.task = task;
175    }
176
177}
Note: See TracBrowser for help on using the repository browser.