View Javadoc

1   /*
2    * $Id: BooleanUtils.java,v 1.1 2004/12/15 14:18:20 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.util;
10  
11  import bexee.model.InvalidValueException;
12  
13  /***
14   * Utility class for boolean values. This class provides methods for the
15   * transformation of String values into boolean values.
16   * 
17   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:20 $
18   * @author Pawel Kowalski
19   */
20  public class BooleanUtils {
21  
22      private static final String INVALID_VALUE_EXCEPTION_STRING = "Value must be a \"YES\" or a \"NO\" String";
23  
24      /***
25       * Transform a "YES" or "NO" string to a boolean value. This method returns
26       * a "true" if the parameter String is "YES" in any other case, a "false"
27       * will be returned.
28       * 
29       * @param yesOrNoString
30       * @return a boolean value
31       */
32      public static boolean yesNoToBoolean(String yesOrNoString) {
33          if (yesOrNoString != null) {
34              return yesOrNoString.equalsIgnoreCase("YES");
35          } else {
36              return false;
37          }
38      }
39  
40      /***
41       * Transform a "YES" or "NO" string to a boolean value. If the parameter
42       * string doesn't match a "YES" or "NO" string (ignoring the case) this
43       * method will throw an InvalidValueException.
44       * 
45       * @param yesOrNoString
46       * @return @throws
47       *         InvalidValueException
48       */
49      public static boolean strictYesNoToBoolean(String yesOrNoString)
50              throws InvalidValueException {
51  
52          try {
53              if (yesOrNoString.equalsIgnoreCase("YES")) {
54                  return true;
55              } else if (yesOrNoString.equalsIgnoreCase("NO")) {
56                  return false;
57              }
58          } catch (NullPointerException e) {
59              throw new InvalidValueException(INVALID_VALUE_EXCEPTION_STRING, e);
60          }
61          throw new InvalidValueException(INVALID_VALUE_EXCEPTION_STRING);
62      }
63  }