Java Object to Boolean castToBoolean(String fieldValue)

Here you can find the source of castToBoolean(String fieldValue)

Description

Cast a string representation of a boolean value to a boolean primitive.

License

Open Source License

Parameter

Parameter Description
fieldValue the value of the boolean string field

Declaration

public static boolean castToBoolean(String fieldValue) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j av  a 2s  .c  o m
 * Copyright (C) 2014-2016 LinkedIn Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
 */

public class Main {
    /** 
     * Cast a string representation of a boolean value to a boolean primitive.
     * Used especially for Oracle representation of booleans as varchar2(1)
     * Returns true for values such as [t|true|yes|1] and false for [f|false|no].
     * If a boolean value cannot be trivially parsed, false is returned. 
     * 
     * @param fieldValue    the value of the boolean string field    
     */
    public static boolean castToBoolean(String fieldValue) {
        String lowerField = fieldValue.toLowerCase();

        switch (lowerField) {
        case "y":
            return true;
        case "n":
            return false;
        case "true":
            return true;
        case "false":
            return false;
        case "t":
            return true;
        case "f":
            return false;
        case "yes":
            return true;
        case "no":
            return false;
        case "0":
            return false;
        case "1":
            return true;
        }
        return false;
    }
}

Related

  1. castBoolean(Object o)
  2. castBoolean(Object o, boolean defaultValue)
  3. castBoolean(Object obj)
  4. castToBoolean(Object value)
  5. castToBoolean(Object value)
  6. objectToBoolean(Object o)
  7. objectToBoolean(Object o)
  8. objectToBoolean(Object Obj)
  9. objectToBoolean(Object p1)