01: //
02: //Copyright (C) 2005 United States Government as represented by the
03: //Administrator of the National Aeronautics and Space Administration
04: //(NASA). All Rights Reserved.
05: //
06: //This software is distributed under the NASA Open Source Agreement
07: //(NOSA), version 1.3. The NOSA has been approved by the Open Source
08: //Initiative. See the file NOSA-1.3-JPF at the top of the distribution
09: //directory tree for the complete NOSA document.
10: //
11: //THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12: //KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13: //LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14: //SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15: //A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16: //THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17: //DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18: //
19:
20: /*
21: * Created on Sep 8, 2005
22: *
23: * TODO To change the template for this generated file go to
24: * Window - Preferences - Java - Code Style - Code Templates
25: */
26: package gov.nasa.jpf.jvm.choice;
27:
28: import gov.nasa.jpf.Config;
29: import gov.nasa.jpf.jvm.IntChoiceGenerator;
30: import gov.nasa.jpf.jvm.JVM;
31: import gov.nasa.jpf.JPFException;
32:
33: /**
34: * @author jpenix
35: *
36: * choose from a set of values provided in configuration as
37: * xxx.class = IntChoiceFromSet
38: * xxx.values = {1, 2, 3, 400}
39: * where "xxx" is the choice id.
40: *
41: * choices can then made using: getInt("xxx");
42: */
43: public class IntChoiceFromSet extends IntChoiceGenerator {
44:
45: // int values to choose from stored as strings
46: String[] values;
47:
48: int count = -1;
49:
50: /**
51: * @param id
52: */
53: public IntChoiceFromSet(Config conf, String id) {
54: super (id);
55: values = conf.getStringArray(id + ".values");
56: if (values == null) {
57: throw new JPFException("value set for <" + id
58: + "> choice did not load");
59: }
60: }
61:
62: /* (non-Javadoc)
63: * @see gov.nasa.jpf.jvm.IntChoiceGenerator#getNextChoice()
64: */
65: public int getNextChoice(JVM vm) {
66:
67: try {
68: // watch out this returns 0 if it parses wrong
69: int ret = Integer.parseInt(values[count]);
70: vm.println(id + ": " + ret);
71: return ret;
72: } catch (NumberFormatException nfx) {
73: throw new JPFException("Number format error for <" + id
74: + ">: parsing \"" + values[count] + "\"");
75: }
76: }
77:
78: /* (non-Javadoc)
79: * @see gov.nasa.jpf.jvm.ChoiceGenerator#hasMoreChoices()
80: */
81: public boolean hasMoreChoices(JVM vm) {
82: if (count < values.length - 1)
83: return true;
84: else
85: return false;
86: }
87:
88: /* (non-Javadoc)
89: * @see gov.nasa.jpf.jvm.ChoiceGenerator#advance(gov.nasa.jpf.jvm.JVM)
90: */
91: public void advance(JVM vm) {
92: count++;
93: }
94:
95: }
|