View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model.types;
17  
18  import java.util.Locale;
19  
20  /**
21   * @author Christian Bauer
22   */
23  public class BooleanDatatype extends AbstractDatatype<Boolean> {
24  
25      public BooleanDatatype() {
26      }
27  
28      @Override
29      public boolean isHandlingJavaType(Class type) {
30          return type == Boolean.TYPE || Boolean.class.isAssignableFrom(type);
31      }
32  
33      public Boolean valueOf(String s) throws InvalidValueException {
34          if (s.equals("")) return null;
35          if (s.equals("1") || s.toUpperCase(Locale.ROOT).equals("YES") || s.toUpperCase(Locale.ROOT).equals("TRUE")) {
36              return true;
37          } else if (s.equals("0") || s.toUpperCase(Locale.ROOT).equals("NO") || s.toUpperCase(Locale.ROOT).equals("FALSE")) {
38              return false;
39          } else {
40              throw new InvalidValueException("Invalid boolean value string: " + s);
41          }
42      }
43  
44      public String getString(Boolean value) throws InvalidValueException {
45          if (value == null) return "";
46          return value ? "1" : "0";
47      }
48  
49  }