KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > choice > IntChoiceFromSet


1  //
2
//Copyright (C) 2005 United States Government as represented by the
3
//Administrator of the National Aeronautics and Space Administration
4
//(NASA). All Rights Reserved.
5
//
6
//This software is distributed under the NASA Open Source Agreement
7
//(NOSA), version 1.3. The NOSA has been approved by the Open Source
8
//Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
//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  * @author jpenix
34  *
35  * choose from a set of values provided in configuration as
36  * xxx.class = IntChoiceFromSet
37  * xxx.values = {1, 2, 3, 400}
38  * where "xxx" is the choice id.
39  *
40  * choices can then made using: getInt("xxx");
41  */

42 public class IntChoiceFromSet extends IntChoiceGenerator {
43
44     // int values to choose from stored as strings
45
String JavaDoc[] values;
46     
47     int count = -1;
48     
49     /**
50      * @param id
51      */

52     public IntChoiceFromSet(Config conf, String JavaDoc id) {
53         super(id);
54         values = conf.getStringArray(id + ".values");
55         if (values == null) {
56             throw new JPFException("value set for <" + id + "> choice did not load");
57         }
58     }
59
60     /* (non-Javadoc)
61      * @see gov.nasa.jpf.jvm.IntChoiceGenerator#getNextChoice()
62      */

63     public int getNextChoice(JVM vm) {
64         
65         try {
66         // watch out this returns 0 if it parses wrong
67
int ret = Integer.parseInt(values[count]);
68             vm.println(id + ": " + ret);
69             return ret;
70           } catch (NumberFormatException JavaDoc nfx) {
71             throw new JPFException("Number format error for <" + id +">: parsing \"" + values[count] +"\"");
72           }
73     }
74
75     /* (non-Javadoc)
76      * @see gov.nasa.jpf.jvm.ChoiceGenerator#hasMoreChoices()
77      */

78     public boolean hasMoreChoices(JVM vm) {
79         if (count < values.length-1)
80             return true;
81         else
82             return false;
83     }
84
85     /* (non-Javadoc)
86      * @see gov.nasa.jpf.jvm.ChoiceGenerator#advance(gov.nasa.jpf.jvm.JVM)
87      */

88     public void advance(JVM vm) {
89         count++;
90     }
91
92 }
93
Popular Tags