package com.ejlchina.searcher.implement;

import com.ejlchina.searcher.FieldConvertor;
import com.ejlchina.searcher.FieldMeta;

import java.util.Arrays;
import java.util.Objects;

/**
 * [字符串 | 数字 to 布尔] 字段转换器
 * 与 {@link DefaultBeanReflector } 配合使用
 * @author Troy.Zhou @ 2021-11-01
 * @since v3.0.0
 */
public class BoolFieldConvertor implements FieldConvertor.BFieldConvertor {

	private String[] falseValues = new String[] { "0", "OFF", "FALSE", "N", "NO", "F" };

	@Override
	public boolean supports(FieldMeta meta, Class<?> valueType) {
		if (valueType == String.class || Number.class.isAssignableFrom(valueType)) {
			Class<?> targetType = meta.getType();
			return targetType == boolean.class || targetType == Boolean.class;
		}
		return false;
	}

	@Override
	public Object convert(FieldMeta meta, Object value) {
		if (value instanceof String) {
			String bool = (String) value;
			for (String t: falseValues) {
				if (t.equalsIgnoreCase(bool)) {
					return Boolean.FALSE;
				}
			}
			return Boolean.TRUE;
		}
		return ((Number) value).intValue() != 0;
	}

	public String[] getFalseValues() {
		return falseValues;
	}

	public void setFalseValues(String[] falseValues) {
		this.falseValues = Objects.requireNonNull(falseValues);
	}

	/**
	 * 追加假值
	 * @param falseValues 假值
	 * @since v3.1.0
	 */
	public void addFalseValues(String[] falseValues) {
		int newLength = falseValues.length + this.falseValues.length;
		this.falseValues = Arrays.copyOf(this.falseValues, newLength);
		System.arraycopy(falseValues, 0, this.falseValues, this.falseValues.length, falseValues.length);
	}

}

@@ -185,22 +185,55 @@
  * %5B
-%E5%AD%97%E7%AC%A6%E4%B8%B2 %7C %E6%95%B0%E5%AD%97 to %E5%B8%83%E5%B0%94
+ String %7C Number %7C Boolean to Boolean %7C boolean
 %5D %E5%AD%97%E6%AE%B5
@@ -644,16 +644,46 @@
 lueType)
+ %7C%7C Boolean.class == valueType
 ) %7B%0A%09%09%09C
@@ -875,24 +875,79 @@
 ct value) %7B%0A
+%09%09if (value instanceof Boolean) %7B%0A%09%09%09return value;%0A%09%09%7D%0A
 %09%09if (value 

增强 BoolFieldConvertor，使支持 Boolean -> boolean 方向的转换