01: /*
02: * $Id: BooleanUtils.java,v 1.2 2004/11/19 16:07:31 kowap Exp $
03: *
04: * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
05: * Berne University of Applied Sciences
06: * School of Engineering and Information Technology
07: * All rights reserved.
08: */
09: 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.2 $, $Date: 2004/11/19 16:07:31 $
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(
60: INVALID_VALUE_EXCEPTION_STRING, e);
61: }
62: throw new InvalidValueException(INVALID_VALUE_EXCEPTION_STRING);
63: }
64: }
|