Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / choice / InvocationCG.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.vm.choice;
20
21 import gov.nasa.jpf.util.Invocation;
22 import gov.nasa.jpf.vm.ChoiceGenerator;
23 import gov.nasa.jpf.vm.ChoiceGeneratorBase;
24
25 import java.io.PrintWriter;
26 import java.util.List;
27 import java.util.ListIterator;
28
29 /**
30  * ChoiceGenerator that represents method calls
31  */
32 public class InvocationCG extends ChoiceGeneratorBase<Invocation> {
33
34   protected List<Invocation> invokes;
35   protected Invocation cur;
36   protected ListIterator<Invocation> it;
37   
38   public InvocationCG (String id, List<Invocation> invokes){
39     super(id);
40     
41     this.invokes = invokes;
42     
43     it = invokes.listIterator();
44   }
45   
46   @Override
47   public Invocation getChoice (int idx){
48     if (idx >=0 && idx < invokes.size()){
49       return invokes.get(idx);
50     } else {
51       throw new IllegalArgumentException("choice index out of range: " + idx);
52     }
53   }
54   
55   @Override
56   public void advance () {
57     cur = it.next();
58   }
59
60   @Override
61   public Class<Invocation> getChoiceType () {
62     return Invocation.class;
63   }
64
65   @Override
66   public Invocation getNextChoice () {
67     return cur;
68   }
69
70   @Override
71   public int getProcessedNumberOfChoices () {
72     return it.nextIndex();
73   }
74
75   @Override
76   public int getTotalNumberOfChoices () {
77     return invokes.size();
78   }
79
80   @Override
81   public boolean hasMoreChoices () {
82     return it.hasNext();
83   }
84
85   @Override
86   public ChoiceGenerator<Invocation> randomize () {
87     // <2do>
88     return this;
89   }
90
91   @Override
92   public String toString() {
93     StringBuilder sb = new StringBuilder(getClass().getName());
94     sb.append(" [");
95     int n = invokes.size();
96     for (int i=0; i<n; i++) {
97       if (i > 0) sb.append(',');
98       Invocation inv = invokes.get(i);
99       if (inv == cur) {
100         sb.append(MARKER);
101       }
102       sb.append(inv);
103     }
104     sb.append(']');
105     return sb.toString();
106   }
107   
108   public void printOn (PrintWriter pw) {
109     pw.print(toString());
110   }
111   
112   @Override
113   public void reset () {
114     cur = null;
115     it = invokes.listIterator();
116
117     isDone = false;
118   }
119
120 }