source: src/main/java/weka/core/SingleIndex.java @ 4

Last change on this file since 4 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.6 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    SingleIndex.java
19 *    Copyright (C) 2003 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.core;
24
25import java.io.Serializable;
26
27/**
28 * Class representing a single cardinal number. The number is set by a
29 * string representation such as: <P>
30 *
31 * <code>
32 *   first
33 *   last
34 *   1
35 *   3
36 * </code> <P>
37 * The number is internally converted from 1-based to 0-based (so methods that
38 * set or get numbers not in string format should use 0-based numbers).
39 *
40 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
41 * @version $Revision: 5953 $
42 */
43public class SingleIndex
44  implements Serializable, RevisionHandler {
45 
46  /** for serialization */
47  static final long serialVersionUID = 5285169134430839303L;
48
49  /** Record the string representation of the number */
50  protected /*@non_null spec_public@*/ String m_IndexString = "";
51
52  /** The selected index */
53  protected /*@ spec_public @*/ int m_SelectedIndex = -1;
54
55  /** Store the maximum value permitted. -1 indicates that no upper
56      value has been set */
57  protected /*@ spec_public @*/ int m_Upper = -1;
58
59  /**
60   * Default constructor.
61   *
62   */
63  //@ assignable m_IndexString, m_SelectedIndex, m_Upper;
64  //@ ensures m_SelectedIndex == -1;
65  //@ ensures m_Upper == -1;
66  public SingleIndex() {
67  }
68
69  /**
70   * Constructor to set initial index.
71   *
72   * @param index the initial index
73   * @throws IllegalArgumentException if the index is invalid
74   */
75  //@ assignable m_IndexString, m_SelectedIndex, m_Upper;
76  //@ ensures m_IndexString == index;
77  //@ ensures m_SelectedIndex == -1;
78  //@ ensures m_Upper == -1;
79  public SingleIndex(/*@non_null@*/ String index) {
80
81    setSingleIndex(index);
82  }
83
84  /**
85   * Sets the value of "last".
86   *
87   * @param newUpper the value of "last"
88   */
89  //@ assignable m_Upper, m_IndexString, m_SelectedIndex;
90  //@ ensures newUpper < 0 ==> m_Upper == \old(m_Upper);
91  //@ ensures newUpper >= 0 ==> m_Upper == newUpper;
92  public void setUpper(int newUpper) {
93
94    if (newUpper >= 0) {
95      m_Upper = newUpper;
96      setValue();
97    }
98  }
99
100  /**
101   * Gets the string representing the selected range of values
102   *
103   * @return the range selection string
104   */
105  //@ ensures \result == m_IndexString;
106  public /*@pure@*/ String getSingleIndex() {
107
108    return m_IndexString;
109  }
110
111  /**
112   * Sets the index from a string representation. Note that setUpper()
113   * must be called for the value to be actually set
114   *
115   * @param index the index set
116   * @throws IllegalArgumentException if the index was not well formed
117   */
118  //@ assignable m_IndexString, m_SelectedIndex;
119  //@ ensures m_IndexString == index;
120  //@ ensures m_SelectedIndex == -1;
121  public void setSingleIndex(/*@non_null@*/ String index) {
122
123    m_IndexString = index;
124    m_SelectedIndex = -1;
125  }
126
127  /**
128   * Constructs a representation of the current range. Being a string
129   * representation, the numbers are based from 1.
130   *
131   * @return the string representation of the current range
132   */
133  //@ also signals (RuntimeException e) \old(m_Upper) < 0;
134  //@ ensures \result != null;
135  public /*@pure@*/ String toString() {
136
137    if (m_IndexString.equals("")) {
138      return "No index set";
139    }
140    if (m_Upper == -1) {
141      throw new RuntimeException("Upper limit has not been specified");
142    }
143    return m_IndexString;
144  }
145
146  /**
147   * Gets the selected index
148   *
149   * @return the selected index
150   * @throws RuntimeException if the upper limit of the index hasn't been defined
151   */
152  //@ requires m_Upper >= 0;
153  //@ requires m_IndexString.length() > 0;
154  //@ ensures \result == m_SelectedIndex;
155  public /*@pure@*/ int getIndex() {
156
157    if (m_IndexString.equals("")) {
158      throw new RuntimeException("No index set");
159    }
160    if (m_Upper == -1) {
161      throw new RuntimeException("No upper limit has been specified for index");
162    }
163    return m_SelectedIndex;
164  }
165
166  /**
167   * Creates a string representation of the given index.
168   *
169   * @param index the index to turn into a string.
170   * Since the index will typically come from a program, indices are assumed
171   * from 0, and thus will have 1 added in the String representation.
172   * @return the string representation
173   */
174  //@ requires index >= 0;
175  public static /*@pure non_null@*/ String indexToString(int index) {
176
177    return "" + (index + 1);
178  }
179
180  /**
181   * Translates a single string selection into it's internal 0-based equivalent
182   */
183  //@ assignable m_SelectedIndex, m_IndexString;
184  protected void setValue() {
185
186    if (m_IndexString.equals("")) {
187      throw new RuntimeException("No index set");
188    }
189    if (m_IndexString.toLowerCase().equals("first")) {
190      m_SelectedIndex = 0;
191    } else if (m_IndexString.toLowerCase().equals("last")) {
192      m_SelectedIndex = m_Upper;
193    } else {
194      m_SelectedIndex = Integer.parseInt(m_IndexString) - 1;
195      if (m_SelectedIndex < 0) {
196        m_IndexString = "";
197        throw new IllegalArgumentException("Index must be greater than zero");
198      }
199      if (m_SelectedIndex > m_Upper) {
200        m_IndexString = "";
201        throw new IllegalArgumentException("Index is too large");
202      }
203    }
204  }
205 
206  /**
207   * Returns the revision string.
208   *
209   * @return            the revision
210   */
211  public String getRevision() {
212    return RevisionUtils.extract("$Revision: 5953 $");
213  }
214
215  /**
216   * Main method for testing this class.
217   *
218   * @param argv one parameter: a test index specification
219   */
220  //@ requires \nonnullelements(argv);
221  public static void main(/*@non_null@*/ String [] argv) {
222
223    try {
224      if (argv.length == 0) {
225        throw new Exception("Usage: SingleIndex <indexspec>");
226      }
227      SingleIndex singleIndex = new SingleIndex();
228      singleIndex.setSingleIndex(argv[0]);
229      singleIndex.setUpper(9);
230      System.out.println("Input: " + argv[0] + "\n"
231                         + singleIndex.toString());
232      int selectedIndex = singleIndex.getIndex();
233      System.out.println(selectedIndex + "");
234    } catch (Exception ex) {
235      ex.printStackTrace();
236      System.out.println(ex.getMessage());
237    }
238  }
239}
Note: See TracBrowser for help on using the repository browser.