/*
 * Copyright 2014-2015 the original author or authors.
 *
 * 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. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.lastaflute.di.util;

/**
 * @author modified by jflute (originated in Seasar)
 */
public class LdiBooleanConversionUtil {

    protected LdiBooleanConversionUtil() {
    }

    /**
     * @param o
     * @return {@link Boolean}
     */
    public static Boolean toBoolean(Object o) {
        if (o == null) {
            return null;
        } else if (o instanceof Boolean) {
            return (Boolean) o;
        } else if (o instanceof Number) {
            int num = ((Number) o).intValue();
            return Boolean.valueOf(num != 0);
        } else if (o instanceof String) {
            String s = (String) o;
            if ("true".equalsIgnoreCase(s)) {
                return Boolean.TRUE;
            } else if ("false".equalsIgnoreCase(s)) {
                return Boolean.FALSE;
            } else if (s.equals("0")) {
                return Boolean.FALSE;
            } else {
                return Boolean.TRUE;
            }
        } else {
            return Boolean.TRUE;
        }
    }

    /**
     * @param o
     * @return boolean
     */
    public static boolean toPrimitiveBoolean(Object o) {
        Boolean b = toBoolean(o);
        if (b != null) {
            return b.booleanValue();
        }
        return false;
    }
}

@@ -802,71 +802,8 @@
  %7D%0A%0A
-    /**%0A     * @param o%0A     * @return %7B@link Boolean%7D%0A     */%0A
     
@@ -834,35 +834,76 @@
 Boolean(Object o
+bj
 ) %7B
+ // similar with DfTypeUtil.toBoolean()
 %0A        if (o =
@@ -900,16 +900,18 @@
    if (o
+bj
  == null
@@ -933,20 +933,29 @@
  return 
-null
+(Boolean) obj
 ;%0A      
@@ -960,32 +960,34 @@
     %7D else if (o
+bj
  instanceof Bool
@@ -1023,16 +1023,18 @@
 olean) o
+bj
 ;%0A      
@@ -1039,32 +1039,34 @@
     %7D else if (o
+bj
  instanceof Numb
@@ -1083,16 +1083,22 @@
         
+final 
 int num 
@@ -1110,16 +1110,18 @@
 umber) o
+bj
 ).intVal
@@ -1142,41 +1142,304 @@
     
-return Boolean.valueOf
+if (num == 1) %7B%0A                return Boolean.TRUE;%0A            %7D else if 
 (num 
-!
+=
 = 0)
-;
+ %7B%0A                return Boolean.FALSE;%0A            %7D else %7B%0A                String msg = %22Failed to parse the boolean number: number=%22 + num;%0A                throw new IllegalStateException(msg);%0A            %7D
 %0A   
@@ -1455,16 +1455,18 @@
 se if (o
+bj
  instanc
@@ -1495,16 +1495,22 @@
     
+final 
 String s
  = (
@@ -1505,16 +1505,18 @@
 String s
+tr
  = (Stri
@@ -1520,16 +1520,18 @@
 tring) o
+bj
 ;%0A      
@@ -1557,32 +1557,34 @@
 ualsIgnoreCase(s
+tr
 )) %7B%0A           
@@ -1658,16 +1658,18 @@
 reCase(s
+tr
 )) %7B%0A   
@@ -1731,18 +1731,30 @@
 f (s
+tr
 .equals
+IgnoreCase
 (%22
-0
+1
 %22)) 
@@ -1778,36 +1778,35 @@
  return Boolean.
-FALS
+TRU
 E;%0A            %7D
@@ -1802,32 +1802,63 @@
           %7D else
+ if (str.equalsIgnoreCase(%220%22))
  %7B%0A             
@@ -1867,35 +1867,36 @@
  return Boolean.
-TRU
+FALS
 E;%0A            %7D
@@ -1899,26 +1899,140 @@
    %7D
-%0A        %7D else %7B%0A
+ else if (str.equalsIgnoreCase(%22t%22)) %7B%0A                return Boolean.TRUE;%0A            %7D else if (str.equalsIgnoreCase(%22f%22)) %7B%0A    
     
@@ -2046,35 +2046,36 @@
  return Boolean.
-TRU
+FALS
 E;%0A        %7D%0A   
@@ -2073,71 +2073,287 @@
     
-%7D
+    %7D else %7B
 %0A    
-%7D%0A%0A
     
-/**%0A
      
-* @param o%0A     * @
+   String msg = %22Failed to parse the boolean string:%22;%0A                msg = msg + %22 value=%22 + str;%0A                throw new IllegalStateException(msg);%0A            %7D%0A        %7D else %7B%0A            
 return 
-b
+B
 oolean
-%0A     */
+.FALSE; // couldn't parse%0A        %7D%0A    %7D%0A
 %0A   

improve boolean handling (e.g. number)