Browse Source

添加 BigInteger 扩展

James 2 years ago
parent
commit
fdc4014948

+ 4 - 7
src/main/java/com/jfinal/template/expr/ast/MethodKit.java

@@ -18,19 +18,14 @@ package com.jfinal.template.expr.ast;
 
 import java.io.File;
 import java.lang.reflect.Method;
+import java.math.BigInteger;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import com.jfinal.kit.ReflectKit;
 import com.jfinal.kit.SyncWriteMap;
-import com.jfinal.template.ext.extensionmethod.ByteExt;
-import com.jfinal.template.ext.extensionmethod.DoubleExt;
-import com.jfinal.template.ext.extensionmethod.FloatExt;
-import com.jfinal.template.ext.extensionmethod.IntegerExt;
-import com.jfinal.template.ext.extensionmethod.LongExt;
-import com.jfinal.template.ext.extensionmethod.ShortExt;
-import com.jfinal.template.ext.extensionmethod.StringExt;
+import com.jfinal.template.ext.extensionmethod.*;
 
 /**
  * MethodKit
@@ -252,6 +247,8 @@ public class MethodKit {
 		addExtensionMethod(Double.class, new DoubleExt());
 		addExtensionMethod(Short.class, new ShortExt());
 		addExtensionMethod(Byte.class, new ByteExt());
+		
+		addExtensionMethod(BigInteger.class, new BigIntegerExt());
 	}
 	
 	public synchronized static void addExtensionMethod(Class<?> targetClass, Object objectOfExtensionClass) {

+ 59 - 0
src/main/java/com/jfinal/template/ext/extensionmethod/BigIntegerExt.java

@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) 2011-2023, James Zhan 詹波 (jfinal@126.com).
+ *
+ * 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 com.jfinal.template.ext.extensionmethod;
+
+import java.math.BigInteger;
+
+/**
+ * 针对 java.math.BigInteger 的扩展方法
+ *
+ * 用法:
+ * #if(value.toInt() == 123)
+ */
+public class BigIntegerExt {
+    public Boolean toBoolean(BigInteger self) {
+        return !self.equals(BigInteger.ZERO);
+    }
+
+    public Integer toInt(BigInteger self) {
+        return self.intValueExact();
+    }
+
+    public Long toLong(BigInteger self) {
+        return self.longValueExact();
+    }
+
+    public Float toFloat(BigInteger self) {
+        return self.floatValue();
+    }
+
+    public Double toDouble(BigInteger self) {
+        return self.doubleValue();
+    }
+
+    public Short toShort(BigInteger self) {
+        return self.shortValueExact();
+    }
+
+    public Byte toByte(BigInteger self) {
+        return self.byteValueExact();
+    }
+
+    public BigInteger toBigInteger(BigInteger self) {
+        return self;
+    }
+}