浏览代码

add methods

Looly 5 年之前
父节点
当前提交
c38e6f0c1a

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@
 ## 5.2.3
 ## 5.2.3
 
 
 ### 新特性
 ### 新特性
+* 【http   】     UserAgentUtil增加识别ios和android等(issue#781@Github)
 
 
 ### Bug修复
 ### Bug修复
 * 【core   】     修复PageUtil第一页语义不明确的问题(issue#782@Github)
 * 【core   】     修复PageUtil第一页语义不明确的问题(issue#782@Github)

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/convert/AbstractConverter.java

@@ -53,7 +53,7 @@ public abstract class AbstractConverter<T> implements Converter<T>, Serializable
 		if (null == defaultValue || targetType.isInstance(defaultValue)) {
 		if (null == defaultValue || targetType.isInstance(defaultValue)) {
 			if (targetType.isInstance(value) && false == Map.class.isAssignableFrom(targetType)) {
 			if (targetType.isInstance(value) && false == Map.class.isAssignableFrom(targetType)) {
 				// 除Map外,已经是目标类型,不需要转换(Map类型涉及参数类型,需要单独转换)
 				// 除Map外,已经是目标类型,不需要转换(Map类型涉及参数类型,需要单独转换)
-				return (T) targetType.cast(value);
+				return targetType.cast(value);
 			}
 			}
 			T result = convertInternal(value);
 			T result = convertInternal(value);
 			return ((null == result) ? defaultValue : result);
 			return ((null == result) ? defaultValue : result);

+ 2 - 1
hutool-core/src/main/java/cn/hutool/core/io/FastByteArrayOutputStream.java

@@ -34,6 +34,7 @@ public class FastByteArrayOutputStream extends OutputStream {
 		buffer = new FastByteBuffer(size);
 		buffer = new FastByteBuffer(size);
 	}
 	}
 
 
+	@SuppressWarnings("NullableProblems")
 	@Override
 	@Override
 	public void write(byte[] b, int off, int len) {
 	public void write(byte[] b, int off, int len) {
 		buffer.append(b, off, len);
 		buffer.append(b, off, len);
@@ -52,7 +53,7 @@ public class FastByteArrayOutputStream extends OutputStream {
 	 * 此方法无任何效果,当流被关闭后不会抛出IOException
 	 * 此方法无任何效果,当流被关闭后不会抛出IOException
 	 */
 	 */
 	@Override
 	@Override
-	public void close() throws IOException{
+	public void close() {
 		// nop
 		// nop
 	}
 	}
 
 

+ 13 - 1
hutool-core/src/main/java/cn/hutool/core/util/CharUtil.java

@@ -244,6 +244,7 @@ public class CharUtil {
 	 * @return true表示为字符类
 	 * @return true表示为字符类
 	 */
 	 */
 	public static boolean isChar(Object value) {
 	public static boolean isChar(Object value) {
+		//noinspection ConstantConditions
 		return value instanceof Character || value.getClass() == char.class;
 		return value instanceof Character || value.getClass() == char.class;
 	}
 	}
 
 
@@ -283,13 +284,14 @@ public class CharUtil {
 	 * @since 4.0.8
 	 * @since 4.0.8
 	 */
 	 */
 	public static boolean isEmoji(char c) {
 	public static boolean isEmoji(char c) {
+		//noinspection ConstantConditions
 		return false ==  ((c == 0x0) || //
 		return false ==  ((c == 0x0) || //
 				(c == 0x9) || //
 				(c == 0x9) || //
 				(c == 0xA) || //
 				(c == 0xA) || //
 				(c == 0xD) || //
 				(c == 0xD) || //
 				((c >= 0x20) && (c <= 0xD7FF)) || //
 				((c >= 0x20) && (c <= 0xD7FF)) || //
 				((c >= 0xE000) && (c <= 0xFFFD)) || //
 				((c >= 0xE000) && (c <= 0xFFFD)) || //
-				((c >= 0x10000) && (c <= 0x10FFFF)));
+				((c >= 0x100000) && (c <= 0x10FFFF)));
 	}
 	}
 	
 	
 	/**
 	/**
@@ -319,4 +321,14 @@ public class CharUtil {
 		}
 		}
 		return c1 == c2;
 		return c1 == c2;
 	}
 	}
+
+	/**
+	 * 获取字符类型
+	 * @param c 字符
+	 * @return 字符类型
+	 * @since 5.2.3
+	 */
+	public static int getType(int c){
+		return Character.getType(c);
+	}
 }
 }

+ 6 - 0
hutool-core/src/test/java/cn/hutool/core/util/CharUtilTest.java

@@ -20,4 +20,10 @@ public class CharUtilTest {
 		Assert.assertTrue(CharUtil.isEmoji(a.charAt(1)));
 		Assert.assertTrue(CharUtil.isEmoji(a.charAt(1)));
 		
 		
 	}
 	}
+
+	@Test
+	public void isCharTest(){
+		char[] a = new char[]{'a'};
+		Assert.assertTrue(CharUtil.isChar(a));
+	}
 }
 }

+ 86 - 12
hutool-http/src/main/java/cn/hutool/http/useragent/Platform.java

@@ -7,24 +7,55 @@ import cn.hutool.core.collection.CollUtil;
 
 
 /**
 /**
  * 平台对象
  * 平台对象
- * 
+ *
  * @author looly
  * @author looly
  * @since 4.2.1
  * @since 4.2.1
  */
  */
 public class Platform extends UserAgentInfo {
 public class Platform extends UserAgentInfo {
 
 
-	/** 未知 */
+	/**
+	 * 未知
+	 */
 	public static final Platform Unknown = new Platform(NameUnknown, null);
 	public static final Platform Unknown = new Platform(NameUnknown, null);
 
 
 	/**
 	/**
+	 * Iphone
+	 */
+	public static final Platform IPHONE = new Platform("iPhone", "iphone");
+	/**
+	 * ipod
+	 */
+	public static final Platform IPOD = new Platform("iPod", "ipod");
+	/**
+	 * ipad
+	 */
+	public static final Platform IPAD = new Platform("iPad", "ipad");
+
+	/**
+	 * android
+	 */
+	public static final Platform ANDROID = new Platform("Android", "android");
+	/**
+	 * android
+	 */
+	public static final Platform GOOGLE_TV = new Platform("GoogleTV", "googletv");
+
+	/**
+	 * Windows Phone
+	 */
+	public static final Platform WINDOWS_PHONE = new Platform("Windows Phone", "windows (ce|phone|mobile)( os)?");
+
+	/**
 	 * 支持的移动平台类型
 	 * 支持的移动平台类型
 	 */
 	 */
 	public static final List<Platform> mobilePlatforms = CollUtil.newArrayList(//
 	public static final List<Platform> mobilePlatforms = CollUtil.newArrayList(//
-			new Platform("Windows Phone", "windows (ce|phone|mobile)( os)?"), //
-			new Platform("iPad", "ipad"), //
-			new Platform("iPod", "ipod"), //
-			new Platform("iPhone", "iphone"), //
-			new Platform("Android", "android"), //
+			IPAD, //
+			IPOD, //
+			IPHONE, //
+			ANDROID, //
+			WINDOWS_PHONE, //
+			GOOGLE_TV, //
+			new Platform("htcFlyer", "htc_flyer"), //
 			new Platform("Symbian", "symbian(os)?"), //
 			new Platform("Symbian", "symbian(os)?"), //
 			new Platform("Blackberry", "blackberry") //
 			new Platform("Blackberry", "blackberry") //
 	);
 	);
@@ -32,7 +63,7 @@ public class Platform extends UserAgentInfo {
 	/**
 	/**
 	 * 支持的桌面平台类型
 	 * 支持的桌面平台类型
 	 */
 	 */
-	public static final List<Platform> desktopPlatforms=CollUtil.newArrayList(//
+	public static final List<Platform> desktopPlatforms = CollUtil.newArrayList(//
 			new Platform("Windows", "windows"), //
 			new Platform("Windows", "windows"), //
 			new Platform("Mac", "(macintosh|darwin)"), //
 			new Platform("Mac", "(macintosh|darwin)"), //
 			new Platform("Linux", "linux"), //
 			new Platform("Linux", "linux"), //
@@ -40,21 +71,22 @@ public class Platform extends UserAgentInfo {
 			new Platform("Playstation", "playstation"), //
 			new Platform("Playstation", "playstation"), //
 			new Platform("Java", "java") //
 			new Platform("Java", "java") //
 	);
 	);
-	
+
 	/**
 	/**
 	 * 支持的平台类型
 	 * 支持的平台类型
 	 */
 	 */
 	public static final List<Platform> platforms;
 	public static final List<Platform> platforms;
+
 	static {
 	static {
-		platforms=new ArrayList<Platform>(13);
+		platforms = new ArrayList<>(13);
 		platforms.addAll(mobilePlatforms);
 		platforms.addAll(mobilePlatforms);
 		platforms.addAll(desktopPlatforms);
 		platforms.addAll(desktopPlatforms);
 	}
 	}
 
 
 	/**
 	/**
 	 * 构造
 	 * 构造
-	 * 
-	 * @param name 平台名称
+	 *
+	 * @param name  平台名称
 	 * @param regex 关键字或表达式
 	 * @param regex 关键字或表达式
 	 */
 	 */
 	public Platform(String name, String regex) {
 	public Platform(String name, String regex) {
@@ -63,9 +95,51 @@ public class Platform extends UserAgentInfo {
 
 
 	/**
 	/**
 	 * 是否为移动平台
 	 * 是否为移动平台
+	 *
 	 * @return 是否为移动平台
 	 * @return 是否为移动平台
 	 */
 	 */
 	public boolean isMobile() {
 	public boolean isMobile() {
 		return mobilePlatforms.contains(this);
 		return mobilePlatforms.contains(this);
 	}
 	}
+
+	/**
+	 * 是否为Iphone或者iPod设备
+	 *
+	 * @return 是否为Iphone或者iPod设备
+	 * @since 5.2.3
+	 */
+	public boolean isIPhoneOrIPod() {
+		return IPHONE.equals(this) || IPOD.equals(this);
+	}
+
+	/**
+	 * 是否为Iphone或者iPod设备
+	 *
+	 * @return 是否为Iphone或者iPod设备
+	 * @since 5.2.3
+	 */
+	public boolean isIPad() {
+		return IPAD.equals(this);
+	}
+
+	/**
+	 * 是否为IOS平台,包括IPhone、IPod、IPad
+	 *
+	 * @return 是否为IOS平台,包括IPhone、IPod、IPad
+	 * @since 5.2.3
+	 */
+	public boolean isIos() {
+		return isIPhoneOrIPod() || isIPad();
+	}
+
+	/**
+	 * 是否为Android平台,包括Android和Google TV
+	 *
+	 * @return 是否为Android平台,包括Android和Google TV
+	 * @since 5.2.3
+	 */
+	public boolean isAndroid() {
+		return ANDROID.equals(this) || GOOGLE_TV.equals(this);
+	}
+
 }
 }

+ 2 - 0
hutool-http/src/main/java/cn/hutool/http/useragent/UserAgentUtil.java

@@ -17,4 +17,6 @@ public class UserAgentUtil {
 	public static UserAgent parse(String userAgentString) {
 	public static UserAgent parse(String userAgentString) {
 		return UserAgentParser.parse(userAgentString);
 		return UserAgentParser.parse(userAgentString);
 	}
 	}
+
+
 }
 }