| | |
| | | <old.spring.version>3.2.0.RELEASE</old.spring.version> |
| | | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| | | <java.version>1.8</java.version> |
| | | <lombok.version>1.18.22</lombok.version> |
| | | <jackson.version>2.13.3</jackson.version> |
| | | </properties> |
| | | |
| | | <!-- <distributionManagement> |
| | |
| | | <artifactId>vci-platform-web</artifactId> |
| | | |
| | | <dependencies> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-parent</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <type>pom</type> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci.corba</groupId> |
| | | <artifactId>plt-slice</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-parent</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <type>pom</type> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci.corba</groupId> |
| | | <artifactId>plt-slice</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-web</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>org.apache.logging.log4j</groupId> |
| | | <artifactId>*</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>com.google.guava</groupId> |
| | | <artifactId>guava</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-corba</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency><!--æä»¶çä¿¡æ¯--> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-file-api</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>org.apache.logging.log4j</groupId> |
| | | <artifactId>*</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-file-integration</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-word</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-web</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-poi</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-web</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>eu.bitwalker</groupId> |
| | | <artifactId>UserAgentUtils</artifactId> |
| | | <version>1.20</version> |
| | | </dependency> |
| | | <!--å¼å¯ cache ç¼å --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-cache</artifactId> |
| | | <version>2.1.3.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-web</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>org.apache.logging.log4j</groupId> |
| | | <artifactId>*</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>com.google.guava</groupId> |
| | | <artifactId>guava</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-corba</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency><!--æä»¶çä¿¡æ¯--> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-file-api</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>org.apache.logging.log4j</groupId> |
| | | <artifactId>*</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-file-integration</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-word</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-web</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-poi</artifactId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| | | </exclusion> |
| | | <exclusion> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-starter-web</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>eu.bitwalker</groupId> |
| | | <artifactId>UserAgentUtils</artifactId> |
| | | <version>1.20</version> |
| | | </dependency> |
| | | <!--å¼å¯ cache ç¼å --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-cache</artifactId> |
| | | <version>2.1.3.RELEASE</version> |
| | | </dependency> |
| | | |
| | | <!-- ehcacheç¼å --> |
| | | <dependency> |
| | | <groupId>net.sf.ehcache</groupId> |
| | | <artifactId>ehcache</artifactId> |
| | | <version>2.9.1</version><!--$NO-MVN-MAN-VER$ --> |
| | | </dependency> |
| | | <!-- ehcacheç¼å --> |
| | | <dependency> |
| | | <groupId>net.sf.ehcache</groupId> |
| | | <artifactId>ehcache</artifactId> |
| | | <version>2.9.1</version><!--$NO-MVN-MAN-VER$ --> |
| | | </dependency> |
| | | |
| | | <dependency><!--java bean åxml转æ¢--> |
| | | <groupId>com.thoughtworks.xstream</groupId> |
| | | <artifactId>xstream</artifactId> |
| | | <version>1.4.10</version> |
| | | </dependency> |
| | | <dependency><!--代ç çæå¨æé模æ¿--> |
| | | <artifactId>velocity</artifactId> |
| | | <groupId>org.apache.velocity</groupId> |
| | | <version>1.7</version> |
| | | </dependency> |
| | | <dependency><!--æ°æ®åºæä½å·¥å
·--> |
| | | <groupId>com.vci.platform</groupId> |
| | | <artifactId>ddlTool-client</artifactId> |
| | | <version>${vciplt.version}</version> |
| | | </dependency> |
| | | <dependency><!--æ°æ®åºæä½å·¥å
·--> |
| | | <groupId>com.vci.platform</groupId> |
| | | <artifactId>ddlTool-common</artifactId> |
| | | <version>${vciplt.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci.client</groupId> |
| | | <artifactId>plt-clientbase</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.zeroc</groupId> |
| | | <artifactId>icegridgui</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | <dependency><!--java bean åxml转æ¢--> |
| | | <groupId>com.thoughtworks.xstream</groupId> |
| | | <artifactId>xstream</artifactId> |
| | | <version>1.4.10</version> |
| | | </dependency> |
| | | <dependency><!--代ç çæå¨æé模æ¿--> |
| | | <artifactId>velocity</artifactId> |
| | | <groupId>org.apache.velocity</groupId> |
| | | <version>1.7</version> |
| | | </dependency> |
| | | <dependency><!--æ°æ®åºæä½å·¥å
·--> |
| | | <groupId>com.vci.platform</groupId> |
| | | <artifactId>ddlTool-client</artifactId> |
| | | <version>${vciplt.version}</version> |
| | | </dependency> |
| | | <dependency><!--æ°æ®åºæä½å·¥å
·--> |
| | | <groupId>com.vci.platform</groupId> |
| | | <artifactId>ddlTool-common</artifactId> |
| | | <version>${vciplt.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci.client</groupId> |
| | | <artifactId>plt-clientbase</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.zeroc</groupId> |
| | | <artifactId>icegridgui</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci.common</groupId> |
| | | <artifactId>plt-common</artifactId> |
| | | <version>1.0.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-platform-starter</artifactId> |
| | | <version>2022.1-SNAPSHOT</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.vci</groupId> |
| | | <artifactId>vci-platform-starter</artifactId> |
| | | <version>2022.1-SNAPSHOT</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.reflections</groupId> |
| | | <artifactId>reflections</artifactId> |
| | | <version>0.9.11</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.projectlombok</groupId> |
| | | <artifactId>lombok</artifactId> |
| | | <version>${lombok.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-core</artifactId> |
| | | <version>${jackson.version}</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.reflections</groupId> |
| | | <artifactId>reflections</artifactId> |
| | | <version>0.9.11</version> |
| | | </dependency> |
| | | |
| | | <!--redis start--> |
| | | <dependency> |
| | | <groupId>org.springframework.data</groupId> |
| | | <artifactId>spring-data-redis</artifactId> |
| | | <version>2.1.5.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>redis.clients</groupId> |
| | | <artifactId>jedis</artifactId> |
| | | <version>2.9.0</version> |
| | | </dependency> |
| | | <!--redis end--> |
| | | </dependencies> |
| | | <!--redis start--> |
| | | <dependency> |
| | | <groupId>org.springframework.data</groupId> |
| | | <artifactId>spring-data-redis</artifactId> |
| | | <version>2.1.5.RELEASE</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>redis.clients</groupId> |
| | | <artifactId>jedis</artifactId> |
| | | <version>2.9.0</version> |
| | | </dependency> |
| | | <!--redis end--> |
| | | </dependencies> |
| | | |
| | | <build> |
| | | <finalName>vci-platform-web</finalName> |
| | |
| | | </excludes> |
| | | </configuration> |
| | | </plugin> |
| | | <!-- <plugin>--> |
| | | <!-- <groupId>org.springframework.boot</groupId>--> |
| | | <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> |
| | | <!-- <configuration>--> |
| | | <!-- <!–éåå
å«ä¾èµï¼å
å«ä¸åå¨çä¾èµï¼jaréæ²¡æpoméçä¾èµ–>--> |
| | | <!-- <includes>--> |
| | | <!-- <include>--> |
| | | <!-- <groupId>null</groupId>--> |
| | | <!-- <artifactId>null</artifactId>--> |
| | | <!-- </include>--> |
| | | <!-- </includes>--> |
| | | <!-- <layout>ZIP</layout>--> |
| | | <!-- <!–使ç¨å¤é¨é
ç½®æä»¶ï¼jarå
éæ²¡æèµæºæä»¶–>--> |
| | | <!-- <addResources>true</addResources>--> |
| | | <!-- </configuration>--> |
| | | <!-- <executions>--> |
| | | <!-- <execution>--> |
| | | <!-- <goals>--> |
| | | <!-- <goal>repackage</goal>--> |
| | | <!-- </goals>--> |
| | | <!-- <configuration>--> |
| | | <!-- <!–é
ç½®jarå
ç¹æ®æ è¯ é
ç½®åï¼ä¿çåæä»¶ï¼çææ°æä»¶ *-run.jar –>--> |
| | | <!-- <!–é
ç½®jarå
ç¹æ®æ è¯ ä¸é
ç½®ï¼åæä»¶å½å为 *.jar.originalï¼çææ°æä»¶ *.jar –>--> |
| | | <!-- <!–<classifier>run</classifier>–>--> |
| | | <!-- </configuration>--> |
| | | <!-- </execution>--> |
| | | <!-- </executions>--> |
| | | <!-- </plugin>--> |
| | | <!-- <plugin>--> |
| | | <!-- <groupId>org.springframework.boot</groupId>--> |
| | | <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> |
| | | <!-- <configuration>--> |
| | | <!-- <!–éåå
å«ä¾èµï¼å
å«ä¸åå¨çä¾èµï¼jaréæ²¡æpoméçä¾èµ–>--> |
| | | <!-- <includes>--> |
| | | <!-- <include>--> |
| | | <!-- <groupId>null</groupId>--> |
| | | <!-- <artifactId>null</artifactId>--> |
| | | <!-- </include>--> |
| | | <!-- </includes>--> |
| | | <!-- <layout>ZIP</layout>--> |
| | | <!-- <!–使ç¨å¤é¨é
ç½®æä»¶ï¼jarå
éæ²¡æèµæºæä»¶–>--> |
| | | <!-- <addResources>true</addResources>--> |
| | | <!-- </configuration>--> |
| | | <!-- <executions>--> |
| | | <!-- <execution>--> |
| | | <!-- <goals>--> |
| | | <!-- <goal>repackage</goal>--> |
| | | <!-- </goals>--> |
| | | <!-- <configuration>--> |
| | | <!-- <!–é
ç½®jarå
ç¹æ®æ è¯ é
ç½®åï¼ä¿çåæä»¶ï¼çææ°æä»¶ *-run.jar –>--> |
| | | <!-- <!–é
ç½®jarå
ç¹æ®æ è¯ ä¸é
ç½®ï¼åæä»¶å½å为 *.jar.originalï¼çææ°æä»¶ *.jar –>--> |
| | | <!-- <!–<classifier>run</classifier>–>--> |
| | | <!-- </configuration>--> |
| | | <!-- </execution>--> |
| | | <!-- </executions>--> |
| | | <!-- </plugin>--> |
| | | |
| | | </plugins> |
| | | </build> |
| | |
| | | import com.vci.frameworkcore.lcstatuspck.FrameworkDataLCStatus; |
| | | import com.vci.frameworkcore.lcstatuspck.ReleaseDataLCStatus; |
| | | import com.vci.starter.revision.bo.TreeWrapperOptions; |
| | | import com.vci.starter.revision.service.RevisionModelUtil; |
| | | import com.vci.starter.web.annotation.bus.VciChangeDataAfter; |
| | | import com.vci.starter.web.annotation.bus.VciChangeDataBefore; |
| | | import com.vci.starter.web.annotation.bus.VciChangeDataPlugin; |
| | |
| | | @Override |
| | | @VciUnLog |
| | | public UITableDefineVO tableDO2VO(PortalVI portal, boolean queryDetail) { |
| | | if(){ |
| | | |
| | | } |
| | | UITableDefineVO tableDefineVO = new UITableDefineVO(); |
| | | tableDefineVO.setOid(portal.id); |
| | | tableDefineVO.setId(portal.viName); |
| | |
| | | throw new VciBaseException("{0}éçè¡¨æ ¼{1}ä¸åå¨", new String[]{btmType, componentDefineXO.getTemplateId()}); |
| | | } |
| | | componentVO.setTableDefineVO(tableDefineVOMap.get(key)); |
| | | |
| | | }else{ |
| | | try { |
| | | componentVO.setTableDefineVO(tableDO2VO(ServiceProvider.getUIService().getPortalVIByTypeNameAndVIName(btmType,componentDefineXO.getTemplateId()),true)); |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.support; |
| | | |
| | | import com.vci.web.util.StringUtil; |
| | | import com.vci.web.util.Func; |
| | | import com.vci.web.util.StringPool; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * å符串ååå¨ |
| | | * |
| | | * @author Looly |
| | | */ |
| | | public class StrSpliter { |
| | | |
| | | //---------------------------------------------------------------------------------------------- Split by char |
| | | |
| | | /** |
| | | * ååå符串路å¾ï¼ä»
æ¯æUnixåç符ï¼/ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> splitPath(String str) { |
| | | return splitPath(str, 0); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串路å¾ï¼ä»
æ¯æUnixåç符ï¼/ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static String[] splitPathToArray(String str) { |
| | | return toArray(splitPath(str)); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串路å¾ï¼ä»
æ¯æUnixåç符ï¼/ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param limit éå¶åçæ° |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> splitPath(String str, int limit) { |
| | | return split(str, StringPool.SLASH, limit, true, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串路å¾ï¼ä»
æ¯æUnixåç符ï¼/ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param limit éå¶åçæ° |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static String[] splitPathToArray(String str, int limit) { |
| | | return toArray(splitPath(str, limit)); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> splitTrim(String str, char separator, boolean ignoreEmpty) { |
| | | return split(str, separator, 0, true, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(String str, char separator, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, 0, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å¤§å°åææï¼å»é¤æ¯ä¸ªå
ç´ ä¸¤è¾¹ç©ºç½ç¬¦ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> splitTrim(String str, char separator, int limit, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, true, ignoreEmpty, false); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å¤§å°åææ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, isTrim, ignoreEmpty, false); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> splitIgnoreCase(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, isTrim, ignoreEmpty, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @param ignoreCase æ¯å¦å¿½ç¥å¤§å°å |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> split(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty, boolean ignoreCase) { |
| | | if (StringUtil.isEmpty(str)) { |
| | | return new ArrayList<String>(0); |
| | | } |
| | | if (limit == 1) { |
| | | return addToList(new ArrayList<String>(1), str, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | final ArrayList<String> list = new ArrayList<>(limit > 0 ? limit : 16); |
| | | int len = str.length(); |
| | | int start = 0; |
| | | for (int i = 0; i < len; i++) { |
| | | if (Func.equals(separator, str.charAt(i))) { |
| | | addToList(list, str.substring(start, i), isTrim, ignoreEmpty); |
| | | start = i + 1; |
| | | |
| | | //æ£æ¥æ¯å¦è¶
åºèå´ï¼æå¤§å
许limit-1个ï¼å©ä¸ä¸ä¸ªçç»æ«å°¾åç¬¦ä¸²ï¼ |
| | | if (limit > 0 && list.size() > limit - 2) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return addToList(list, str.substring(start, len), isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串为å符串æ°ç» |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static String[] splitToArray(String str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return toArray(split(str, separator, limit, isTrim, ignoreEmpty)); |
| | | } |
| | | |
| | | //---------------------------------------------------------------------------------------------- Split by String |
| | | |
| | | /** |
| | | * ååå符串ï¼ä¸å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(String str, String separator, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, -1, isTrim, ignoreEmpty, false); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤æ¯ä¸ªå
ç´ ä¸¤è¾¹ç©ºæ ¼ï¼å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> splitTrim(String str, String separator, boolean ignoreEmpty) { |
| | | return split(str, separator, true, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼ä¸å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, isTrim, ignoreEmpty, false); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤æ¯ä¸ªå
ç´ ä¸¤è¾¹ç©ºæ ¼ï¼å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param limit éå¶åçæ° |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> splitTrim(String str, String separator, int limit, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, true, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> splitIgnoreCase(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, isTrim, ignoreEmpty, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤æ¯ä¸ªå
ç´ ä¸¤è¾¹ç©ºæ ¼ï¼å¿½ç¥å¤§å°å |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param limit éå¶åçæ° |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> splitTrimIgnoreCase(String str, String separator, int limit, boolean ignoreEmpty) { |
| | | return split(str, separator, limit, true, ignoreEmpty, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符串 |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @param ignoreCase æ¯å¦å¿½ç¥å¤§å°å |
| | | * @return åååçéå |
| | | * @since 3.2.1 |
| | | */ |
| | | public static List<String> split(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty, boolean ignoreCase) { |
| | | if (StringUtil.isEmpty(str)) { |
| | | return new ArrayList<String>(0); |
| | | } |
| | | if (limit == 1) { |
| | | return addToList(new ArrayList<String>(1), str, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | if (StringUtil.isEmpty(separator)) { |
| | | return split(str, limit); |
| | | } else if (separator.length() == 1) { |
| | | return split(str, separator.charAt(0), limit, isTrim, ignoreEmpty, ignoreCase); |
| | | } |
| | | |
| | | final ArrayList<String> list = new ArrayList<>(); |
| | | int len = str.length(); |
| | | int separatorLen = separator.length(); |
| | | int start = 0; |
| | | int i = 0; |
| | | while (i < len) { |
| | | i = StringUtil.indexOf(str, separator, start, ignoreCase); |
| | | if (i > -1) { |
| | | addToList(list, str.substring(start, i), isTrim, ignoreEmpty); |
| | | start = i + separatorLen; |
| | | |
| | | //æ£æ¥æ¯å¦è¶
åºèå´ï¼æå¤§å
许limit-1个ï¼å©ä¸ä¸ä¸ªçç»æ«å°¾åç¬¦ä¸²ï¼ |
| | | if (limit > 0 && list.size() > limit - 2) { |
| | | break; |
| | | } |
| | | } else { |
| | | break; |
| | | } |
| | | } |
| | | return addToList(list, str.substring(start, len), isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串为å符串æ°ç» |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static String[] splitToArray(String str, String separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return toArray(split(str, separator, limit, isTrim, ignoreEmpty)); |
| | | } |
| | | |
| | | //---------------------------------------------------------------------------------------------- Split by Whitespace |
| | | |
| | | /** |
| | | * 使ç¨ç©ºç½ç¬¦ååå符串<br> |
| | | * åååçå符串两边ä¸å
å«ç©ºç½ç¬¦ï¼ç©ºä¸²æç©ºç½ç¬¦ä¸²å¹¶ä¸å为å
ç´ ä¹ä¸ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param limit éå¶åçæ° |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(String str, int limit) { |
| | | if (StringUtil.isEmpty(str)) { |
| | | return new ArrayList<String>(0); |
| | | } |
| | | if (limit == 1) { |
| | | return addToList(new ArrayList<String>(1), str, true, true); |
| | | } |
| | | |
| | | final ArrayList<String> list = new ArrayList<>(); |
| | | int len = str.length(); |
| | | int start = 0; |
| | | for (int i = 0; i < len; i++) { |
| | | if (Func.isEmpty(str.charAt(i))) { |
| | | addToList(list, str.substring(start, i), true, true); |
| | | start = i + 1; |
| | | if (limit > 0 && list.size() > limit - 2) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return addToList(list, str.substring(start, len), true, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串为å符串æ°ç» |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param limit éå¶åçæ° |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static String[] splitToArray(String str, int limit) { |
| | | return toArray(split(str, limit)); |
| | | } |
| | | |
| | | //---------------------------------------------------------------------------------------------- Split by regex |
| | | |
| | | /** |
| | | * éè¿æ£åååå符串 |
| | | * |
| | | * @param str å符串 |
| | | * @param separatorPattern åé符æ£å{@link Pattern} |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(String str, Pattern separatorPattern, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | if (StringUtil.isEmpty(str)) { |
| | | return new ArrayList<String>(0); |
| | | } |
| | | if (limit == 1) { |
| | | return addToList(new ArrayList<String>(1), str, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | if (null == separatorPattern) { |
| | | return split(str, limit); |
| | | } |
| | | |
| | | final Matcher matcher = separatorPattern.matcher(str); |
| | | final ArrayList<String> list = new ArrayList<>(); |
| | | int len = str.length(); |
| | | int start = 0; |
| | | while (matcher.find()) { |
| | | addToList(list, str.substring(start, matcher.start()), isTrim, ignoreEmpty); |
| | | start = matcher.end(); |
| | | |
| | | if (limit > 0 && list.size() > limit - 2) { |
| | | break; |
| | | } |
| | | } |
| | | return addToList(list, str.substring(start, len), isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æ£åååå符串为å符串æ°ç» |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separatorPattern åé符æ£å{@link Pattern} |
| | | * @param limit éå¶åçæ° |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static String[] splitToArray(String str, Pattern separatorPattern, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | return toArray(split(str, separatorPattern, limit, isTrim, ignoreEmpty)); |
| | | } |
| | | |
| | | //---------------------------------------------------------------------------------------------- Split by length |
| | | |
| | | /** |
| | | * æ ¹æ®ç»å®é¿åº¦ï¼å°ç»å®å符串æªå为å¤ä¸ªé¨å |
| | | * |
| | | * @param str å符串 |
| | | * @param len æ¯ä¸ä¸ªå°èçé¿åº¦ |
| | | * @return æªååçå符串æ°ç» |
| | | */ |
| | | public static String[] splitByLength(String str, int len) { |
| | | int partCount = str.length() / len; |
| | | int lastPartCount = str.length() % len; |
| | | int fixPart = 0; |
| | | if (lastPartCount != 0) { |
| | | fixPart = 1; |
| | | } |
| | | |
| | | final String[] strs = new String[partCount + fixPart]; |
| | | for (int i = 0; i < partCount + fixPart; i++) { |
| | | if (i == partCount + fixPart - 1 && lastPartCount != 0) { |
| | | strs[i] = str.substring(i * len, i * len + lastPartCount); |
| | | } else { |
| | | strs[i] = str.substring(i * len, i * len + len); |
| | | } |
| | | } |
| | | return strs; |
| | | } |
| | | |
| | | //---------------------------------------------------------------------------------------------------------- Private method start |
| | | |
| | | /** |
| | | * å°å符串å å
¥Listä¸ |
| | | * |
| | | * @param list å表 |
| | | * @param part 被å å
¥çé¨å |
| | | * @param isTrim æ¯å¦å»é¤ä¸¤ç«¯ç©ºç½ç¬¦ |
| | | * @param ignoreEmpty æ¯å¦ç¥è¿ç©ºå符串ï¼ç©ºå符串ä¸å为ä¸ä¸ªå
ç´ ï¼ |
| | | * @return å表 |
| | | */ |
| | | private static List<String> addToList(List<String> list, String part, boolean isTrim, boolean ignoreEmpty) { |
| | | part = part.toString(); |
| | | if (isTrim) { |
| | | part = part.trim(); |
| | | } |
| | | if (false == ignoreEmpty || false == part.isEmpty()) { |
| | | list.add(part); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * List转Array |
| | | * |
| | | * @param list List |
| | | * @return Array |
| | | */ |
| | | private static String[] toArray(List<String> list) { |
| | | return list.toArray(new String[list.size()]); |
| | | } |
| | | //---------------------------------------------------------------------------------------------------------- Private method end |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package com.vci.web.util; |
| | | |
| | | /** |
| | | * Base64å·¥å
· |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class Base64Util extends org.springframework.util.Base64Utils { |
| | | |
| | | /** |
| | | * ç¼ç |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String encode(String value) { |
| | | return Base64Util.encode(value, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * ç¼ç |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String encode(String value, java.nio.charset.Charset charset) { |
| | | byte[] val = value.getBytes(charset); |
| | | return new String(Base64Util.encode(val), charset); |
| | | } |
| | | |
| | | /** |
| | | * ç¼ç URLå®å
¨ |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String encodeUrlSafe(String value) { |
| | | return Base64Util.encodeUrlSafe(value, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * ç¼ç URLå®å
¨ |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String encodeUrlSafe(String value, java.nio.charset.Charset charset) { |
| | | byte[] val = value.getBytes(charset); |
| | | return new String(Base64Util.encodeUrlSafe(val), charset); |
| | | } |
| | | |
| | | /** |
| | | * è§£ç |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String decode(String value) { |
| | | return Base64Util.decode(value, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * è§£ç |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String decode(String value, java.nio.charset.Charset charset) { |
| | | byte[] val = value.getBytes(charset); |
| | | byte[] decodedValue = Base64Util.decode(val); |
| | | return new String(decodedValue, charset); |
| | | } |
| | | |
| | | /** |
| | | * è§£ç URLå®å
¨ |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String decodeUrlSafe(String value) { |
| | | return Base64Util.decodeUrlSafe(value, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * è§£ç URLå®å
¨ |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String decodeUrlSafe(String value, java.nio.charset.Charset charset) { |
| | | byte[] val = value.getBytes(charset); |
| | | byte[] decodedValue = Base64Util.decodeUrlSafe(val); |
| | | return new String(decodedValue, charset); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | /** |
| | | * char 叏鿱 |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public interface CharPool { |
| | | |
| | | // @formatter:off |
| | | char UPPER_A = 'A'; |
| | | char LOWER_A = 'a'; |
| | | char UPPER_Z = 'Z'; |
| | | char LOWER_Z = 'z'; |
| | | char DOT = '.'; |
| | | char AT = '@'; |
| | | char LEFT_BRACE = '{'; |
| | | char RIGHT_BRACE = '}'; |
| | | char LEFT_BRACKET = '('; |
| | | char RIGHT_BRACKET = ')'; |
| | | char DASH = '-'; |
| | | char PERCENT = '%'; |
| | | char PIPE = '|'; |
| | | char PLUS = '+'; |
| | | char QUESTION_MARK = '?'; |
| | | char EXCLAMATION_MARK = '!'; |
| | | char EQUALS = '='; |
| | | char AMPERSAND = '&'; |
| | | char ASTERISK = '*'; |
| | | char STAR = ASTERISK; |
| | | char BACK_SLASH = '\\'; |
| | | char COLON = ':'; |
| | | char COMMA = ','; |
| | | char DOLLAR = '$'; |
| | | char SLASH = '/'; |
| | | char HASH = '#'; |
| | | char HAT = '^'; |
| | | char LEFT_CHEV = '<'; |
| | | char NEWLINE = '\n'; |
| | | char N = 'n'; |
| | | char Y = 'y'; |
| | | char QUOTE = '\"'; |
| | | char RETURN = '\r'; |
| | | char TAB = '\t'; |
| | | char RIGHT_CHEV = '>'; |
| | | char SEMICOLON = ';'; |
| | | char SINGLE_QUOTE = '\''; |
| | | char BACKTICK = '`'; |
| | | char SPACE = ' '; |
| | | char TILDA = '~'; |
| | | char LEFT_SQ_BRACKET = '['; |
| | | char RIGHT_SQ_BRACKET = ']'; |
| | | char UNDERSCORE = '_'; |
| | | char ONE = '1'; |
| | | char ZERO = '0'; |
| | | // @formatter:on |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package com.vci.web.util; |
| | | |
| | | |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.charset.UnsupportedCharsetException; |
| | | |
| | | /** |
| | | * å符éå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class Charsets { |
| | | |
| | | /** |
| | | * å符éISO-8859-1 |
| | | */ |
| | | public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; |
| | | public static final String ISO_8859_1_NAME = ISO_8859_1.name(); |
| | | |
| | | /** |
| | | * å符éGBK |
| | | */ |
| | | public static final Charset GBK = Charset.forName(StringPool.GBK); |
| | | public static final String GBK_NAME = GBK.name(); |
| | | |
| | | /** |
| | | * å符éutf-8 |
| | | */ |
| | | public static final Charset UTF_8 = StandardCharsets.UTF_8; |
| | | public static final String UTF_8_NAME = UTF_8.name(); |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºCharset对象 |
| | | * |
| | | * @param charsetName å符éï¼ä¸ºç©ºåè¿åé»è®¤å符é |
| | | * @return Charsets |
| | | * @throws UnsupportedCharsetException ç¼ç 䏿¯æ |
| | | */ |
| | | public static Charset charset(String charsetName) throws UnsupportedCharsetException { |
| | | return StringUtil.isBlank(charsetName) ? Charset.defaultCharset() : Charset.forName(charsetName); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.lang.reflect.Array; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * éåå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class CollectionUtil extends CollectionUtils { |
| | | |
| | | /** |
| | | * Return {@code true} if the supplied Collection is not {@code null} or empty. |
| | | * Otherwise, return {@code false}. |
| | | * |
| | | * @param collection the Collection to check |
| | | * @return whether the given Collection is not empty |
| | | */ |
| | | public static boolean isNotEmpty(@Nullable Collection<?> collection) { |
| | | return !CollectionUtil.isEmpty(collection); |
| | | } |
| | | |
| | | /** |
| | | * Return {@code true} if the supplied Map is not {@code null} or empty. |
| | | * Otherwise, return {@code false}. |
| | | * |
| | | * @param map the Map to check |
| | | * @return whether the given Map is not empty |
| | | */ |
| | | public static boolean isNotEmpty(@Nullable Map<?, ?> map) { |
| | | return !CollectionUtil.isEmpty(map); |
| | | } |
| | | |
| | | /** |
| | | * Check whether the given Array contains the given element. |
| | | * |
| | | * @param array the Array to check |
| | | * @param element the element to look for |
| | | * @param <T> The generic tag |
| | | * @return {@code true} if found, {@code false} else |
| | | */ |
| | | public static <T> boolean contains(@Nullable T[] array, final T element) { |
| | | if (array == null) { |
| | | return false; |
| | | } |
| | | return Arrays.stream(array).anyMatch(x -> ObjectUtil.nullSafeEquals(x, element)); |
| | | } |
| | | |
| | | /** |
| | | * Concatenates 2 arrays |
| | | * |
| | | * @param one æ°ç»1 |
| | | * @param other æ°ç»2 |
| | | * @return æ°æ°ç» |
| | | */ |
| | | public static String[] concat(String[] one, String[] other) { |
| | | return concat(one, other, String.class); |
| | | } |
| | | |
| | | /** |
| | | * Concatenates 2 arrays |
| | | * |
| | | * @param one æ°ç»1 |
| | | * @param other æ°ç»2 |
| | | * @param clazz æ°ç»ç±» |
| | | * @return æ°æ°ç» |
| | | */ |
| | | public static <T> T[] concat(T[] one, T[] other, Class<T> clazz) { |
| | | T[] target = (T[]) Array.newInstance(clazz, one.length + other.length); |
| | | System.arraycopy(one, 0, target, 0, one.length); |
| | | System.arraycopy(other, 0, target, one.length, other.length); |
| | | return target; |
| | | } |
| | | |
| | | /** |
| | | * 对象æ¯å¦ä¸ºæ°ç»å¯¹è±¡ |
| | | * |
| | | * @param obj 对象 |
| | | * @return æ¯å¦ä¸ºæ°ç»å¯¹è±¡ï¼å¦æä¸º{@code null} è¿åfalse |
| | | */ |
| | | public static boolean isArray(Object obj) { |
| | | if (null == obj) { |
| | | return false; |
| | | } |
| | | return obj.getClass().isArray(); |
| | | } |
| | | |
| | | /** |
| | | * ä¸å¯å Set |
| | | * |
| | | * @param es 对象 |
| | | * @param <E> æ³å |
| | | * @return éå |
| | | */ |
| | | @SafeVarargs |
| | | public static <E> Set<E> ofImmutableSet(E... es) { |
| | | Objects.requireNonNull(es, "args es is null."); |
| | | return Arrays.stream(es).collect(Collectors.toSet()); |
| | | } |
| | | |
| | | /** |
| | | * ä¸å¯å List |
| | | * |
| | | * @param es 对象 |
| | | * @param <E> æ³å |
| | | * @return éå |
| | | */ |
| | | @SafeVarargs |
| | | public static <E> List<E> ofImmutableList(E... es) { |
| | | Objects.requireNonNull(es, "args es is null."); |
| | | return Arrays.stream(es).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * Iterable 转æ¢ä¸ºListéå |
| | | * |
| | | * @param elements Iterable |
| | | * @param <E> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <E> List<E> toList(Iterable<E> elements) { |
| | | Objects.requireNonNull(elements, "elements es is null."); |
| | | if (elements instanceof Collection) { |
| | | return new ArrayList((Collection) elements); |
| | | } |
| | | Iterator<E> iterator = elements.iterator(); |
| | | List<E> list = new ArrayList<>(); |
| | | while (iterator.hasNext()) { |
| | | list.add(iterator.next()); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * å°key value æ°ç»è½¬ä¸º map |
| | | * |
| | | * @param keysValues key value æ°ç» |
| | | * @param <K> key |
| | | * @param <V> value |
| | | * @return map éå |
| | | */ |
| | | public static <K, V> Map<K, V> toMap(Object... keysValues) { |
| | | int kvLength = keysValues.length; |
| | | if (kvLength % 2 != 0) { |
| | | throw new IllegalArgumentException("wrong number of arguments for met, keysValues length can not be odd"); |
| | | } |
| | | Map<K, V> keyValueMap = new HashMap<>(kvLength); |
| | | for (int i = kvLength - 2; i >= 0; i -= 2) { |
| | | Object key = keysValues[i]; |
| | | Object value = keysValues[i + 1]; |
| | | keyValueMap.put((K) key, (V) value); |
| | | } |
| | | return keyValueMap; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package com.vci.web.util; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Locale; |
| | | import java.util.Queue; |
| | | import java.util.TimeZone; |
| | | import java.util.concurrent.ConcurrentLinkedQueue; |
| | | |
| | | /** |
| | | * åètomcat8ä¸çå¹¶åDateFormat |
| | | * <p> |
| | | * {@link SimpleDateFormat}ç线ç¨å®å
¨å
è£
å¨ã |
| | | * ä¸ä½¿ç¨ThreadLocalï¼å建足å¤çSimpleDateFormatå¯¹è±¡æ¥æ»¡è¶³å¹¶åæ§è¦æ±ã |
| | | * </p> |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class ConcurrentDateFormat { |
| | | private final String format; |
| | | private final Locale locale; |
| | | private final TimeZone timezone; |
| | | private final Queue<SimpleDateFormat> queue = new ConcurrentLinkedQueue<>(); |
| | | |
| | | private ConcurrentDateFormat(String format, Locale locale, TimeZone timezone) { |
| | | this.format = format; |
| | | this.locale = locale; |
| | | this.timezone = timezone; |
| | | SimpleDateFormat initial = createInstance(); |
| | | queue.add(initial); |
| | | } |
| | | |
| | | public static ConcurrentDateFormat of(String format) { |
| | | return new ConcurrentDateFormat(format, Locale.getDefault(), TimeZone.getDefault()); |
| | | } |
| | | |
| | | public static ConcurrentDateFormat of(String format, TimeZone timezone) { |
| | | return new ConcurrentDateFormat(format, Locale.getDefault(), timezone); |
| | | } |
| | | |
| | | public static ConcurrentDateFormat of(String format, Locale locale, TimeZone timezone) { |
| | | return new ConcurrentDateFormat(format, locale, timezone); |
| | | } |
| | | |
| | | public String format(Date date) { |
| | | SimpleDateFormat sdf = queue.poll(); |
| | | if (sdf == null) { |
| | | sdf = createInstance(); |
| | | } |
| | | String result = sdf.format(date); |
| | | queue.add(sdf); |
| | | return result; |
| | | } |
| | | |
| | | public Date parse(String source) throws ParseException { |
| | | SimpleDateFormat sdf = queue.poll(); |
| | | if (sdf == null) { |
| | | sdf = createInstance(); |
| | | } |
| | | Date result = sdf.parse(source); |
| | | queue.add(sdf); |
| | | return result; |
| | | } |
| | | |
| | | private SimpleDateFormat createInstance() { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(format, locale); |
| | | sdf.setTimeZone(timezone); |
| | | return sdf; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | /** |
| | | * æ°æ®ç±»å转æ¢å·¥å
·ç±» |
| | | * |
| | | * @author Chill |
| | | */ |
| | | public class DatatypeConverterUtil { |
| | | |
| | | /** |
| | | * hexææ¬è½¬æ¢ä¸ºäºè¿å¶ |
| | | * |
| | | * @param hexStr hexææ¬ |
| | | * @return byte[] |
| | | */ |
| | | public static byte[] parseHexBinary(String hexStr) { |
| | | final int len = hexStr.length(); |
| | | |
| | | if (len % 2 != 0) { |
| | | throw new IllegalArgumentException("hexBinary needs to be even-length: " + hexStr); |
| | | } |
| | | |
| | | byte[] out = new byte[len / 2]; |
| | | |
| | | for (int i = 0; i < len; i += 2) { |
| | | int h = hexToBin(hexStr.charAt(i)); |
| | | int l = hexToBin(hexStr.charAt(i + 1)); |
| | | if (h == -1 || l == -1) { |
| | | throw new IllegalArgumentException("contains illegal character for hexBinary: " + hexStr); |
| | | } |
| | | |
| | | out[i / 2] = (byte) (h * 16 + l); |
| | | } |
| | | |
| | | return out; |
| | | } |
| | | |
| | | /** |
| | | * hexææ¬è½¬æ¢ä¸ºint |
| | | * |
| | | * @param ch hexææ¬ |
| | | * @return int |
| | | */ |
| | | private static int hexToBin(char ch) { |
| | | if ('0' <= ch && ch <= '9') { |
| | | return ch - '0'; |
| | | } |
| | | if ('A' <= ch && ch <= 'F') { |
| | | return ch - 'A' + 10; |
| | | } |
| | | if ('a' <= ch && ch <= 'f') { |
| | | return ch - 'a' + 10; |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package com.vci.web.util; |
| | | |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.Temporal; |
| | | import java.time.temporal.TemporalAccessor; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * DateTime å·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class DateTimeUtil { |
| | | public static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATETIME); |
| | | public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATE); |
| | | public static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern(DateUtil.PATTERN_TIME); |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTime(TemporalAccessor temporal) { |
| | | return DATETIME_FORMAT.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDate(TemporalAccessor temporal) { |
| | | return DATE_FORMAT.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatTime(TemporalAccessor temporal) { |
| | | return TIME_FORMAT.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String format(TemporalAccessor temporal, String pattern) { |
| | | return DateTimeFormatter.ofPattern(pattern).format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDateTime parseDateTime(String dateStr, String pattern) { |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| | | return DateTimeUtil.parseDateTime(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param formatter DateTimeFormatter |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDateTime parseDateTime(String dateStr, DateTimeFormatter formatter) { |
| | | return LocalDateTime.parse(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDateTime parseDateTime(String dateStr) { |
| | | return DateTimeUtil.parseDateTime(dateStr, DateTimeUtil.DATETIME_FORMAT); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDate parseDate(String dateStr, String pattern) { |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| | | return DateTimeUtil.parseDate(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param formatter DateTimeFormatter |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDate parseDate(String dateStr, DateTimeFormatter formatter) { |
| | | return LocalDate.parse(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¥æ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDate parseDate(String dateStr) { |
| | | return DateTimeUtil.parseDate(dateStr, DateTimeUtil.DATE_FORMAT); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param pattern æ¶é´æ£å |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalTime parseTime(String dateStr, String pattern) { |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| | | return DateTimeUtil.parseTime(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param formatter DateTimeFormatter |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalTime parseTime(String dateStr, DateTimeFormatter formatter) { |
| | | return LocalTime.parse(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalTime parseTime(String dateStr) { |
| | | return DateTimeUtil.parseTime(dateStr, DateTimeUtil.TIME_FORMAT); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´è½¬ Instant |
| | | * |
| | | * @param dateTime æ¶é´ |
| | | * @return Instant |
| | | */ |
| | | public static Instant toInstant(LocalDateTime dateTime) { |
| | | return dateTime.atZone(ZoneId.systemDefault()).toInstant(); |
| | | } |
| | | |
| | | /** |
| | | * Instant 转 æ¶é´ |
| | | * |
| | | * @param instant Instant |
| | | * @return Instant |
| | | */ |
| | | public static LocalDateTime toDateTime(Instant instant) { |
| | | return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æ date |
| | | * |
| | | * @param dateTime LocalDateTime |
| | | * @return Date |
| | | */ |
| | | public static Date toDate(LocalDateTime dateTime) { |
| | | return Date.from(DateTimeUtil.toInstant(dateTime)); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾2个æ¶é´å·®ï¼è·¨åº¦æ¯è¾å° |
| | | * |
| | | * @param startInclusive å¼å§æ¶é´ |
| | | * @param endExclusive ç»ææ¶é´ |
| | | * @return æ¶é´é´é |
| | | */ |
| | | public static Duration between(Temporal startInclusive, Temporal endExclusive) { |
| | | return Duration.between(startInclusive, endExclusive); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾2个æ¶é´å·®ï¼è·¨åº¦æ¯è¾å¤§ï¼å¹´ææ¥ä¸ºåä½ |
| | | * |
| | | * @param startDate å¼å§æ¶é´ |
| | | * @param endDate ç»ææ¶é´ |
| | | * @return æ¶é´é´é |
| | | */ |
| | | public static Period between(LocalDate startDate, LocalDate endDate) { |
| | | return Period.between(startDate, endDate); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import org.springframework.util.Assert; |
| | | |
| | | import java.text.ParseException; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.Temporal; |
| | | import java.time.temporal.TemporalAccessor; |
| | | import java.time.temporal.TemporalAmount; |
| | | import java.time.temporal.TemporalQuery; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.GregorianCalendar; |
| | | import java.util.TimeZone; |
| | | |
| | | /** |
| | | * æ¥æå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class DateUtil { |
| | | |
| | | public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss"; |
| | | public static final String PATTERN_DATETIME_MINI = "yyyyMMddHHmmss"; |
| | | public static final String PATTERN_DATE = "yyyy-MM-dd"; |
| | | public static final String PATTERN_TIME = "HH:mm:ss"; |
| | | /** |
| | | * è date æ ¼å¼å |
| | | */ |
| | | public static final ConcurrentDateFormat DATETIME_FORMAT = ConcurrentDateFormat.of(PATTERN_DATETIME); |
| | | public static final ConcurrentDateFormat DATETIME_MINI_FORMAT = ConcurrentDateFormat.of(PATTERN_DATETIME_MINI); |
| | | public static final ConcurrentDateFormat DATE_FORMAT = ConcurrentDateFormat.of(PATTERN_DATE); |
| | | public static final ConcurrentDateFormat TIME_FORMAT = ConcurrentDateFormat.of(PATTERN_TIME); |
| | | /** |
| | | * java 8 æ¶é´æ ¼å¼å |
| | | */ |
| | | public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATETIME); |
| | | public static final DateTimeFormatter DATETIME_MINI_FORMATTER = DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATETIME_MINI); |
| | | public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATE); |
| | | public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(DateUtil.PATTERN_TIME); |
| | | |
| | | /** |
| | | * è·åå½åæ¥æ |
| | | * |
| | | * @return å½åæ¥æ |
| | | */ |
| | | public static Date now() { |
| | | return new Date(); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å å¹´ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param yearsToAdd æ·»å çå¹´æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusYears(Date date, int yearsToAdd) { |
| | | return DateUtil.set(date, Calendar.YEAR, yearsToAdd); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å æ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param monthsToAdd æ·»å çææ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusMonths(Date date, int monthsToAdd) { |
| | | return DateUtil.set(date, Calendar.MONTH, monthsToAdd); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å å¨ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param weeksToAdd æ·»å ç卿° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusWeeks(Date date, int weeksToAdd) { |
| | | return DateUtil.plus(date, Period.ofWeeks(weeksToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å 天 |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param daysToAdd æ·»å çå¤©æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusDays(Date date, long daysToAdd) { |
| | | return DateUtil.plus(date, Duration.ofDays(daysToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å å°æ¶ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param hoursToAdd æ·»å çå°æ¶æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusHours(Date date, long hoursToAdd) { |
| | | return DateUtil.plus(date, Duration.ofHours(hoursToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å åé |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param minutesToAdd æ·»å çåéæ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusMinutes(Date date, long minutesToAdd) { |
| | | return DateUtil.plus(date, Duration.ofMinutes(minutesToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å ç§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param secondsToAdd æ·»å çç§æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusSeconds(Date date, long secondsToAdd) { |
| | | return DateUtil.plus(date, Duration.ofSeconds(secondsToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å æ¯«ç§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param millisToAdd æ·»å çæ¯«ç§æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusMillis(Date date, long millisToAdd) { |
| | | return DateUtil.plus(date, Duration.ofMillis(millisToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ·»å çº³ç§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param nanosToAdd æ·»å ççº³ç§æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plusNanos(Date date, long nanosToAdd) { |
| | | return DateUtil.plus(date, Duration.ofNanos(nanosToAdd)); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ·»å æ¶é´é |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param amount æ¶é´é |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date plus(Date date, TemporalAmount amount) { |
| | | Instant instant = date.toInstant(); |
| | | return Date.from(instant.plus(amount)); |
| | | } |
| | | |
| | | /** |
| | | * åå°å¹´ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param years åå°çå¹´æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusYears(Date date, int years) { |
| | | return DateUtil.set(date, Calendar.YEAR, -years); |
| | | } |
| | | |
| | | /** |
| | | * åå°æ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param months åå°çææ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusMonths(Date date, int months) { |
| | | return DateUtil.set(date, Calendar.MONTH, -months); |
| | | } |
| | | |
| | | /** |
| | | * åå°å¨ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param weeks åå°ç卿° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusWeeks(Date date, int weeks) { |
| | | return DateUtil.minus(date, Period.ofWeeks(weeks)); |
| | | } |
| | | |
| | | /** |
| | | * åå°å¤© |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param days åå°çå¤©æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusDays(Date date, long days) { |
| | | return DateUtil.minus(date, Duration.ofDays(days)); |
| | | } |
| | | |
| | | /** |
| | | * åå°å°æ¶ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param hours åå°çå°æ¶æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusHours(Date date, long hours) { |
| | | return DateUtil.minus(date, Duration.ofHours(hours)); |
| | | } |
| | | |
| | | /** |
| | | * åå°åé |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param minutes åå°çåéæ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusMinutes(Date date, long minutes) { |
| | | return DateUtil.minus(date, Duration.ofMinutes(minutes)); |
| | | } |
| | | |
| | | /** |
| | | * åå°ç§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param seconds åå°çç§æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusSeconds(Date date, long seconds) { |
| | | return DateUtil.minus(date, Duration.ofSeconds(seconds)); |
| | | } |
| | | |
| | | /** |
| | | * åå°æ¯«ç§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param millis åå°çæ¯«ç§æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusMillis(Date date, long millis) { |
| | | return DateUtil.minus(date, Duration.ofMillis(millis)); |
| | | } |
| | | |
| | | /** |
| | | * åå°çº³ç§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param nanos åå°ççº³ç§æ° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minusNanos(Date date, long nanos) { |
| | | return DateUtil.minus(date, Duration.ofNanos(nanos)); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æåå°æ¶é´é |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param amount æ¶é´é |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | public static Date minus(Date date, TemporalAmount amount) { |
| | | Instant instant = date.toInstant(); |
| | | return Date.from(instant.minus(amount)); |
| | | } |
| | | |
| | | /** |
| | | * è®¾ç½®æ¥æå±æ§ |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param calendarField æ´æ¹ç屿§ |
| | | * @param amount æ´æ¹æ°ï¼-1表示åå° |
| | | * @return 设置åçæ¶é´ |
| | | */ |
| | | private static Date set(Date date, int calendarField, int amount) { |
| | | Assert.notNull(date, "The date must not be null"); |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setLenient(false); |
| | | c.setTime(date); |
| | | c.add(calendarField, amount); |
| | | return c.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTime(Date date) { |
| | | return DATETIME_FORMAT.format(date); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTimeMini(Date date) { |
| | | return DATETIME_MINI_FORMAT.format(date); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDate(Date date) { |
| | | return DATE_FORMAT.format(date); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´æ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatTime(Date date) { |
| | | return TIME_FORMAT.format(date); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String format(Date date, String pattern) { |
| | | return ConcurrentDateFormat.of(pattern).format(date); |
| | | } |
| | | |
| | | /** |
| | | * java8 æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTime(TemporalAccessor temporal) { |
| | | return DATETIME_FORMATTER.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * java8 æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTimeMini(TemporalAccessor temporal) { |
| | | return DATETIME_MINI_FORMATTER.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * java8 æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDate(TemporalAccessor temporal) { |
| | | return DATE_FORMATTER.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * java8 æ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatTime(TemporalAccessor temporal) { |
| | | return TIME_FORMATTER.format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * java8 æ¥ææ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String format(TemporalAccessor temporal, String pattern) { |
| | | return DateTimeFormatter.ofPattern(pattern).format(temporal); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ¶é´ |
| | | */ |
| | | public static Date parse(String dateStr, String pattern) { |
| | | ConcurrentDateFormat format = ConcurrentDateFormat.of(pattern); |
| | | try { |
| | | return format.parse(dateStr); |
| | | } catch (ParseException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param format ConcurrentDateFormat |
| | | * @return æ¶é´ |
| | | */ |
| | | public static Date parse(String dateStr, ConcurrentDateFormat format) { |
| | | try { |
| | | return format.parse(dateStr); |
| | | } catch (ParseException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ¶é´ |
| | | */ |
| | | public static <T> T parse(String dateStr, String pattern, TemporalQuery<T> query) { |
| | | return DateTimeFormatter.ofPattern(pattern).parse(dateStr, query); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´è½¬ Instant |
| | | * |
| | | * @param dateTime æ¶é´ |
| | | * @return Instant |
| | | */ |
| | | public static Instant toInstant(LocalDateTime dateTime) { |
| | | return dateTime.atZone(ZoneId.systemDefault()).toInstant(); |
| | | } |
| | | |
| | | /** |
| | | * Instant 转 æ¶é´ |
| | | * |
| | | * @param instant Instant |
| | | * @return Instant |
| | | */ |
| | | public static LocalDateTime toDateTime(Instant instant) { |
| | | return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æ date |
| | | * |
| | | * @param dateTime LocalDateTime |
| | | * @return Date |
| | | */ |
| | | public static Date toDate(LocalDateTime dateTime) { |
| | | return Date.from(DateUtil.toInstant(dateTime)); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æ date |
| | | * |
| | | * @param localDate LocalDate |
| | | * @return Date |
| | | */ |
| | | public static Date toDate(final LocalDate localDate) { |
| | | return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| | | } |
| | | |
| | | /** |
| | | * Converts local date time to Calendar. |
| | | */ |
| | | public static Calendar toCalendar(final LocalDateTime localDateTime) { |
| | | return GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault())); |
| | | } |
| | | |
| | | /** |
| | | * localDateTime è½¬æ¢ææ¯«ç§æ° |
| | | * |
| | | * @param localDateTime LocalDateTime |
| | | * @return long |
| | | */ |
| | | public static long toMilliseconds(final LocalDateTime localDateTime) { |
| | | return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| | | } |
| | | |
| | | /** |
| | | * localDate è½¬æ¢ææ¯«ç§æ° |
| | | * |
| | | * @param localDate LocalDate |
| | | * @return long |
| | | */ |
| | | public static long toMilliseconds(LocalDate localDate) { |
| | | return toMilliseconds(localDate.atStartOfDay()); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æjava8 æ¶é´ |
| | | * |
| | | * @param calendar æ¥å |
| | | * @return LocalDateTime |
| | | */ |
| | | public static LocalDateTime fromCalendar(final Calendar calendar) { |
| | | TimeZone tz = calendar.getTimeZone(); |
| | | ZoneId zid = tz == null ? ZoneId.systemDefault() : tz.toZoneId(); |
| | | return LocalDateTime.ofInstant(calendar.toInstant(), zid); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æjava8 æ¶é´ |
| | | * |
| | | * @param instant Instant |
| | | * @return LocalDateTime |
| | | */ |
| | | public static LocalDateTime fromInstant(final Instant instant) { |
| | | return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æjava8 æ¶é´ |
| | | * |
| | | * @param date Date |
| | | * @return LocalDateTime |
| | | */ |
| | | public static LocalDateTime fromDate(final Date date) { |
| | | return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); |
| | | } |
| | | |
| | | /** |
| | | * è½¬æ¢æjava8 æ¶é´ |
| | | * |
| | | * @param milliseconds æ¯«ç§æ° |
| | | * @return LocalDateTime |
| | | */ |
| | | public static LocalDateTime fromMilliseconds(final long milliseconds) { |
| | | return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault()); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾2个æ¶é´å·®ï¼è·¨åº¦æ¯è¾å° |
| | | * |
| | | * @param startInclusive å¼å§æ¶é´ |
| | | * @param endExclusive ç»ææ¶é´ |
| | | * @return æ¶é´é´é |
| | | */ |
| | | public static Duration between(Temporal startInclusive, Temporal endExclusive) { |
| | | return Duration.between(startInclusive, endExclusive); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾2个æ¶é´å·®ï¼è·¨åº¦æ¯è¾å¤§ï¼å¹´ææ¥ä¸ºåä½ |
| | | * |
| | | * @param startDate å¼å§æ¶é´ |
| | | * @param endDate ç»ææ¶é´ |
| | | * @return æ¶é´é´é |
| | | */ |
| | | public static Period between(LocalDate startDate, LocalDate endDate) { |
| | | return Period.between(startDate, endDate); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾2个 æ¶é´å·® |
| | | * |
| | | * @param startDate å¼å§æ¶é´ |
| | | * @param endDate ç»ææ¶é´ |
| | | * @return æ¶é´é´é |
| | | */ |
| | | public static Duration between(Date startDate, Date endDate) { |
| | | return Duration.between(startDate.toInstant(), endDate.toInstant()); |
| | | } |
| | | |
| | | /** |
| | | * å°ç§æ°è½¬æ¢ä¸ºæ¥æ¶åç§ |
| | | * |
| | | * @param second ç§æ° |
| | | * @return æ¶é´ |
| | | */ |
| | | public static String secondToTime(Long second) { |
| | | // 夿æ¯å¦ä¸ºç©º |
| | | if (second == null || second == 0L) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | //转æ¢å¤©æ° |
| | | long days = second / 86400; |
| | | //å©ä½ç§æ° |
| | | second = second % 86400; |
| | | //转æ¢å°æ¶ |
| | | long hours = second / 3600; |
| | | //å©ä½ç§æ° |
| | | second = second % 3600; |
| | | //转æ¢åé |
| | | long minutes = second / 60; |
| | | //å©ä½ç§æ° |
| | | second = second % 60; |
| | | if (days > 0) { |
| | | return StringUtil.format("{}天{}å°æ¶{}å{}ç§", days, hours, minutes, second); |
| | | } else { |
| | | return StringUtil.format("{}å°æ¶{}å{}ç§", hours, minutes, second); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åä»å¤©çæ¥æ |
| | | * |
| | | * @return æ¶é´ |
| | | */ |
| | | public static String today() { |
| | | return format(new Date(), "yyyyMMdd"); |
| | | } |
| | | |
| | | /** |
| | | * è·åä»å¤©çæ¶é´ |
| | | * |
| | | * @return æ¶é´ |
| | | */ |
| | | public static String time() { |
| | | return format(new Date(), PATTERN_DATETIME_MINI); |
| | | } |
| | | |
| | | /** |
| | | * è·åä»å¤©çå°æ¶æ° |
| | | * |
| | | * @return æ¶é´ |
| | | */ |
| | | public static Integer hour() { |
| | | return NumberUtil.toInt(format(new Date(), "HH")); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.DigestUtils; |
| | | |
| | | import javax.crypto.Mac; |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | |
| | | /** |
| | | * å å¯ç¸å
³å·¥å
·ç±»ç´æ¥ä½¿ç¨Spring utilå°è£
ï¼åå°jarä¾èµ |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class DigestUtil extends org.springframework.util.DigestUtils { |
| | | private static final char[] HEX_CODE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| | | |
| | | /** |
| | | * Calculates the MD5 digest and returns the value as a 32 character hex string. |
| | | * |
| | | * @param data Data to digest |
| | | * @return MD5 digest as a hex string |
| | | */ |
| | | public static String md5Hex(final String data) { |
| | | return DigestUtils.md5DigestAsHex(data.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * Return a hexadecimal string representation of the MD5 digest of the given bytes. |
| | | * |
| | | * @param bytes the bytes to calculate the digest over |
| | | * @return a hexadecimal digest string |
| | | */ |
| | | public static String md5Hex(final byte[] bytes) { |
| | | return DigestUtils.md5DigestAsHex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha1Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha1Hex(String data) { |
| | | return DigestUtil.sha1Hex(data.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * sha1Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha1Hex(final byte[] bytes) { |
| | | return DigestUtil.digestHex("SHA-1", bytes); |
| | | } |
| | | |
| | | /** |
| | | * SHA224Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha224Hex(String data) { |
| | | return DigestUtil.sha224Hex(data.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * SHA224Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha224Hex(final byte[] bytes) { |
| | | return DigestUtil.digestHex("SHA-224", bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha256Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha256Hex(String data) { |
| | | return DigestUtil.sha256Hex(data.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * sha256Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha256Hex(final byte[] bytes) { |
| | | return DigestUtil.digestHex("SHA-256", bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha384Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha384Hex(String data) { |
| | | return DigestUtil.sha384Hex(data.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * sha384Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha384Hex(final byte[] bytes) { |
| | | return DigestUtil.digestHex("SHA-384", bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha512Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha512Hex(String data) { |
| | | return DigestUtil.sha512Hex(data.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * sha512Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha512Hex(final byte[] bytes) { |
| | | return DigestUtil.digestHex("SHA-512", bytes); |
| | | } |
| | | |
| | | /** |
| | | * digest Hex |
| | | * |
| | | * @param algorithm ç®æ³ |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String digestHex(String algorithm, byte[] bytes) { |
| | | try { |
| | | MessageDigest md = MessageDigest.getInstance(algorithm); |
| | | return encodeHex(md.digest(bytes)); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * hmacMd5 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacMd5Hex(String data, String key) { |
| | | return DigestUtil.hmacMd5Hex(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacMd5 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacMd5Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMacHex("HmacMD5", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha1 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha1Hex(String data, String key) { |
| | | return DigestUtil.hmacSha1Hex(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha1 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha1Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMacHex("HmacSHA1", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha224 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha224Hex(String data, String key) { |
| | | return DigestUtil.hmacSha224Hex(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha224 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha224Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMacHex("HmacSHA224", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha256 |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static byte[] hmacSha256(String data, String key) { |
| | | return DigestUtil.hmacSha256(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha256 |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a byte array |
| | | */ |
| | | public static byte[] hmacSha256(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMac("HmacSHA256", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha256 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha256Hex(String data, String key) { |
| | | return DigestUtil.hmacSha256Hex(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha256 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha256Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMacHex("HmacSHA256", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha384 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha384Hex(String data, String key) { |
| | | return DigestUtil.hmacSha384Hex(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha384 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha384Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMacHex("HmacSHA384", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha512 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha512Hex(String data, String key) { |
| | | return DigestUtil.hmacSha512Hex(data.getBytes(Charsets.UTF_8), key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha512 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha512Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.digestHMacHex("HmacSHA512", bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * digest HMac Hex |
| | | * |
| | | * @param algorithm ç®æ³ |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String digestHMacHex(String algorithm, final byte[] bytes, String key) { |
| | | SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm); |
| | | try { |
| | | Mac mac = Mac.getInstance(secretKey.getAlgorithm()); |
| | | mac.init(secretKey); |
| | | return DigestUtil.encodeHex(mac.doFinal(bytes)); |
| | | } catch (NoSuchAlgorithmException | InvalidKeyException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * digest HMac |
| | | * |
| | | * @param algorithm ç®æ³ |
| | | * @param bytes Data to digest |
| | | * @return digest as a byte array |
| | | */ |
| | | public static byte[] digestHMac(String algorithm, final byte[] bytes, String key) { |
| | | SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm); |
| | | try { |
| | | Mac mac = Mac.getInstance(secretKey.getAlgorithm()); |
| | | mac.init(secretKey); |
| | | return mac.doFinal(bytes); |
| | | } catch (NoSuchAlgorithmException | InvalidKeyException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * encode Hex |
| | | * |
| | | * @param bytes Data to Hex |
| | | * @return bytes as a hex string |
| | | */ |
| | | public static String encodeHex(byte[] bytes) { |
| | | StringBuilder r = new StringBuilder(bytes.length * 2); |
| | | for (byte b : bytes) { |
| | | r.append(HEX_CODE[(b >> 4) & 0xF]); |
| | | r.append(HEX_CODE[(b & 0xF)]); |
| | | } |
| | | return r.toString(); |
| | | } |
| | | |
| | | /** |
| | | * decode Hex |
| | | * |
| | | * @param hexStr Hex string |
| | | * @return decode hex to bytes |
| | | */ |
| | | public static byte[] decodeHex(final String hexStr) { |
| | | return DatatypeConverterUtil.parseHexBinary(hexStr); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾å符串ï¼é¿å
å符串å 为è¿é¿ï¼äº§çèæ¶ |
| | | * |
| | | * @param a String |
| | | * @param b String |
| | | * @return æ¯å¦ç¸å |
| | | */ |
| | | public static boolean slowEquals(@Nullable String a, @Nullable String b) { |
| | | if (a == null || b == null) { |
| | | return false; |
| | | } |
| | | return DigestUtil.slowEquals(a.getBytes(Charsets.UTF_8), b.getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾ byte æ°ç»ï¼é¿å
å符串å 为è¿é¿ï¼äº§çèæ¶ |
| | | * |
| | | * @param a byte array |
| | | * @param b byte array |
| | | * @return æ¯å¦ç¸å |
| | | */ |
| | | public static boolean slowEquals(@Nullable byte[] a, @Nullable byte[] b) { |
| | | if (a == null || b == null) { |
| | | return false; |
| | | } |
| | | if (a.length != b.length) { |
| | | return false; |
| | | } |
| | | int diff = a.length ^ b.length; |
| | | for (int i = 0; i < a.length; i++) { |
| | | diff |= a[i] ^ b[i]; |
| | | } |
| | | return diff == 0; |
| | | } |
| | | |
| | | /** |
| | | * èªå®ä¹å å¯ å°åç«¯ä¼ éçå¯ç 忬¡å å¯ |
| | | * |
| | | * @param data æ°æ® |
| | | * @return {String} |
| | | */ |
| | | public static String hex(String data) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | return sha1Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * ç¨æ·å¯ç å å¯è§å å
MD5åSHA1 |
| | | * |
| | | * @param data æ°æ® |
| | | * @return {String} |
| | | */ |
| | | public static String encrypt(String data) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | return sha1Hex(md5Hex(data)); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | |
| | | package com.vci.web.util; |
| | | |
| | | import cn.hutool.core.io.FastStringWriter; |
| | | |
| | | import java.io.PrintWriter; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.UndeclaredThrowableException; |
| | | |
| | | /** |
| | | * å¼å¸¸å¤çå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class Exceptions { |
| | | |
| | | /** |
| | | * å°CheckedException转æ¢ä¸ºUncheckedException. |
| | | * |
| | | * @param e Throwable |
| | | * @return {RuntimeException} |
| | | */ |
| | | public static RuntimeException unchecked(Throwable e) { |
| | | if (e instanceof Error) { |
| | | throw (Error) e; |
| | | } else if (e instanceof IllegalAccessException || |
| | | e instanceof IllegalArgumentException || |
| | | e instanceof NoSuchMethodException) { |
| | | return new IllegalArgumentException(e); |
| | | } else if (e instanceof InvocationTargetException) { |
| | | return new RuntimeException(((InvocationTargetException) e).getTargetException()); |
| | | } else if (e instanceof RuntimeException) { |
| | | return (RuntimeException) e; |
| | | } else if (e instanceof InterruptedException) { |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | return Exceptions.runtime(e); |
| | | } |
| | | |
| | | /** |
| | | * ä¸éç¨ RuntimeException å
è£
ï¼ç´æ¥æåºï¼ä½¿å¼å¸¸æ´å ç²¾å |
| | | * |
| | | * @param throwable Throwable |
| | | * @param <T> æ³åæ è®° |
| | | * @return Throwable |
| | | * @throws T æ³å |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | private static <T extends Throwable> T runtime(Throwable throwable) throws T { |
| | | throw (T) throwable; |
| | | } |
| | | |
| | | /** |
| | | * 代çå¼å¸¸è§£å
|
| | | * |
| | | * @param wrapped å
è£
è¿å¾å¼å¸¸ |
| | | * @return è§£å
åçå¼å¸¸ |
| | | */ |
| | | public static Throwable unwrap(Throwable wrapped) { |
| | | Throwable unwrapped = wrapped; |
| | | while (true) { |
| | | if (unwrapped instanceof InvocationTargetException) { |
| | | unwrapped = ((InvocationTargetException) unwrapped).getTargetException(); |
| | | } else if (unwrapped instanceof UndeclaredThrowableException) { |
| | | unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable(); |
| | | } else { |
| | | return unwrapped; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°ErrorStack转å为String. |
| | | * |
| | | * @param ex Throwable |
| | | * @return {String} |
| | | */ |
| | | public static String getStackTraceAsString(Throwable ex) { |
| | | FastStringWriter stringWriter = new FastStringWriter(); |
| | | ex.printStackTrace(new PrintWriter(stringWriter)); |
| | | return stringWriter.toString(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | |
| | | package com.vci.web.util; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.util.FileSystemUtils; |
| | | import org.springframework.util.PatternMatchUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | import java.nio.file.Files; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * æä»¶å·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class FileUtil extends org.springframework.util.FileCopyUtils { |
| | | |
| | | /** |
| | | * é»è®¤ä¸ºtrue |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public static class TrueFilter implements FileFilter, Serializable { |
| | | private static final long serialVersionUID = -6420452043795072619L; |
| | | |
| | | public final static TrueFilter TRUE = new TrueFilter(); |
| | | |
| | | @Override |
| | | public boolean accept(File pathname) { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param path è·¯å¾ |
| | | * @return æä»¶éå |
| | | */ |
| | | public static List<File> list(String path) { |
| | | File file = new File(path); |
| | | return list(file, TrueFilter.TRUE); |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param path è·¯å¾ |
| | | * @param fileNamePattern æä»¶å * å· |
| | | * @return æä»¶éå |
| | | */ |
| | | public static List<File> list(String path, final String fileNamePattern) { |
| | | File file = new File(path); |
| | | return list(file, pathname -> { |
| | | String fileName = pathname.getName(); |
| | | return PatternMatchUtils.simpleMatch(fileNamePattern, fileName); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param path è·¯å¾ |
| | | * @param filter æä»¶è¿æ»¤ |
| | | * @return æä»¶éå |
| | | */ |
| | | public static List<File> list(String path, FileFilter filter) { |
| | | File file = new File(path); |
| | | return list(file, filter); |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param file æä»¶ |
| | | * @return æä»¶éå |
| | | */ |
| | | public static List<File> list(File file) { |
| | | List<File> fileList = new ArrayList<>(); |
| | | return list(file, fileList, TrueFilter.TRUE); |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param file æä»¶ |
| | | * @param fileNamePattern Spring AntPathMatcher è§å |
| | | * @return æä»¶éå |
| | | */ |
| | | public static List<File> list(File file, final String fileNamePattern) { |
| | | List<File> fileList = new ArrayList<>(); |
| | | return list(file, fileList, pathname -> { |
| | | String fileName = pathname.getName(); |
| | | return PatternMatchUtils.simpleMatch(fileNamePattern, fileName); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param file æä»¶ |
| | | * @param filter æä»¶è¿æ»¤ |
| | | * @return æä»¶éå |
| | | */ |
| | | public static List<File> list(File file, FileFilter filter) { |
| | | List<File> fileList = new ArrayList<>(); |
| | | return list(file, fileList, filter); |
| | | } |
| | | |
| | | /** |
| | | * æ«æç®å½ä¸çæä»¶ |
| | | * |
| | | * @param file æä»¶ |
| | | * @param filter æä»¶è¿æ»¤ |
| | | * @return æä»¶éå |
| | | */ |
| | | private static List<File> list(File file, List<File> fileList, FileFilter filter) { |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | if (files != null) { |
| | | for (File f : files) { |
| | | list(f, fileList, filter); |
| | | } |
| | | } |
| | | } else { |
| | | // è¿æ»¤æä»¶ |
| | | boolean accept = filter.accept(file); |
| | | if (file.exists() && accept) { |
| | | fileList.add(file); |
| | | } |
| | | } |
| | | return fileList; |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶åç¼å |
| | | * @param fullName æä»¶å
¨å |
| | | * @return {String} |
| | | */ |
| | | public static String getFileExtension(String fullName) { |
| | | if (StringUtil.isBlank(fullName)) return StringPool.EMPTY; |
| | | String fileName = new File(fullName).getName(); |
| | | int dotIndex = fileName.lastIndexOf(CharPool.DOT); |
| | | return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1); |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶åï¼å»é¤åç¼å |
| | | * @param fullName æä»¶å
¨å |
| | | * @return {String} |
| | | */ |
| | | public static String getNameWithoutExtension(String fullName) { |
| | | if (StringUtil.isBlank(fullName)) return StringPool.EMPTY; |
| | | String fileName = new File(fullName).getName(); |
| | | int dotIndex = fileName.lastIndexOf(CharPool.DOT); |
| | | return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex); |
| | | } |
| | | |
| | | /** |
| | | * Returns the path to the system temporary directory. |
| | | * |
| | | * @return the path to the system temporary directory. |
| | | */ |
| | | public static String getTempDirPath() { |
| | | return System.getProperty("java.io.tmpdir"); |
| | | } |
| | | |
| | | /** |
| | | * Returns a {@link File} representing the system temporary directory. |
| | | * |
| | | * @return the system temporary directory. |
| | | */ |
| | | public static File getTempDir() { |
| | | return new File(getTempDirPath()); |
| | | } |
| | | |
| | | /** |
| | | * Reads the contents of a file into a String. |
| | | * The file is always closed. |
| | | * |
| | | * @param file the file to read, must not be {@code null} |
| | | * @return the file contents, never {@code null} |
| | | */ |
| | | public static String readToString(final File file) { |
| | | return readToString(file, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * Reads the contents of a file into a String. |
| | | * The file is always closed. |
| | | * |
| | | * @param file the file to read, must not be {@code null} |
| | | * @param encoding the encoding to use, {@code null} means platform default |
| | | * @return the file contents, never {@code null} |
| | | */ |
| | | public static String readToString(final File file, final Charset encoding) { |
| | | try (InputStream in = Files.newInputStream(file.toPath())) { |
| | | return IoUtil.readToString(in, encoding); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Reads the contents of a file into a String. |
| | | * The file is always closed. |
| | | * |
| | | * @param file the file to read, must not be {@code null} |
| | | * @return the file contents, never {@code null} |
| | | */ |
| | | public static byte[] readToByteArray(final File file) { |
| | | try (InputStream in = Files.newInputStream(file.toPath())) { |
| | | return IoUtil.readToByteArray(in); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Writes a String to a file creating the file if it does not exist. |
| | | * |
| | | * @param file the file to write |
| | | * @param data the content to write to the file |
| | | */ |
| | | public static void writeToFile(final File file, final String data) { |
| | | writeToFile(file, data, Charsets.UTF_8, false); |
| | | } |
| | | |
| | | /** |
| | | * Writes a String to a file creating the file if it does not exist. |
| | | * |
| | | * @param file the file to write |
| | | * @param data the content to write to the file |
| | | * @param append if {@code true}, then the String will be added to the |
| | | * end of the file rather than overwriting |
| | | */ |
| | | public static void writeToFile(final File file, final String data, final boolean append){ |
| | | writeToFile(file, data, Charsets.UTF_8, append); |
| | | } |
| | | |
| | | /** |
| | | * Writes a String to a file creating the file if it does not exist. |
| | | * |
| | | * @param file the file to write |
| | | * @param data the content to write to the file |
| | | * @param encoding the encoding to use, {@code null} means platform default |
| | | */ |
| | | public static void writeToFile(final File file, final String data, final Charset encoding) { |
| | | writeToFile(file, data, encoding, false); |
| | | } |
| | | |
| | | /** |
| | | * Writes a String to a file creating the file if it does not exist. |
| | | * |
| | | * @param file the file to write |
| | | * @param data the content to write to the file |
| | | * @param encoding the encoding to use, {@code null} means platform default |
| | | * @param append if {@code true}, then the String will be added to the |
| | | * end of the file rather than overwriting |
| | | */ |
| | | public static void writeToFile(final File file, final String data, final Charset encoding, final boolean append) { |
| | | try (OutputStream out = new FileOutputStream(file, append)) { |
| | | IoUtil.write(data, out, encoding); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æfile |
| | | * @param multipartFile MultipartFile |
| | | * @param file File |
| | | */ |
| | | public static void toFile(MultipartFile multipartFile, final File file) { |
| | | try { |
| | | FileUtil.toFile(multipartFile.getInputStream(), file); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æfile |
| | | * @param in InputStream |
| | | * @param file File |
| | | */ |
| | | public static void toFile(InputStream in, final File file) { |
| | | try (OutputStream out = new FileOutputStream(file)) { |
| | | FileUtil.copy(in, out); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Moves a file. |
| | | * <p> |
| | | * When the destination file is on another file system, do a "copy and delete". |
| | | * |
| | | * @param srcFile the file to be moved |
| | | * @param destFile the destination file |
| | | * @throws NullPointerException if source or destination is {@code null} |
| | | * @throws IOException if source or destination is invalid |
| | | * @throws IOException if an IO error occurs moving the file |
| | | */ |
| | | public static void moveFile(final File srcFile, final File destFile) throws IOException { |
| | | Assert.notNull(srcFile, "Source must not be null"); |
| | | Assert.notNull(destFile, "Destination must not be null"); |
| | | if (!srcFile.exists()) { |
| | | throw new FileNotFoundException("Source '" + srcFile + "' does not exist"); |
| | | } |
| | | if (srcFile.isDirectory()) { |
| | | throw new IOException("Source '" + srcFile + "' is a directory"); |
| | | } |
| | | if (destFile.exists()) { |
| | | throw new IOException("Destination '" + destFile + "' already exists"); |
| | | } |
| | | if (destFile.isDirectory()) { |
| | | throw new IOException("Destination '" + destFile + "' is a directory"); |
| | | } |
| | | final boolean rename = srcFile.renameTo(destFile); |
| | | if (!rename) { |
| | | FileUtil.copy(srcFile, destFile); |
| | | if (!srcFile.delete()) { |
| | | FileUtil.deleteQuietly(destFile); |
| | | throw new IOException("Failed to delete original file '" + srcFile + "' after copy to '" + destFile + "'"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. |
| | | * <p> |
| | | * The difference between File.delete() and this method are: |
| | | * <ul> |
| | | * <li>A directory to be deleted does not have to be empty.</li> |
| | | * <li>No exceptions are thrown when a file or directory cannot be deleted.</li> |
| | | * </ul> |
| | | * |
| | | * @param file file or directory to delete, can be {@code null} |
| | | * @return {@code true} if the file or directory was deleted, otherwise |
| | | * {@code false} |
| | | */ |
| | | public static boolean deleteQuietly(@Nullable final File file) { |
| | | if (file == null) { |
| | | return false; |
| | | } |
| | | try { |
| | | if (file.isDirectory()) { |
| | | FileSystemUtils.deleteRecursively(file); |
| | | } |
| | | } catch (final Exception ignored) { |
| | | } |
| | | |
| | | try { |
| | | return file.delete(); |
| | | } catch (final Exception ignored) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.aspose.words.ConvertUtil; |
| | | import com.fasterxml.jackson.core.JsonParser; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.sun.jndi.toolkit.url.UrlUtil; |
| | | import com.vci.web.util.jackson.JsonUtil; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.core.MethodParameter; |
| | | import org.springframework.core.annotation.AnnotatedElementUtils; |
| | | import org.springframework.core.convert.TypeDescriptor; |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.PatternMatchUtils; |
| | | import org.springframework.web.method.HandlerMethod; |
| | | |
| | | import java.io.Closeable; |
| | | import java.io.File; |
| | | import java.io.InputStream; |
| | | import java.lang.annotation.Annotation; |
| | | import java.lang.reflect.AnnotatedElement; |
| | | import java.lang.reflect.Constructor; |
| | | import java.lang.reflect.Method; |
| | | import java.nio.charset.Charset; |
| | | import java.text.DecimalFormat; |
| | | import java.time.Duration; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.Temporal; |
| | | import java.time.temporal.TemporalAccessor; |
| | | import java.util.*; |
| | | import java.util.function.Supplier; |
| | | |
| | | /** |
| | | * å·¥å
·å
éåï¼å·¥å
·ç±»å¿«æ·æ¹å¼ |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class Func { |
| | | |
| | | /** |
| | | * æè¨ï¼å¿
é¡»ä¸è½ä¸º null |
| | | * <blockquote><pre> |
| | | * public Foo(Bar bar) { |
| | | * this.bar = $.requireNotNull(bar); |
| | | * } |
| | | * </pre></blockquote> |
| | | * |
| | | * @param obj the object reference to check for nullity |
| | | * @param <T> the type of the reference |
| | | * @return {@code obj} if not {@code null} |
| | | * @throws NullPointerException if {@code obj} is {@code null} |
| | | */ |
| | | public static <T> T requireNotNull(T obj) { |
| | | return Objects.requireNonNull(obj); |
| | | } |
| | | |
| | | /** |
| | | * æè¨ï¼å¿
é¡»ä¸è½ä¸º null |
| | | * <blockquote><pre> |
| | | * public Foo(Bar bar, Baz baz) { |
| | | * this.bar = $.requireNotNull(bar, "bar must not be null"); |
| | | * this.baz = $.requireNotNull(baz, "baz must not be null"); |
| | | * } |
| | | * </pre></blockquote> |
| | | * |
| | | * @param obj the object reference to check for nullity |
| | | * @param message detail message to be used in the event that a {@code |
| | | * NullPointerException} is thrown |
| | | * @param <T> the type of the reference |
| | | * @return {@code obj} if not {@code null} |
| | | * @throws NullPointerException if {@code obj} is {@code null} |
| | | */ |
| | | public static <T> T requireNotNull(T obj, String message) { |
| | | return Objects.requireNonNull(obj, message); |
| | | } |
| | | |
| | | /** |
| | | * æè¨ï¼å¿
é¡»ä¸è½ä¸º null |
| | | * <blockquote><pre> |
| | | * public Foo(Bar bar, Baz baz) { |
| | | * this.bar = $.requireNotNull(bar, () -> "bar must not be null"); |
| | | * } |
| | | * </pre></blockquote> |
| | | * |
| | | * @param obj the object reference to check for nullity |
| | | * @param messageSupplier supplier of the detail message to be |
| | | * used in the event that a {@code NullPointerException} is thrown |
| | | * @param <T> the type of the reference |
| | | * @return {@code obj} if not {@code null} |
| | | * @throws NullPointerException if {@code obj} is {@code null} |
| | | */ |
| | | public static <T> T requireNotNull(T obj, Supplier<String> messageSupplier) { |
| | | return Objects.requireNonNull(obj, messageSupplier); |
| | | } |
| | | |
| | | /** |
| | | * å¤æå¯¹è±¡æ¯å¦ä¸ºnull |
| | | * <p> |
| | | * This method exists to be used as a |
| | | * {@link java.util.function.Predicate}, {@code filter($::isNull)} |
| | | * </p> |
| | | * |
| | | * @param obj a reference to be checked against {@code null} |
| | | * @return {@code true} if the provided reference is {@code null} otherwise |
| | | * {@code false} |
| | | * @see java.util.function.Predicate |
| | | */ |
| | | public static boolean isNull(@Nullable Object obj) { |
| | | return Objects.isNull(obj); |
| | | } |
| | | |
| | | /** |
| | | * å¤æå¯¹è±¡æ¯å¦ not null |
| | | * <p> |
| | | * This method exists to be used as a |
| | | * {@link java.util.function.Predicate}, {@code filter($::notNull)} |
| | | * </p> |
| | | * |
| | | * @param obj a reference to be checked against {@code null} |
| | | * @return {@code true} if the provided reference is non-{@code null} |
| | | * otherwise {@code false} |
| | | * @see java.util.function.Predicate |
| | | */ |
| | | public static boolean notNull(@Nullable Object obj) { |
| | | return Objects.nonNull(obj); |
| | | } |
| | | |
| | | /** |
| | | * é¦åæ¯åå°å |
| | | * |
| | | * @param str å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String firstCharToLower(String str) { |
| | | return StringUtil.firstCharToLower(str); |
| | | } |
| | | |
| | | /** |
| | | * é¦åæ¯å大å |
| | | * |
| | | * @param str å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String firstCharToUpper(String str) { |
| | | return StringUtil.firstCharToUpper(str); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦ä¸ºç©ºå符串 |
| | | * <pre class="code"> |
| | | * $.isBlank(null) = true |
| | | * $.isBlank("") = true |
| | | * $.isBlank(" ") = true |
| | | * $.isBlank("12345") = false |
| | | * $.isBlank(" 12345 ") = false |
| | | * </pre> |
| | | * |
| | | * @param cs the {@code CharSequence} to check (may be {@code null}) |
| | | * @return {@code true} if the {@code CharSequence} is not {@code null}, |
| | | * its length is greater than 0, and it does not contain whitespace only |
| | | * @see Character#isWhitespace |
| | | */ |
| | | public static boolean isBlank(@Nullable final CharSequence cs) { |
| | | return StringUtil.isBlank(cs); |
| | | } |
| | | |
| | | /** |
| | | * 夿ä¸ä¸ºç©ºå符串 |
| | | * <pre> |
| | | * $.isNotBlank(null) = false |
| | | * $.isNotBlank("") = false |
| | | * $.isNotBlank(" ") = false |
| | | * $.isNotBlank("bob") = true |
| | | * $.isNotBlank(" bob ") = true |
| | | * </pre> |
| | | * |
| | | * @param cs the CharSequence to check, may be null |
| | | * @return {@code true} if the CharSequence is |
| | | * not empty and not null and not whitespace |
| | | * @see Character#isWhitespace |
| | | */ |
| | | public static boolean isNotBlank(@Nullable final CharSequence cs) { |
| | | return StringUtil.isNotBlank(cs); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦æä»»æä¸ä¸ª 空å符串 |
| | | * |
| | | * @param css CharSequence |
| | | * @return boolean |
| | | */ |
| | | public static boolean isAnyBlank(final CharSequence... css) { |
| | | return StringUtil.isAnyBlank(css); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦å
¨ä¸ºé空å符串 |
| | | * |
| | | * @param css CharSequence |
| | | * @return boolean |
| | | */ |
| | | public static boolean isNoneBlank(final CharSequence... css) { |
| | | return StringUtil.isNoneBlank(css); |
| | | } |
| | | |
| | | /** |
| | | * å¤æå¯¹è±¡æ¯æ°ç» |
| | | * |
| | | * @param obj the object to check |
| | | * @return æ¯å¦æ°ç» |
| | | */ |
| | | public static boolean isArray(@Nullable Object obj) { |
| | | return ObjectUtil.isArray(obj); |
| | | } |
| | | |
| | | /** |
| | | * å¤æç©ºå¯¹è±¡ objectãmapãlistãsetãåç¬¦ä¸²ãæ°ç» |
| | | * |
| | | * @param obj the object to check |
| | | * @return æ°ç»æ¯å¦ä¸ºç©º |
| | | */ |
| | | public static boolean isEmpty(@Nullable Object obj) { |
| | | return ObjectUtil.isEmpty(obj); |
| | | } |
| | | |
| | | /** |
| | | * 对象ä¸ä¸ºç©º objectãmapãlistãsetãåç¬¦ä¸²ãæ°ç» |
| | | * |
| | | * @param obj the object to check |
| | | * @return æ¯å¦ä¸ä¸ºç©º |
| | | */ |
| | | public static boolean isNotEmpty(@Nullable Object obj) { |
| | | return !ObjectUtil.isEmpty(obj); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ°ç»ä¸ºç©º |
| | | * |
| | | * @param array the array to check |
| | | * @return æ°ç»æ¯å¦ä¸ºç©º |
| | | */ |
| | | public static boolean isEmpty(@Nullable Object[] array) { |
| | | return ObjectUtil.isEmpty(array); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ°ç»ä¸ä¸ºç©º |
| | | * |
| | | * @param array æ°ç» |
| | | * @return æ°ç»æ¯å¦ä¸ä¸ºç©º |
| | | */ |
| | | public static boolean isNotEmpty(@Nullable Object[] array) { |
| | | return ObjectUtil.isNotEmpty(array); |
| | | } |
| | | |
| | | /** |
| | | * 对象ç»ä¸æ¯å¦åå¨ Empty Object |
| | | * |
| | | * @param os å¯¹è±¡ç» |
| | | * @return boolean |
| | | */ |
| | | public static boolean hasEmpty(Object... os) { |
| | | for (Object o : os) { |
| | | if (isEmpty(o)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 对象ç»ä¸æ¯å¦å
¨é¨ä¸º Empty Object |
| | | * |
| | | * @param os å¯¹è±¡ç» |
| | | * @return boolean |
| | | */ |
| | | public static boolean isAllEmpty(Object... os) { |
| | | for (Object o : os) { |
| | | if (isNotEmpty(o)) { |
| | | return false; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * å°å符串ä¸ç¹å®æ¨¡å¼çåç¬¦è½¬æ¢æmapä¸å¯¹åºçå¼ |
| | | * <p> |
| | | * use: format("my name is ${name}, and i like ${like}!", {"name":"L.cm", "like": "Java"}) |
| | | * |
| | | * @param message éè¦è½¬æ¢çå符串 |
| | | * @param params è½¬æ¢æéçé®å¼å¯¹éå |
| | | * @return 转æ¢åçå符串 |
| | | */ |
| | | public static String format(@Nullable String message, @Nullable Map<String, ?> params) { |
| | | return StringUtil.format(message, params); |
| | | } |
| | | |
| | | /** |
| | | * å log æ ¼å¼ç format è§å |
| | | * <p> |
| | | * use: format("my name is {}, and i like {}!", "L.cm", "Java") |
| | | * |
| | | * @param message éè¦è½¬æ¢çå符串 |
| | | * @param arguments éè¦æ¿æ¢çåé |
| | | * @return 转æ¢åçå符串 |
| | | */ |
| | | public static String format(@Nullable String message, @Nullable Object... arguments) { |
| | | return StringUtil.format(message, arguments); |
| | | } |
| | | |
| | | /** |
| | | * æ ¼å¼åæ§è¡æ¶é´ï¼åä½ä¸º ms å sï¼ä¿çä¸ä½å°æ° |
| | | * |
| | | * @param nanos çº³ç§ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String format(long nanos) { |
| | | return StringUtil.format(nanos); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾ä¸¤ä¸ªå¯¹è±¡æ¯å¦ç¸çã<br> |
| | | * ç¸åçæ¡ä»¶æä¸¤ä¸ªï¼æ»¡è¶³å
¶ä¸å³å¯ï¼<br> |
| | | * |
| | | * @param obj1 对象1 |
| | | * @param obj2 对象2 |
| | | * @return æ¯å¦ç¸ç |
| | | */ |
| | | public static boolean equals(Object obj1, Object obj2) { |
| | | return Objects.equals(obj1, obj2); |
| | | } |
| | | |
| | | /** |
| | | * å®å
¨ç equals |
| | | * |
| | | * @param o1 first Object to compare |
| | | * @param o2 second Object to compare |
| | | * @return whether the given objects are equal |
| | | * @see Object#equals(Object) |
| | | * @see java.util.Arrays#equals |
| | | */ |
| | | public static boolean equalsSafe(@Nullable Object o1, @Nullable Object o2) { |
| | | return ObjectUtil.nullSafeEquals(o1, o2); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ°ç»ä¸æ¯å¦å
å«å
ç´ |
| | | * |
| | | * @param array the Array to check |
| | | * @param element the element to look for |
| | | * @param <T> The generic tag |
| | | * @return {@code true} if found, {@code false} else |
| | | */ |
| | | public static <T> boolean contains(@Nullable T[] array, final T element) { |
| | | return CollectionUtil.contains(array, element); |
| | | } |
| | | |
| | | /** |
| | | * 夿è¿ä»£å¨ä¸æ¯å¦å
å«å
ç´ |
| | | * |
| | | * @param iterator the Iterator to check |
| | | * @param element the element to look for |
| | | * @return {@code true} if found, {@code false} otherwise |
| | | */ |
| | | public static boolean contains(@Nullable Iterator<?> iterator, Object element) { |
| | | return CollectionUtil.contains(iterator, element); |
| | | } |
| | | |
| | | /** |
| | | * 夿æä¸¾æ¯å¦å
å«è¯¥å
ç´ |
| | | * |
| | | * @param enumeration the Enumeration to check |
| | | * @param element the element to look for |
| | | * @return {@code true} if found, {@code false} otherwise |
| | | */ |
| | | public static boolean contains(@Nullable Enumeration<?> enumeration, Object element) { |
| | | return CollectionUtil.contains(enumeration, element); |
| | | } |
| | | |
| | | /** |
| | | * ä¸å¯å Set |
| | | * |
| | | * @param es 对象 |
| | | * @param <E> æ³å |
| | | * @return éå |
| | | */ |
| | | @SafeVarargs |
| | | public static <E> Set<E> ofImmutableSet(E... es) { |
| | | return CollectionUtil.ofImmutableSet(es); |
| | | } |
| | | |
| | | /** |
| | | * ä¸å¯å List |
| | | * |
| | | * @param es 对象 |
| | | * @param <E> æ³å |
| | | * @return éå |
| | | */ |
| | | @SafeVarargs |
| | | public static <E> List<E> ofImmutableList(E... es) { |
| | | return CollectionUtil.ofImmutableList(es); |
| | | } |
| | | |
| | | /** |
| | | * 强转string,并廿å¤ä½ç©ºæ ¼ |
| | | * |
| | | * @param str å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String toStr(Object str) { |
| | | return toStr(str, ""); |
| | | } |
| | | |
| | | /** |
| | | * 强转string,并廿å¤ä½ç©ºæ ¼ |
| | | * |
| | | * @param str å符串 |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return {String} |
| | | */ |
| | | public static String toStr(Object str, String defaultValue) { |
| | | if (null == str || str.equals(StringPool.NULL)) { |
| | | return defaultValue; |
| | | } |
| | | return String.valueOf(str); |
| | | } |
| | | |
| | | /** |
| | | * 强转string(å
å«ç©ºå符串),并廿å¤ä½ç©ºæ ¼ |
| | | * |
| | | * @param str å符串 |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return {String} |
| | | */ |
| | | public static String toStrWithEmpty(Object str, String defaultValue) { |
| | | if (null == str || str.equals(StringPool.NULL) || str.equals(StringPool.EMPTY)) { |
| | | return defaultValue; |
| | | } |
| | | return String.valueOf(str); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 夿ä¸ä¸ªå符串æ¯å¦æ¯æ°å |
| | | * |
| | | * @param cs the CharSequence to check, may be null |
| | | * @return {boolean} |
| | | */ |
| | | public static boolean isNumeric(final CharSequence cs) { |
| | | return StringUtil.isNumeric(cs); |
| | | } |
| | | |
| | | /** |
| | | * å符串转 intï¼ä¸ºç©ºåè¿å0 |
| | | * |
| | | * <pre> |
| | | * $.toInt(null) = 0 |
| | | * $.toInt("") = 0 |
| | | * $.toInt("1") = 1 |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @return the int represented by the string, or <code>zero</code> if |
| | | * conversion fails |
| | | */ |
| | | public static int toInt(final Object str) { |
| | | return NumberUtil.toInt(String.valueOf(str)); |
| | | } |
| | | |
| | | /** |
| | | * å符串转 intï¼ä¸ºç©ºåè¿åé»è®¤å¼ |
| | | * |
| | | * <pre> |
| | | * $.toInt(null, 1) = 1 |
| | | * $.toInt("", 1) = 1 |
| | | * $.toInt("1", 0) = 1 |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static int toInt(@Nullable final Object str, final int defaultValue) { |
| | | return NumberUtil.toInt(String.valueOf(str), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * å符串转 longï¼ä¸ºç©ºåè¿å0 |
| | | * |
| | | * <pre> |
| | | * $.toLong(null) = 0L |
| | | * $.toLong("") = 0L |
| | | * $.toLong("1") = 1L |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @return the long represented by the string, or <code>0</code> if |
| | | * conversion fails |
| | | */ |
| | | public static long toLong(final Object str) { |
| | | return NumberUtil.toLong(String.valueOf(str)); |
| | | } |
| | | |
| | | /** |
| | | * å符串转 longï¼ä¸ºç©ºåè¿åé»è®¤å¼ |
| | | * |
| | | * <pre> |
| | | * $.toLong(null, 1L) = 1L |
| | | * $.toLong("", 1L) = 1L |
| | | * $.toLong("1", 0L) = 1L |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the long represented by the string, or the default if conversion fails |
| | | */ |
| | | public static long toLong(@Nullable final Object str, final long defaultValue) { |
| | | return NumberUtil.toLong(String.valueOf(str), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>Double</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * $.toDouble(null, 1) = 1.0 |
| | | * $.toDouble("", 1) = 1.0 |
| | | * $.toDouble("1", 0) = 1.0 |
| | | * </pre> |
| | | * |
| | | * @param value the string to convert, may be null |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static Double toDouble(Object value) { |
| | | return toDouble(String.valueOf(value), -1.00); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>Double</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * $.toDouble(null, 1) = 1.0 |
| | | * $.toDouble("", 1) = 1.0 |
| | | * $.toDouble("1", 0) = 1.0 |
| | | * </pre> |
| | | * |
| | | * @param value the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static Double toDouble(Object value, Double defaultValue) { |
| | | return NumberUtil.toDouble(String.valueOf(value), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>Float</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * $.toFloat(null, 1) = 1.00f |
| | | * $.toFloat("", 1) = 1.00f |
| | | * $.toFloat("1", 0) = 1.00f |
| | | * </pre> |
| | | * |
| | | * @param value the string to convert, may be null |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static Float toFloat(Object value) { |
| | | return toFloat(String.valueOf(value), -1.0f); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>Float</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * $.toFloat(null, 1) = 1.00f |
| | | * $.toFloat("", 1) = 1.00f |
| | | * $.toFloat("1", 0) = 1.00f |
| | | * </pre> |
| | | * |
| | | * @param value the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static Float toFloat(Object value, Float defaultValue) { |
| | | return NumberUtil.toFloat(String.valueOf(value), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>Boolean</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * $.toBoolean("true", true) = true |
| | | * $.toBoolean("false") = false |
| | | * $.toBoolean("", false) = false |
| | | * </pre> |
| | | * |
| | | * @param value the string to convert, may be null |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static Boolean toBoolean(Object value) { |
| | | return toBoolean(value, null); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>Boolean</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * $.toBoolean("true", true) = true |
| | | * $.toBoolean("false") = false |
| | | * $.toBoolean("", false) = false |
| | | * </pre> |
| | | * |
| | | * @param value the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static Boolean toBoolean(Object value, Boolean defaultValue) { |
| | | if (value != null) { |
| | | String val = String.valueOf(value); |
| | | val = val.toLowerCase().trim(); |
| | | return Boolean.parseBoolean(val); |
| | | } |
| | | return defaultValue; |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºIntegeræ°ç»<br> |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer[] toIntArray(String str) { |
| | | return toIntArray(",", str); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºIntegeræ°ç»<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer[] toIntArray(String split, String str) { |
| | | if (StringUtil.isEmpty(str)) { |
| | | return new Integer[]{}; |
| | | } |
| | | String[] arr = str.split(split); |
| | | final Integer[] ints = new Integer[arr.length]; |
| | | for (int i = 0; i < arr.length; i++) { |
| | | final Integer v = toInt(arr[i], 0); |
| | | ints[i] = v; |
| | | } |
| | | return ints; |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºIntegeréå<br> |
| | | * |
| | | * @param str ç»æè¢«è½¬æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static List<Integer> toIntList(String str) { |
| | | return Arrays.asList(toIntArray(str)); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºIntegeréå<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static List<Integer> toIntList(String split, String str) { |
| | | return Arrays.asList(toIntArray(split, str)); |
| | | } |
| | | |
| | | /** |
| | | * è·å第ä¸ä½Integeræ°å¼ |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer firstInt(String str) { |
| | | return firstInt(",", str); |
| | | } |
| | | |
| | | /** |
| | | * è·å第ä¸ä½Integeræ°å¼ |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer firstInt(String split, String str) { |
| | | List<Integer> ints = toIntList(split, str); |
| | | if (isEmpty(ints)) { |
| | | return null; |
| | | } else { |
| | | return ints.get(0); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºLongæ°ç»<br> |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long[] toLongArray(String str) { |
| | | return toLongArray(",", str); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºLongæ°ç»<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long[] toLongArray(String split, String str) { |
| | | if (StringUtil.isEmpty(str)) { |
| | | return new Long[]{}; |
| | | } |
| | | String[] arr = str.split(split); |
| | | final Long[] longs = new Long[arr.length]; |
| | | for (int i = 0; i < arr.length; i++) { |
| | | final Long v = toLong(arr[i], 0); |
| | | longs[i] = v; |
| | | } |
| | | return longs; |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºLongéå<br> |
| | | * |
| | | * @param str ç»æè¢«è½¬æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static List<Long> toLongList(String str) { |
| | | return Arrays.asList(toLongArray(str)); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºLongéå<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static List<Long> toLongList(String split, String str) { |
| | | return Arrays.asList(toLongArray(split, str)); |
| | | } |
| | | |
| | | /** |
| | | * è·å第ä¸ä½Longæ°å¼ |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long firstLong(String str) { |
| | | return firstLong(",", str); |
| | | } |
| | | |
| | | /** |
| | | * è·å第ä¸ä½Longæ°å¼ |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long firstLong(String split, String str) { |
| | | List<Long> longs = toLongList(split, str); |
| | | if (isEmpty(longs)) { |
| | | return null; |
| | | } else { |
| | | return longs.get(0); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºStringæ°ç»<br> |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String[] toStrArray(String str) { |
| | | return toStrArray(",", str); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºStringæ°ç»<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String[] toStrArray(String split, String str) { |
| | | if (isBlank(str)) { |
| | | return new String[]{}; |
| | | } |
| | | return str.split(split); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºStringéå<br> |
| | | * |
| | | * @param str ç»æè¢«è½¬æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static List<String> toStrList(String str) { |
| | | return Arrays.asList(toStrArray(str)); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºStringéå<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static List<String> toStrList(String split, String str) { |
| | | return Arrays.asList(toStrArray(split, str)); |
| | | } |
| | | |
| | | /** |
| | | * è·å第ä¸ä½Stringæ°å¼ |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String firstStr(String str) { |
| | | return firstStr(",", str); |
| | | } |
| | | |
| | | /** |
| | | * è·å第ä¸ä½Stringæ°å¼ |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String firstStr(String split, String str) { |
| | | List<String> strs = toStrList(split, str); |
| | | if (isEmpty(strs)) { |
| | | return null; |
| | | } else { |
| | | return strs.get(0); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å° long 转çå符串 为 62 è¿å¶ |
| | | * |
| | | * @param num æ°å |
| | | * @return çå符串 |
| | | */ |
| | | public static String to62String(long num) { |
| | | return NumberUtil.to62String(num); |
| | | } |
| | | |
| | | /** |
| | | * å°éåæ¼æ¥æå符串ï¼é»è®¤ä½¿ç¨`,`æ¼æ¥ |
| | | * |
| | | * @param coll the {@code Collection} to convert |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Collection<?> coll) { |
| | | return StringUtil.join(coll); |
| | | } |
| | | |
| | | /** |
| | | * å°éåæ¼æ¥æå符串ï¼é»è®¤æå®åé符 |
| | | * |
| | | * @param coll the {@code Collection} to convert |
| | | * @param delim the delimiter to use (typically a ",") |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Collection<?> coll, String delim) { |
| | | return StringUtil.join(coll, delim); |
| | | } |
| | | |
| | | /** |
| | | * å°æ°ç»æ¼æ¥æå符串ï¼é»è®¤ä½¿ç¨`,`æ¼æ¥ |
| | | * |
| | | * @param arr the array to display |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Object[] arr) { |
| | | return StringUtil.join(arr); |
| | | } |
| | | |
| | | /** |
| | | * å°æ°ç»æ¼æ¥æå符串ï¼é»è®¤æå®åé符 |
| | | * |
| | | * @param arr the array to display |
| | | * @param delim the delimiter to use (typically a ",") |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Object[] arr, String delim) { |
| | | return StringUtil.join(arr, delim); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼ä¸å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼ä¸å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @return åååçéå |
| | | */ |
| | | public static List<String> split(CharSequence str, char separator) { |
| | | return StringUtil.split(str, separator, -1); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @return åååçéå |
| | | */ |
| | | public static List<String> splitTrim(CharSequence str, char separator) { |
| | | return StringUtil.splitTrim(str, separator); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @return åååçéå |
| | | */ |
| | | public static List<String> splitTrim(CharSequence str, CharSequence separator) { |
| | | return StringUtil.splitTrim(str, separator); |
| | | } |
| | | |
| | | /** |
| | | * åå² å符串 |
| | | * |
| | | * @param str å符串 |
| | | * @param delimiter åå²ç¬¦ |
| | | * @return å符串æ°ç» |
| | | */ |
| | | public static String[] split(@Nullable String str, @Nullable String delimiter) { |
| | | return StringUtil.delimitedListToStringArray(str, delimiter); |
| | | } |
| | | |
| | | /** |
| | | * åå² å符串 å é¤å¸¸è§ 空ç½ç¬¦ |
| | | * |
| | | * @param str å符串 |
| | | * @param delimiter åå²ç¬¦ |
| | | * @return å符串æ°ç» |
| | | */ |
| | | public static String[] splitTrim(@Nullable String str, @Nullable String delimiter) { |
| | | return StringUtil.delimitedListToStringArray(str, delimiter, " \t\n\n\f"); |
| | | } |
| | | |
| | | /** |
| | | * å符串æ¯å¦ç¬¦åæå®ç è¡¨è¾¾å¼ |
| | | * |
| | | * <p> |
| | | * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" |
| | | * </p> |
| | | * |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @param str å符串 |
| | | * @return æ¯å¦å¹é
|
| | | */ |
| | | public static boolean simpleMatch(@Nullable String pattern, @Nullable String str) { |
| | | return PatternMatchUtils.simpleMatch(pattern, str); |
| | | } |
| | | |
| | | /** |
| | | * å符串æ¯å¦ç¬¦åæå®ç è¡¨è¾¾å¼ |
| | | * |
| | | * <p> |
| | | * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" |
| | | * </p> |
| | | * |
| | | * @param patterns è¡¨è¾¾å¼ æ°ç» |
| | | * @param str å符串 |
| | | * @return æ¯å¦å¹é
|
| | | */ |
| | | public static boolean simpleMatch(@Nullable String[] patterns, String str) { |
| | | return PatternMatchUtils.simpleMatch(patterns, str); |
| | | } |
| | | |
| | | /** |
| | | * çæuuid |
| | | * |
| | | * @return UUID |
| | | */ |
| | | public static String randomUUID() { |
| | | return StringUtil.randomUUID(); |
| | | } |
| | | |
| | | /** |
| | | * 转ä¹HTMLç¨äºå®å
¨è¿æ»¤ |
| | | * |
| | | * @param html html |
| | | * @return {String} |
| | | */ |
| | | public static String escapeHtml(String html) { |
| | | return StringUtil.escapeHtml(html); |
| | | } |
| | | |
| | | /** |
| | | * éæºæ°çæ |
| | | * |
| | | * @param count å符é¿åº¦ |
| | | * @return éæºæ° |
| | | */ |
| | | public static String random(int count) { |
| | | return StringUtil.random(count); |
| | | } |
| | | |
| | | /** |
| | | * éæºæ°çæ |
| | | * |
| | | * @param count å符é¿åº¦ |
| | | * @param randomType éæºæ°ç±»å« |
| | | * @return éæºæ° |
| | | */ |
| | | public static String random(int count, RandomType randomType) { |
| | | return StringUtil.random(count, randomType); |
| | | } |
| | | |
| | | /** |
| | | * å符串åºååæ md5 |
| | | * |
| | | * @param data Data to digest |
| | | * @return MD5 digest as a hex string |
| | | */ |
| | | public static String md5Hex(final String data) { |
| | | return DigestUtil.md5Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * æ°ç»åºååæ md5 |
| | | * |
| | | * @param bytes the bytes to calculate the digest over |
| | | * @return md5 digest string |
| | | */ |
| | | public static String md5Hex(final byte[] bytes) { |
| | | return DigestUtil.md5Hex(bytes); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * sha1Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha1Hex(String data) { |
| | | return DigestUtil.sha1Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * sha1Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha1Hex(final byte[] bytes) { |
| | | return DigestUtil.sha1Hex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * SHA224Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha224Hex(String data) { |
| | | return DigestUtil.sha224Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * SHA224Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha224Hex(final byte[] bytes) { |
| | | return DigestUtil.sha224Hex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha256Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha256Hex(String data) { |
| | | return DigestUtil.sha256Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * sha256Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha256Hex(final byte[] bytes) { |
| | | return DigestUtil.sha256Hex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha384Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha384Hex(String data) { |
| | | return DigestUtil.sha384Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * sha384Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha384Hex(final byte[] bytes) { |
| | | return DigestUtil.sha384Hex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * sha512Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha512Hex(String data) { |
| | | return DigestUtil.sha512Hex(data); |
| | | } |
| | | |
| | | /** |
| | | * sha512Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String sha512Hex(final byte[] bytes) { |
| | | return DigestUtil.sha512Hex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * hmacMd5 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacMd5Hex(String data, String key) { |
| | | return DigestUtil.hmacMd5Hex(data, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacMd5 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacMd5Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.hmacMd5Hex(bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha1 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha1Hex(String data, String key) { |
| | | return DigestUtil.hmacSha1Hex(data, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha1 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha1Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.hmacSha1Hex(bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha224 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha224Hex(String data, String key) { |
| | | return DigestUtil.hmacSha224Hex(data, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha224 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha224Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.hmacSha224Hex(bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha256 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha256Hex(String data, String key) { |
| | | return DigestUtil.hmacSha256Hex(data, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha256 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha256Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.hmacSha256Hex(bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha384 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha384Hex(String data, String key) { |
| | | return DigestUtil.hmacSha384Hex(data, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha384 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha384Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.hmacSha384Hex(bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha512 Hex |
| | | * |
| | | * @param data Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha512Hex(String data, String key) { |
| | | return DigestUtil.hmacSha512Hex(data, key); |
| | | } |
| | | |
| | | /** |
| | | * hmacSha512 Hex |
| | | * |
| | | * @param bytes Data to digest |
| | | * @param key key |
| | | * @return digest as a hex string |
| | | */ |
| | | public static String hmacSha512Hex(final byte[] bytes, String key) { |
| | | return DigestUtil.hmacSha512Hex(bytes, key); |
| | | } |
| | | |
| | | /** |
| | | * byte æ°ç»åºååæ hex |
| | | * |
| | | * @param bytes bytes to encode |
| | | * @return MD5 digest as a hex string |
| | | */ |
| | | public static String encodeHex(byte[] bytes) { |
| | | return DigestUtil.encodeHex(bytes); |
| | | } |
| | | |
| | | /** |
| | | * å符串ååºååæ hex |
| | | * |
| | | * @param hexString String to decode |
| | | * @return MD5 digest as a hex string |
| | | */ |
| | | public static byte[] decodeHex(final String hexString) { |
| | | return DigestUtil.decodeHex(hexString); |
| | | } |
| | | |
| | | /** |
| | | * Base64ç¼ç |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String encodeBase64(String value) { |
| | | return Base64Util.encode(value); |
| | | } |
| | | |
| | | /** |
| | | * Base64ç¼ç |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String encodeBase64(String value, Charset charset) { |
| | | return Base64Util.encode(value, charset); |
| | | } |
| | | |
| | | /** |
| | | * Base64ç¼ç 为URLå®å
¨ |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String encodeBase64UrlSafe(String value) { |
| | | return Base64Util.encodeUrlSafe(value); |
| | | } |
| | | |
| | | /** |
| | | * Base64ç¼ç 为URLå®å
¨ |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String encodeBase64UrlSafe(String value, Charset charset) { |
| | | return Base64Util.encodeUrlSafe(value, charset); |
| | | } |
| | | |
| | | /** |
| | | * Base64è§£ç |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String decodeBase64(String value) { |
| | | return Base64Util.decode(value); |
| | | } |
| | | |
| | | /** |
| | | * Base64è§£ç |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String decodeBase64(String value, Charset charset) { |
| | | return Base64Util.decode(value, charset); |
| | | } |
| | | |
| | | /** |
| | | * Base64URLå®å
¨è§£ç |
| | | * |
| | | * @param value å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String decodeBase64UrlSafe(String value) { |
| | | return Base64Util.decodeUrlSafe(value); |
| | | } |
| | | |
| | | /** |
| | | * Base64URLå®å
¨è§£ç |
| | | * |
| | | * @param value å符串 |
| | | * @param charset å符é |
| | | * @return {String} |
| | | */ |
| | | public static String decodeBase64UrlSafe(String value, Charset charset) { |
| | | return Base64Util.decodeUrlSafe(value, charset); |
| | | } |
| | | |
| | | /** |
| | | * å
³é Closeable |
| | | * |
| | | * @param closeable èªå¨å
³é |
| | | */ |
| | | public static void closeQuietly(@Nullable Closeable closeable) { |
| | | IoUtil.closeQuietly(closeable); |
| | | } |
| | | |
| | | /** |
| | | * InputStream to String utf-8 |
| | | * |
| | | * @param input the <code>InputStream</code> to read from |
| | | * @return the requested String |
| | | * @throws NullPointerException if the input is null |
| | | */ |
| | | public static String readToString(InputStream input) { |
| | | return IoUtil.readToString(input); |
| | | } |
| | | |
| | | /** |
| | | * InputStream to String |
| | | * |
| | | * @param input the <code>InputStream</code> to read from |
| | | * @param charset the <code>Charset</code> |
| | | * @return the requested String |
| | | * @throws NullPointerException if the input is null |
| | | */ |
| | | public static String readToString(@Nullable InputStream input, Charset charset) { |
| | | return IoUtil.readToString(input, charset); |
| | | } |
| | | |
| | | /** |
| | | * InputStream to bytes æ°ç» |
| | | * |
| | | * @param input InputStream |
| | | * @return the requested byte array |
| | | */ |
| | | public static byte[] readToByteArray(@Nullable InputStream input) { |
| | | return IoUtil.readToByteArray(input); |
| | | } |
| | | |
| | | /** |
| | | * 读åæä»¶ä¸ºå符串 |
| | | * |
| | | * @param file the file to read, must not be {@code null} |
| | | * @return the file contents, never {@code null} |
| | | */ |
| | | public static String readToString(final File file) { |
| | | return FileUtil.readToString(file); |
| | | } |
| | | |
| | | /** |
| | | * 读åæä»¶ä¸ºå符串 |
| | | * |
| | | * @param file the file to read, must not be {@code null} |
| | | * @param encoding the encoding to use, {@code null} means platform default |
| | | * @return the file contents, never {@code null} |
| | | */ |
| | | public static String readToString(File file, Charset encoding) { |
| | | return FileUtil.readToString(file, encoding); |
| | | } |
| | | |
| | | /** |
| | | * 读åæä»¶ä¸º byte æ°ç» |
| | | * |
| | | * @param file the file to read, must not be {@code null} |
| | | * @return the file contents, never {@code null} |
| | | */ |
| | | public static byte[] readToByteArray(File file) { |
| | | return FileUtil.readToByteArray(file); |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡åºååæjsonå符串 |
| | | * |
| | | * @param object javaBean |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static String toJson(Object object) { |
| | | return JsonUtil.toJson(object); |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡åºååæ json byte æ°ç» |
| | | * |
| | | * @param object javaBean |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static byte[] toJsonAsBytes(Object object) { |
| | | return JsonUtil.toJsonAsBytes(object); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param jsonString jsonString |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(String jsonString) { |
| | | return JsonUtil.readTree(jsonString); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param in InputStream |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(InputStream in) { |
| | | return JsonUtil.readTree(in); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param content content |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(byte[] content) { |
| | | return JsonUtil.readTree(content); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param jsonParser JsonParser |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(JsonParser jsonParser) { |
| | | return JsonUtil.readTree(jsonParser); |
| | | } |
| | | |
| | | /** |
| | | * å°json byte æ°ç»ååºååæå¯¹è±¡ |
| | | * |
| | | * @param bytes json bytes |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T readJson(byte[] bytes, Class<T> valueType) { |
| | | return JsonUtil.parse(bytes, valueType); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param jsonString jsonString |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T readJson(String jsonString, Class<T> valueType) { |
| | | return JsonUtil.parse(jsonString, valueType); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param in InputStream |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T readJson(InputStream in, Class<T> valueType) { |
| | | return JsonUtil.parse(in, valueType); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param bytes bytes |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T readJson(byte[] bytes, TypeReference<T> typeReference) { |
| | | return JsonUtil.parse(bytes, typeReference); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param jsonString jsonString |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T readJson(String jsonString, TypeReference<T> typeReference) { |
| | | return JsonUtil.parse(jsonString, typeReference); |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param in InputStream |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T readJson(InputStream in, TypeReference<T> typeReference) { |
| | | return JsonUtil.parse(in, typeReference); |
| | | } |
| | | |
| | | /** |
| | | * url ç¼ç |
| | | * |
| | | * @param source the String to be encoded |
| | | * @return the encoded String |
| | | */ |
| | | public static String urlEncode(String source) { |
| | | return UrlUtil.encode(source, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * url ç¼ç |
| | | * |
| | | * @param source the String to be encoded |
| | | * @param charset the character encoding to encode to |
| | | * @return the encoded String |
| | | */ |
| | | public static String urlEncode(String source, Charset charset) { |
| | | return UrlUtil.encode(source, charset); |
| | | } |
| | | |
| | | /** |
| | | * url è§£ç |
| | | * |
| | | * @param source the encoded String |
| | | * @return the decoded value |
| | | * @throws IllegalArgumentException when the given source contains invalid encoded sequences |
| | | * @see StringUtils#uriDecode(String, Charset) |
| | | * @see java.net.URLDecoder#decode(String, String) |
| | | */ |
| | | public static String urlDecode(String source) { |
| | | return StringUtils.uriDecode(source, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * url è§£ç |
| | | * |
| | | * @param source the encoded String |
| | | * @param charset the character encoding to use |
| | | * @return the decoded value |
| | | * @throws IllegalArgumentException when the given source contains invalid encoded sequences |
| | | * @see StringUtils#uriDecode(String, Charset) |
| | | * @see java.net.URLDecoder#decode(String, String) |
| | | */ |
| | | public static String urlDecode(String source, Charset charset) { |
| | | return StringUtils.uriDecode(source, charset); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTime(Date date) { |
| | | return DateUtil.formatDateTime(date); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDate(Date date) { |
| | | return DateUtil.formatDate(date); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´æ ¼å¼å |
| | | * |
| | | * @param date æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatTime(Date date) { |
| | | return DateUtil.formatTime(date); |
| | | } |
| | | |
| | | /** |
| | | * å¯¹è±¡æ ¼å¼å æ¯ææ°åï¼dateï¼java8æ¶é´ |
| | | * |
| | | * @param object æ ¼å¼å对象 |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ ¼å¼ååçå符串 |
| | | */ |
| | | public static String format(Object object, String pattern) { |
| | | if (object instanceof Number) { |
| | | DecimalFormat decimalFormat = new DecimalFormat(pattern); |
| | | return decimalFormat.format(object); |
| | | } else if (object instanceof Date) { |
| | | return DateUtil.format((Date) object, pattern); |
| | | } else if (object instanceof TemporalAccessor) { |
| | | return DateTimeUtil.format((TemporalAccessor) object, pattern); |
| | | } |
| | | throw new IllegalArgumentException("æªæ¯æç对象:" + object + ",æ ¼å¼:" + object); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @return æ¶é´ |
| | | */ |
| | | public static Date parseDate(String dateStr, String pattern) { |
| | | return DateUtil.parse(dateStr, pattern); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param format ConcurrentDateFormat |
| | | * @return æ¶é´ |
| | | */ |
| | | public static Date parse(String dateStr, ConcurrentDateFormat format) { |
| | | return DateUtil.parse(dateStr, format); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDateTime(TemporalAccessor temporal) { |
| | | return DateTimeUtil.formatDateTime(temporal); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatDate(TemporalAccessor temporal) { |
| | | return DateTimeUtil.formatDate(temporal); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´æ ¼å¼å |
| | | * |
| | | * @param temporal æ¶é´ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String formatTime(TemporalAccessor temporal) { |
| | | return DateTimeUtil.formatTime(temporal); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param formatter DateTimeFormatter |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDateTime parseDateTime(String dateStr, DateTimeFormatter formatter) { |
| | | return DateTimeUtil.parseDateTime(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDateTime parseDateTime(String dateStr) { |
| | | return DateTimeUtil.parseDateTime(dateStr); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param formatter DateTimeFormatter |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDate parseDate(String dateStr, DateTimeFormatter formatter) { |
| | | return DateTimeUtil.parseDate(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¥æ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalDate parseDate(String dateStr) { |
| | | return DateTimeUtil.parseDate(dateStr, DateTimeUtil.DATE_FORMAT); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @param formatter DateTimeFormatter |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalTime parseTime(String dateStr, DateTimeFormatter formatter) { |
| | | return DateTimeUtil.parseTime(dateStr, formatter); |
| | | } |
| | | |
| | | /** |
| | | * å°å符串转æ¢ä¸ºæ¶é´ |
| | | * |
| | | * @param dateStr æ¶é´å符串 |
| | | * @return æ¶é´ |
| | | */ |
| | | public static LocalTime parseTime(String dateStr) { |
| | | return DateTimeUtil.parseTime(dateStr); |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´æ¯è¾ |
| | | * |
| | | * @param startInclusive the start instant, inclusive, not null |
| | | * @param endExclusive the end instant, exclusive, not null |
| | | * @return a {@code Duration}, not null |
| | | */ |
| | | public static Duration between(Temporal startInclusive, Temporal endExclusive) { |
| | | return Duration.between(startInclusive, endExclusive); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾2个 æ¶é´å·® |
| | | * |
| | | * @param startDate å¼å§æ¶é´ |
| | | * @param endDate ç»ææ¶é´ |
| | | * @return æ¶é´é´é |
| | | */ |
| | | public static Duration between(Date startDate, Date endDate) { |
| | | return DateUtil.between(startDate, endDate); |
| | | } |
| | | |
| | | /** |
| | | * 对象类åè½¬æ¢ |
| | | * |
| | | * @param source the source object |
| | | * @param targetType the target type |
| | | * @param <T> æ³åæ è®° |
| | | * @return the converted value |
| | | * @throws IllegalArgumentException if targetType is {@code null}, |
| | | * or sourceType is {@code null} but source is not {@code null} |
| | | */ |
| | | @Nullable |
| | | public static <T> T convert(@Nullable Object source, Class<T> targetType) { |
| | | return ConvertUtil.convert(source, targetType); |
| | | } |
| | | |
| | | /** |
| | | * 对象类åè½¬æ¢ |
| | | * |
| | | * @param source the source object |
| | | * @param sourceType the source type |
| | | * @param targetType the target type |
| | | * @param <T> æ³åæ è®° |
| | | * @return the converted value |
| | | * @throws IllegalArgumentException if targetType is {@code null}, |
| | | * or sourceType is {@code null} but source is not {@code null} |
| | | */ |
| | | @Nullable |
| | | public static <T> T convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
| | | return ConvertUtil.convert(source, sourceType, targetType); |
| | | } |
| | | |
| | | /** |
| | | * 对象类åè½¬æ¢ |
| | | * |
| | | * @param source the source object |
| | | * @param targetType the target type |
| | | * @param <T> æ³åæ è®° |
| | | * @return the converted value |
| | | * @throws IllegalArgumentException if targetType is {@code null}, |
| | | * or sourceType is {@code null} but source is not {@code null} |
| | | */ |
| | | @Nullable |
| | | public static <T> T convert(@Nullable Object source, TypeDescriptor targetType) { |
| | | return ConvertUtil.convert(source, targetType); |
| | | } |
| | | |
| | | /** |
| | | * è·åæ¹æ³åæ°ä¿¡æ¯ |
| | | * |
| | | * @param constructor æé å¨ |
| | | * @param parameterIndex åæ°åºå· |
| | | * @return {MethodParameter} |
| | | */ |
| | | public static MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) { |
| | | return ClassUtil.getMethodParameter(constructor, parameterIndex); |
| | | } |
| | | |
| | | /** |
| | | * è·åæ¹æ³åæ°ä¿¡æ¯ |
| | | * |
| | | * @param method æ¹æ³ |
| | | * @param parameterIndex åæ°åºå· |
| | | * @return {MethodParameter} |
| | | */ |
| | | public static MethodParameter getMethodParameter(Method method, int parameterIndex) { |
| | | return ClassUtil.getMethodParameter(method, parameterIndex); |
| | | } |
| | | |
| | | /** |
| | | * è·åAnnotation注解 |
| | | * |
| | | * @param annotatedElement AnnotatedElement |
| | | * @param annotationType 注解类 |
| | | * @param <A> æ³åæ è®° |
| | | * @return {Annotation} |
| | | */ |
| | | @Nullable |
| | | public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) { |
| | | return AnnotatedElementUtils.findMergedAnnotation(annotatedElement, annotationType); |
| | | } |
| | | |
| | | /** |
| | | * è·åAnnotationï¼å
æ¾æ¹æ³ï¼æ²¡æååæ¾æ¹æ³ä¸çç±» |
| | | * |
| | | * @param method Method |
| | | * @param annotationType 注解类 |
| | | * @param <A> æ³åæ è®° |
| | | * @return {Annotation} |
| | | */ |
| | | @Nullable |
| | | public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) { |
| | | return ClassUtil.getAnnotation(method, annotationType); |
| | | } |
| | | |
| | | /** |
| | | * è·åAnnotationï¼å
æ¾HandlerMethodï¼æ²¡æå忾坹åºçç±» |
| | | * |
| | | * @param handlerMethod HandlerMethod |
| | | * @param annotationType 注解类 |
| | | * @param <A> æ³åæ è®° |
| | | * @return {Annotation} |
| | | */ |
| | | @Nullable |
| | | public static <A extends Annotation> A getAnnotation(HandlerMethod handlerMethod, Class<A> annotationType) { |
| | | return ClassUtil.getAnnotation(handlerMethod, annotationType); |
| | | } |
| | | |
| | | /** |
| | | * å®ä¾å对象 |
| | | * |
| | | * @param clazz ç±» |
| | | * @param <T> æ³åæ è®° |
| | | * @return 对象 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T newInstance(Class<?> clazz) { |
| | | return (T) BeanUtil.instantiateClass(clazz); |
| | | } |
| | | |
| | | /** |
| | | * å®ä¾å对象 |
| | | * |
| | | * @param clazzStr ç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return 对象 |
| | | */ |
| | | public static <T> T newInstance(String clazzStr) { |
| | | return BeanUtil.newInstance(clazzStr); |
| | | } |
| | | |
| | | /** |
| | | * è·åBeanç屿§ |
| | | * |
| | | * @param bean bean |
| | | * @param propertyName 屿§å |
| | | * @return 屿§å¼ |
| | | */ |
| | | @Nullable |
| | | public static Object getProperty(@Nullable Object bean, String propertyName) { |
| | | return BeanUtil.getProperty(bean, propertyName); |
| | | } |
| | | |
| | | /** |
| | | * 设置Bean屿§ |
| | | * |
| | | * @param bean bean |
| | | * @param propertyName 屿§å |
| | | * @param value 屿§å¼ |
| | | */ |
| | | public static void setProperty(Object bean, String propertyName, Object value) { |
| | | BeanUtil.setProperty(bean, propertyName, value); |
| | | } |
| | | |
| | | /** |
| | | * æµ
å¤å¶ |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T clone(@Nullable T source) { |
| | | return BeanUtil.clone(source); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ï¼æ¯æ Map å Bean |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param clazz ç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T copy(@Nullable Object source, Class<T> clazz) { |
| | | return BeanUtil.copy(source, clazz); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ï¼æ¯æ Map å Bean |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param targetBean éè¦èµå¼ç对象 |
| | | */ |
| | | public static void copy(@Nullable Object source, @Nullable Object targetBean) { |
| | | BeanUtil.copy(source, targetBean); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ï¼source å¯¹è±¡å±æ§åé null 夿 |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param targetBean éè¦èµå¼ç对象 |
| | | */ |
| | | public static void copyNonNull(@Nullable Object source, @Nullable Object targetBean) { |
| | | BeanUtil.copyNonNull(source, targetBean); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ï¼å¹¶å¯¹ä¸åç±»å屿§è¿è¡è½¬æ¢ |
| | | * |
| | | * @param source æºå¯¹è±¡ |
| | | * @param clazz ç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | @Nullable |
| | | public static <T> T copyWithConvert(@Nullable Object source, Class<T> clazz) { |
| | | return BeanUtil.copyWithConvert(source, clazz); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å表对象 |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param sourceList æºå表 |
| | | * @param targetClazz è½¬æ¢æçç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | */ |
| | | public static <T> List<T> copy(@Nullable Collection<?> sourceList, Class<T> targetClazz) { |
| | | return BeanUtil.copy(sourceList, targetClazz); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å表对象ï¼å¹¶å¯¹ä¸åç±»å屿§è¿è¡è½¬æ¢ |
| | | * |
| | | * <p> |
| | | * æ¯æ map bean copy |
| | | * </p> |
| | | * |
| | | * @param sourceList æºå¯¹è±¡å表 |
| | | * @param targetClazz è½¬æ¢æçç±» |
| | | * @param <T> æ³åæ è®° |
| | | * @return List |
| | | */ |
| | | public static <T> List<T> copyWithConvert(@Nullable Collection<?> sourceList, Class<T> targetClazz) { |
| | | return BeanUtil.copyWithConvert(sourceList, targetClazz); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´å¯¹è±¡ï¼æ©å± Spring çæ·è´æ¹æ³ |
| | | * |
| | | * @param source the source bean |
| | | * @param clazz the target bean class |
| | | * @param <T> æ³åæ è®° |
| | | * @return T |
| | | * @throws BeansException if the copying failed |
| | | */ |
| | | @Nullable |
| | | public static <T> T copyProperties(@Nullable Object source, Class<T> clazz) throws BeansException { |
| | | return BeanUtil.copyProperties(source, clazz); |
| | | } |
| | | |
| | | /** |
| | | * æ·è´åè¡¨å¯¹è±¡ï¼æ©å± Spring çæ·è´æ¹æ³ |
| | | * |
| | | * @param sourceList the source list bean |
| | | * @param targetClazz the target bean class |
| | | * @param <T> æ³åæ è®° |
| | | * @return List |
| | | * @throws BeansException if the copying failed |
| | | */ |
| | | public static <T> List<T> copyProperties(@Nullable Collection<?> sourceList, Class<T> targetClazz) throws BeansException { |
| | | return BeanUtil.copyProperties(sourceList, targetClazz); |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡è£
æmapå½¢å¼ |
| | | * |
| | | * @param bean æºå¯¹è±¡ |
| | | * @return {Map} |
| | | */ |
| | | public static Map<String, Object> toMap(@Nullable Object bean) { |
| | | return BeanUtil.toMap(bean); |
| | | } |
| | | |
| | | /** |
| | | * å°map 转为 bean |
| | | * |
| | | * @param beanMap map |
| | | * @param valueType 对象类å |
| | | * @param <T> æ³åæ è®° |
| | | * @return {T} |
| | | */ |
| | | public static <T> T toBean(Map<String, Object> beanMap, Class<T> valueType) { |
| | | return BeanUtil.toBean(beanMap, valueType); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import java.security.SecureRandom; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | | * ä¸äºå¸¸ç¨çåä¾å¯¹è±¡ |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class Holder { |
| | | |
| | | /** |
| | | * RANDOM |
| | | */ |
| | | public final static Random RANDOM = new Random(); |
| | | |
| | | /** |
| | | * SECURE_RANDOM |
| | | */ |
| | | public final static SecureRandom SECURE_RANDOM = new SecureRandom(); |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | |
| | | package com.vci.web.util; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | |
| | | /** |
| | | * æµå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class IoUtil extends org.springframework.util.StreamUtils { |
| | | |
| | | /** |
| | | * closeQuietly |
| | | * |
| | | * @param closeable èªå¨å
³é |
| | | */ |
| | | public static void closeQuietly(@Nullable Closeable closeable) { |
| | | if (closeable == null) { |
| | | return; |
| | | } |
| | | if (closeable instanceof Flushable) { |
| | | try { |
| | | ((Flushable) closeable).flush(); |
| | | } catch (IOException ignored) { |
| | | // ignore |
| | | } |
| | | } |
| | | try { |
| | | closeable.close(); |
| | | } catch (IOException ignored) { |
| | | // ignore |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * InputStream to String utf-8 |
| | | * |
| | | * @param input the <code>InputStream</code> to read from |
| | | * @return the requested String |
| | | */ |
| | | public static String readToString(InputStream input) { |
| | | return readToString(input, Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * InputStream to String |
| | | * |
| | | * @param input the <code>InputStream</code> to read from |
| | | * @param charset the <code>Charset</code> |
| | | * @return the requested String |
| | | */ |
| | | public static String readToString(@Nullable InputStream input, Charset charset) { |
| | | try { |
| | | return IoUtil.copyToString(input, charset); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } finally { |
| | | IoUtil.closeQuietly(input); |
| | | } |
| | | } |
| | | |
| | | public static byte[] readToByteArray(@Nullable InputStream input) { |
| | | try { |
| | | return IoUtil.copyToByteArray(input); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } finally { |
| | | IoUtil.closeQuietly(input); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Writes chars from a <code>String</code> to bytes on an |
| | | * <code>OutputStream</code> using the specified character encoding. |
| | | * <p> |
| | | * This method uses {@link String#getBytes(String)}. |
| | | * </p> |
| | | * @param data the <code>String</code> to write, null ignored |
| | | * @param output the <code>OutputStream</code> to write to |
| | | * @param encoding the encoding to use, null means platform default |
| | | * @throws NullPointerException if output is null |
| | | * @throws IOException if an I/O error occurs |
| | | */ |
| | | public static void write(@Nullable final String data, final OutputStream output, final Charset encoding) throws IOException { |
| | | if (data != null) { |
| | | output.write(data.getBytes(encoding)); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | /** |
| | | * æ°åç±»åå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class NumberUtil extends org.springframework.util.NumberUtils { |
| | | |
| | | //----------------------------------------------------------------------- |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>int</code>, returning |
| | | * <code>zero</code> if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toInt(null) = 0 |
| | | * NumberUtil.toInt("") = 0 |
| | | * NumberUtil.toInt("1") = 1 |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @return the int represented by the string, or <code>zero</code> if |
| | | * conversion fails |
| | | */ |
| | | public static int toInt(final String str) { |
| | | return toInt(str, -1); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>int</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toInt(null, 1) = 1 |
| | | * NumberUtil.toInt("", 1) = 1 |
| | | * NumberUtil.toInt("1", 0) = 1 |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the int represented by the string, or the default if conversion fails |
| | | */ |
| | | public static int toInt(@Nullable final String str, final int defaultValue) { |
| | | if (str == null) { |
| | | return defaultValue; |
| | | } |
| | | try { |
| | | return Integer.valueOf(str); |
| | | } catch (final NumberFormatException nfe) { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>long</code>, returning |
| | | * <code>zero</code> if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toLong(null) = 0L |
| | | * NumberUtil.toLong("") = 0L |
| | | * NumberUtil.toLong("1") = 1L |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @return the long represented by the string, or <code>0</code> if |
| | | * conversion fails |
| | | */ |
| | | public static long toLong(final String str) { |
| | | return toLong(str, 0L); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>long</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toLong(null, 1L) = 1L |
| | | * NumberUtil.toLong("", 1L) = 1L |
| | | * NumberUtil.toLong("1", 0L) = 1L |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the long represented by the string, or the default if conversion fails |
| | | */ |
| | | public static long toLong(@Nullable final String str, final long defaultValue) { |
| | | if (str == null) { |
| | | return defaultValue; |
| | | } |
| | | try { |
| | | return Long.valueOf(str); |
| | | } catch (final NumberFormatException nfe) { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @return double value |
| | | */ |
| | | public static Double toDouble(String value) { |
| | | return toDouble(value, null); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return double value |
| | | */ |
| | | public static Double toDouble(@Nullable String value, Double defaultValue) { |
| | | if (value != null) { |
| | | return Double.valueOf(value.trim()); |
| | | } |
| | | return defaultValue; |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @return double value |
| | | */ |
| | | public static Float toFloat(String value) { |
| | | return toFloat(value, null); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return double value |
| | | */ |
| | | public static Float toFloat(@Nullable String value, Float defaultValue) { |
| | | if (value != null) { |
| | | return Float.valueOf(value.trim()); |
| | | } |
| | | return defaultValue; |
| | | } |
| | | |
| | | /** |
| | | * All possible chars for representing a number as a String |
| | | */ |
| | | private final static char[] DIGITS = { |
| | | '0', '1', '2', '3', '4', '5', |
| | | '6', '7', '8', '9', 'a', 'b', |
| | | 'c', 'd', 'e', 'f', 'g', 'h', |
| | | 'i', 'j', 'k', 'l', 'm', 'n', |
| | | 'o', 'p', 'q', 'r', 's', 't', |
| | | 'u', 'v', 'w', 'x', 'y', 'z', |
| | | 'A', 'B', 'C', 'D', 'E', 'F', |
| | | 'G', 'H', 'I', 'J', 'K', 'L', |
| | | 'M', 'N', 'O', 'P', 'Q', 'R', |
| | | 'S', 'T', 'U', 'V', 'W', 'X', |
| | | 'Y', 'Z' |
| | | }; |
| | | |
| | | /** |
| | | * å° long 转çå符串 为 62 è¿å¶ |
| | | * |
| | | * @param i æ°å |
| | | * @return çå符串 |
| | | */ |
| | | public static String to62String(long i) { |
| | | int radix = DIGITS.length; |
| | | char[] buf = new char[65]; |
| | | int charPos = 64; |
| | | i = -i; |
| | | while (i <= -radix) { |
| | | buf[charPos--] = DIGITS[(int) (-(i % radix))]; |
| | | i = i / radix; |
| | | } |
| | | buf[charPos] = DIGITS[(int) (-i)]; |
| | | |
| | | return new String(buf, charPos, (65 - charPos)); |
| | | } |
| | | |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | /** |
| | | * 对象工å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class ObjectUtil extends org.springframework.util.ObjectUtils { |
| | | |
| | | /** |
| | | * 夿å
ç´ ä¸ä¸ºç©º |
| | | * @param obj object |
| | | * @return boolean |
| | | */ |
| | | public static boolean isNotEmpty(@Nullable Object obj) { |
| | | return !ObjectUtil.isEmpty(obj); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | |
| | | import lombok.Getter; |
| | | import lombok.RequiredArgsConstructor; |
| | | |
| | | /** |
| | | * çæçéæºæ°ç±»å |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | @Getter |
| | | @RequiredArgsConstructor |
| | | public enum RandomType { |
| | | /** |
| | | * INT STRING ALL |
| | | */ |
| | | INT(RandomType.INT_STR), |
| | | STRING(RandomType.STR_STR), |
| | | ALL(RandomType.ALL_STR); |
| | | |
| | | private final String factor; |
| | | |
| | | /** |
| | | * éæºå符串å å |
| | | */ |
| | | private static final String INT_STR = "0123456789"; |
| | | private static final String STR_STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| | | private static final String ALL_STR = INT_STR + STR_STR; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | /** |
| | | * éæ String æ± |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public interface StringPool { |
| | | |
| | | String AMPERSAND = "&"; |
| | | String AND = "and"; |
| | | String AT = "@"; |
| | | String ASTERISK = "*"; |
| | | String STAR = ASTERISK; |
| | | String SLASH = "/"; |
| | | String BACK_SLASH = "\\"; |
| | | String DOUBLE_SLASH = "#//"; |
| | | String COLON = ":"; |
| | | String COMMA = ","; |
| | | String DASH = "-"; |
| | | String DOLLAR = "$"; |
| | | String DOT = "."; |
| | | String EMPTY = ""; |
| | | String EMPTY_JSON = "{}"; |
| | | String EQUALS = "="; |
| | | String FALSE = "false"; |
| | | String HASH = "#"; |
| | | String HAT = "^"; |
| | | String LEFT_BRACE = "{"; |
| | | String LEFT_BRACKET = "("; |
| | | String LEFT_CHEV = "<"; |
| | | String NEWLINE = "\n"; |
| | | String N = "n"; |
| | | String NO = "no"; |
| | | String NULL = "null"; |
| | | String OFF = "off"; |
| | | String ON = "on"; |
| | | String PERCENT = "%"; |
| | | String PIPE = "|"; |
| | | String PLUS = "+"; |
| | | String QUESTION_MARK = "?"; |
| | | String EXCLAMATION_MARK = "!"; |
| | | String QUOTE = "\""; |
| | | String RETURN = "\r"; |
| | | String TAB = "\t"; |
| | | String RIGHT_BRACE = "}"; |
| | | String RIGHT_BRACKET = ")"; |
| | | String RIGHT_CHEV = ">"; |
| | | String SEMICOLON = ";"; |
| | | String SINGLE_QUOTE = "'"; |
| | | String BACKTICK = "`"; |
| | | String SPACE = " "; |
| | | String TILDA = "~"; |
| | | String LEFT_SQ_BRACKET = "["; |
| | | String RIGHT_SQ_BRACKET = "]"; |
| | | String TRUE = "true"; |
| | | String UNDERSCORE = "_"; |
| | | String UTF_8 = "UTF-8"; |
| | | String GBK = "GBK"; |
| | | String ISO_8859_1 = "ISO-8859-1"; |
| | | String Y = "y"; |
| | | String YES = "yes"; |
| | | String ONE = "1"; |
| | | String ZERO = "0"; |
| | | String MINUS_ONE = "-1"; |
| | | String DOLLAR_LEFT_BRACE= "${"; |
| | | String UNKNOWN = "unknown"; |
| | | String GET = "GET"; |
| | | String POST = "POST"; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.util.PatternMatchUtils; |
| | | import org.springframework.web.util.HtmlUtils; |
| | | import com.vci.web.support.StrSpliter; |
| | | import java.io.StringReader; |
| | | import java.io.StringWriter; |
| | | import java.text.MessageFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.ThreadLocalRandom; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * ç»§æ¿èªSpring utilçå·¥å
·ç±»ï¼åå°jarä¾èµ |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class StringUtil extends org.springframework.util.StringUtils { |
| | | |
| | | public static final int INDEX_NOT_FOUND = -1; |
| | | // public static final int INDEX_NOT_FOUND = 0; |
| | | |
| | | /** |
| | | * Check whether the given {@code CharSequence} contains actual <em>text</em>. |
| | | * <p>More specifically, this method returns {@code true} if the |
| | | * {@code CharSequence} is not {@code null}, its length is greater than |
| | | * 0, and it contains at least one non-whitespace character. |
| | | * <pre class="code"> |
| | | * StringUtil.isBlank(null) = true |
| | | * StringUtil.isBlank("") = true |
| | | * StringUtil.isBlank(" ") = true |
| | | * StringUtil.isBlank("12345") = false |
| | | * StringUtil.isBlank(" 12345 ") = false |
| | | * </pre> |
| | | * |
| | | * @param cs the {@code CharSequence} to check (may be {@code null}) |
| | | * @return {@code true} if the {@code CharSequence} is not {@code null}, |
| | | * its length is greater than 0, and it does not contain whitespace only |
| | | * @see Character#isWhitespace |
| | | */ |
| | | public static boolean isBlank(final CharSequence cs) { |
| | | return !StringUtil.hasText(cs); |
| | | } |
| | | |
| | | /** |
| | | * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p> |
| | | * <pre> |
| | | * StringUtil.isNotBlank(null) = false |
| | | * StringUtil.isNotBlank("") = false |
| | | * StringUtil.isNotBlank(" ") = false |
| | | * StringUtil.isNotBlank("bob") = true |
| | | * StringUtil.isNotBlank(" bob ") = true |
| | | * </pre> |
| | | * |
| | | * @param cs the CharSequence to check, may be null |
| | | * @return {@code true} if the CharSequence is |
| | | * not empty and not null and not whitespace |
| | | * @see Character#isWhitespace |
| | | */ |
| | | public static boolean isNotBlank(final CharSequence cs) { |
| | | return StringUtil.hasText(cs); |
| | | } |
| | | |
| | | /** |
| | | * æ ä»»æ ä¸ä¸ª Blank |
| | | * |
| | | * @param css CharSequence |
| | | * @return boolean |
| | | */ |
| | | public static boolean isAnyBlank(final CharSequence... css) { |
| | | if (ObjectUtil.isEmpty(css)) { |
| | | return true; |
| | | } |
| | | return Stream.of(css).anyMatch(StringUtil::isBlank); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦å
¨é Blank |
| | | * |
| | | * @param css CharSequence |
| | | * @return boolean |
| | | */ |
| | | public static boolean isNoneBlank(final CharSequence... css) { |
| | | if (ObjectUtil.isEmpty(css)) { |
| | | return false; |
| | | } |
| | | return Stream.of(css).allMatch(StringUtil::isNotBlank); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦å
¨ä¸º Blank |
| | | * |
| | | * @param css CharSequence |
| | | * @return boolean |
| | | */ |
| | | public static boolean isAllBlank(final CharSequence... css) { |
| | | return Stream.of(css).allMatch(StringUtil::isBlank); |
| | | } |
| | | |
| | | /** |
| | | * 夿ä¸ä¸ªå符串æ¯å¦æ¯æ°å |
| | | * |
| | | * @param cs the CharSequence to check, may be null |
| | | * @return {boolean} |
| | | */ |
| | | public static boolean isNumeric(final CharSequence cs) { |
| | | if (isBlank(cs)) { |
| | | return false; |
| | | } |
| | | for (int i = cs.length(); --i >= 0; ) { |
| | | int chr = cs.charAt(i); |
| | | if (chr < 48 || chr > 57) { |
| | | return false; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * å°å符串ä¸ç¹å®æ¨¡å¼çåç¬¦è½¬æ¢æmapä¸å¯¹åºçå¼ |
| | | * <p> |
| | | * use: format("my name is ${name}, and i like ${like}!", {"name":"L.cm", "like": "Java"}) |
| | | * |
| | | * @param message éè¦è½¬æ¢çå符串 |
| | | * @param params è½¬æ¢æéçé®å¼å¯¹éå |
| | | * @return 转æ¢åçå符串 |
| | | */ |
| | | public static String format(@Nullable String message, @Nullable Map<String, ?> params) { |
| | | // message 为 null è¿å空å符串 |
| | | if (message == null) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | // åæ°ä¸º null æè
为空 |
| | | if (params == null || params.isEmpty()) { |
| | | return message; |
| | | } |
| | | // æ¿æ¢åé |
| | | StringBuilder sb = new StringBuilder((int) (message.length() * 1.5)); |
| | | int cursor = 0; |
| | | for (int start, end; (start = message.indexOf(StringPool.DOLLAR_LEFT_BRACE, cursor)) != -1 && (end = message.indexOf(StringPool.RIGHT_BRACE, start)) != -1; ) { |
| | | sb.append(message, cursor, start); |
| | | String key = message.substring(start + 2, end); |
| | | Object value = params.get(StringUtil.trimWhitespace(key)); |
| | | sb.append(value == null ? StringPool.EMPTY : value); |
| | | cursor = end + 1; |
| | | } |
| | | sb.append(message.substring(cursor)); |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * å log æ ¼å¼ç format è§å |
| | | * <p> |
| | | * use: format("my name is {}, and i like {}!", "L.cm", "Java") |
| | | * |
| | | * @param message éè¦è½¬æ¢çå符串 |
| | | * @param arguments éè¦æ¿æ¢çåé |
| | | * @return 转æ¢åçå符串 |
| | | */ |
| | | public static String format(@Nullable String message, @Nullable Object... arguments) { |
| | | // message 为 null è¿å空å符串 |
| | | if (message == null) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | // åæ°ä¸º null æè
为空 |
| | | if (arguments == null || arguments.length == 0) { |
| | | return message; |
| | | } |
| | | StringBuilder sb = new StringBuilder((int) (message.length() * 1.5)); |
| | | int cursor = 0; |
| | | int index = 0; |
| | | int argsLength = arguments.length; |
| | | for (int start, end; (start = message.indexOf('{', cursor)) != -1 && (end = message.indexOf('}', start)) != -1 && index < argsLength; ) { |
| | | sb.append(message, cursor, start); |
| | | sb.append(arguments[index]); |
| | | cursor = end + 1; |
| | | index++; |
| | | } |
| | | sb.append(message.substring(cursor)); |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * æ ¼å¼åæ§è¡æ¶é´ï¼åä½ä¸º ms å sï¼ä¿çä¸ä½å°æ° |
| | | * |
| | | * @param nanos çº³ç§ |
| | | * @return æ ¼å¼ååçæ¶é´ |
| | | */ |
| | | public static String format(long nanos) { |
| | | if (nanos < 1) { |
| | | return "0ms"; |
| | | } |
| | | double millis = (double) nanos / (1000 * 1000); |
| | | // ä¸å¤ 1 msï¼æå°åä½ä¸º ms |
| | | if (millis > 1000) { |
| | | return String.format("%.3fs", millis / 1000); |
| | | } else { |
| | | return String.format("%.3fms", millis); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Convert a {@code Collection} into a delimited {@code String} (e.g., CSV). |
| | | * <p>Useful for {@code toString()} implementations. |
| | | * |
| | | * @param coll the {@code Collection} to convert |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Collection<?> coll) { |
| | | return StringUtil.collectionToCommaDelimitedString(coll); |
| | | } |
| | | |
| | | /** |
| | | * Convert a {@code Collection} into a delimited {@code String} (e.g. CSV). |
| | | * <p>Useful for {@code toString()} implementations. |
| | | * |
| | | * @param coll the {@code Collection} to convert |
| | | * @param delim the delimiter to use (typically a ",") |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Collection<?> coll, String delim) { |
| | | return StringUtil.collectionToDelimitedString(coll, delim); |
| | | } |
| | | |
| | | /** |
| | | * Convert a {@code String} array into a comma delimited {@code String} |
| | | * (i.e., CSV). |
| | | * <p>Useful for {@code toString()} implementations. |
| | | * |
| | | * @param arr the array to display |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Object[] arr) { |
| | | return StringUtil.arrayToCommaDelimitedString(arr); |
| | | } |
| | | |
| | | /** |
| | | * Convert a {@code String} array into a delimited {@code String} (e.g. CSV). |
| | | * <p>Useful for {@code toString()} implementations. |
| | | * |
| | | * @param arr the array to display |
| | | * @param delim the delimiter to use (typically a ",") |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Object[] arr, String delim) { |
| | | return StringUtil.arrayToDelimitedString(arr, delim); |
| | | } |
| | | |
| | | /** |
| | | * å符串æ¯å¦ç¬¦åæå®ç è¡¨è¾¾å¼ |
| | | * |
| | | * <p> |
| | | * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" |
| | | * </p> |
| | | * |
| | | * @param pattern è¡¨è¾¾å¼ |
| | | * @param str å符串 |
| | | * @return æ¯å¦å¹é
|
| | | */ |
| | | public static boolean simpleMatch(@Nullable String pattern, @Nullable String str) { |
| | | return PatternMatchUtils.simpleMatch(pattern, str); |
| | | } |
| | | |
| | | /** |
| | | * å符串æ¯å¦ç¬¦åæå®ç è¡¨è¾¾å¼ |
| | | * |
| | | * <p> |
| | | * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" |
| | | * </p> |
| | | * |
| | | * @param patterns è¡¨è¾¾å¼ æ°ç» |
| | | * @param str å符串 |
| | | * @return æ¯å¦å¹é
|
| | | */ |
| | | public static boolean simpleMatch(@Nullable String[] patterns, String str) { |
| | | return PatternMatchUtils.simpleMatch(patterns, str); |
| | | } |
| | | |
| | | /** |
| | | * çæuuid |
| | | * |
| | | * @return UUID |
| | | */ |
| | | public static String randomUUID() { |
| | | ThreadLocalRandom random = ThreadLocalRandom.current(); |
| | | return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY); |
| | | } |
| | | |
| | | /** |
| | | * 转ä¹HTMLç¨äºå®å
¨è¿æ»¤ |
| | | * |
| | | * @param html html |
| | | * @return {String} |
| | | */ |
| | | public static String escapeHtml(String html) { |
| | | return StringUtil.isBlank(html) ? StringPool.EMPTY : HtmlUtils.htmlEscape(html); |
| | | } |
| | | |
| | | /** |
| | | * æ¸
çåç¬¦ä¸²ï¼æ¸
çåºæäºä¸å¯è§å符 |
| | | * |
| | | * @param txt å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String cleanChars(String txt) { |
| | | return txt.replaceAll("[ ã`·â¢ï¿½\\f\\t\\v\\s]", ""); |
| | | } |
| | | |
| | | /** |
| | | * ç¹æ®å符æ£åï¼sqlç¹æ®å符å空ç½ç¬¦ |
| | | */ |
| | | private final static Pattern SPECIAL_CHARS_REGEX = Pattern.compile("[`'\"|/,;()-+*%#·â¢ï¿½ã\\s]"); |
| | | |
| | | /** |
| | | * æ¸
çåç¬¦ä¸²ï¼æ¸
çåºæäºä¸å¯è§å符åä¸äºsqlç¹æ®å符 |
| | | * |
| | | * @param txt ææ¬ |
| | | * @return {String} |
| | | */ |
| | | @Nullable |
| | | public static String cleanText(@Nullable String txt) { |
| | | if (txt == null) { |
| | | return null; |
| | | } |
| | | return SPECIAL_CHARS_REGEX.matcher(txt).replaceAll(StringPool.EMPTY); |
| | | } |
| | | |
| | | /** |
| | | * è·åæ è¯ç¬¦ï¼ç¨äºåæ°æ¸
ç |
| | | * |
| | | * @param param åæ° |
| | | * @return æ¸
çåçæ è¯ç¬¦ |
| | | */ |
| | | @Nullable |
| | | public static String cleanIdentifier(@Nullable String param) { |
| | | if (param == null) { |
| | | return null; |
| | | } |
| | | StringBuilder paramBuilder = new StringBuilder(); |
| | | for (int i = 0; i < param.length(); i++) { |
| | | char c = param.charAt(i); |
| | | if (Character.isJavaIdentifierPart(c)) { |
| | | paramBuilder.append(c); |
| | | } |
| | | } |
| | | return paramBuilder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * éæºæ°çæ |
| | | * |
| | | * @param count å符é¿åº¦ |
| | | * @return éæºæ° |
| | | */ |
| | | public static String random(int count) { |
| | | return StringUtil.random(count, RandomType.ALL); |
| | | } |
| | | |
| | | /** |
| | | * éæºæ°çæ |
| | | * |
| | | * @param count å符é¿åº¦ |
| | | * @param randomType éæºæ°ç±»å« |
| | | * @return éæºæ° |
| | | */ |
| | | public static String random(int count, RandomType randomType) { |
| | | if (count == 0) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | Assert.isTrue(count > 0, "Requested random string length " + count + " is less than 0."); |
| | | final Random random = Holder.SECURE_RANDOM; |
| | | char[] buffer = new char[count]; |
| | | for (int i = 0; i < count; i++) { |
| | | String factor = randomType.getFactor(); |
| | | buffer[i] = factor.charAt(random.nextInt(factor.length())); |
| | | } |
| | | return new String(buffer); |
| | | } |
| | | |
| | | /** |
| | | * æåºçæ ¼å¼åææ¬ï¼ä½¿ç¨{number}å为å ä½ç¬¦<br> |
| | | * ä¾ï¼<br> |
| | | * é常使ç¨ï¼format("this is {0} for {1}", "a", "b") =ã this is a for b<br> |
| | | * |
| | | * @param pattern ææ¬æ ¼å¼ |
| | | * @param arguments åæ° |
| | | * @return æ ¼å¼ååçææ¬ |
| | | */ |
| | | public static String indexedFormat(CharSequence pattern, Object... arguments) { |
| | | return MessageFormat.format(pattern.toString(), arguments); |
| | | } |
| | | |
| | | /** |
| | | * æ ¼å¼åææ¬ï¼ä½¿ç¨ {varName} å ä½<br> |
| | | * map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=ã aValue and bValue |
| | | * |
| | | * @param template ææ¬æ¨¡æ¿ï¼è¢«æ¿æ¢çé¨åç¨ {key} 表示 |
| | | * @param map åæ°å¼å¯¹ |
| | | * @return æ ¼å¼ååçææ¬ |
| | | */ |
| | | public static String format(CharSequence template, Map<?, ?> map) { |
| | | if (null == template) { |
| | | return null; |
| | | } |
| | | if (null == map || map.isEmpty()) { |
| | | return template.toString(); |
| | | } |
| | | |
| | | String template2 = template.toString(); |
| | | for (Map.Entry<?, ?> entry : map.entrySet()) { |
| | | template2 = template2.replace("{" + entry.getKey() + "}", Func.toStr(entry.getValue())); |
| | | } |
| | | return template2; |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼ä¸å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼ä¸å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @return åååçéå |
| | | */ |
| | | public static List<String> split(CharSequence str, char separator, int limit) { |
| | | return split(str, separator, limit, false, false); |
| | | } |
| | | |
| | | /** |
| | | * åå² å符串 å é¤å¸¸è§ 空ç½ç¬¦ |
| | | * |
| | | * @param str å符串 |
| | | * @param delimiter åå²ç¬¦ |
| | | * @return å符串æ°ç» |
| | | */ |
| | | public static String[] splitTrim(@Nullable String str, @Nullable String delimiter) { |
| | | return StringUtil.delimitedListToStringArray(str, delimiter, " \t\n\n\f"); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @return åååçéå |
| | | * @since 3.1.2 |
| | | */ |
| | | public static List<String> splitTrim(CharSequence str, char separator) { |
| | | return splitTrim(str, separator, -1); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @return åååçéå |
| | | * @since 3.2.0 |
| | | */ |
| | | public static List<String> splitTrim(CharSequence str, CharSequence separator) { |
| | | return splitTrim(str, separator, -1); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @return åååçéå |
| | | * @since 3.1.0 |
| | | */ |
| | | public static List<String> splitTrim(CharSequence str, char separator, int limit) { |
| | | return split(str, separator, limit, true, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼å»é¤åååæ¯ä¸ªå
ç´ ä¸¤è¾¹ç空ç½ç¬¦ï¼å»é¤ç©ºç½é¡¹ |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @return åååçéå |
| | | * @since 3.2.0 |
| | | */ |
| | | public static List<String> splitTrim(CharSequence str, CharSequence separator, int limit) { |
| | | return split(str, separator, limit, true, true); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串ï¼ä¸éå¶åçæ°é |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(CharSequence str, char separator, boolean isTrim, boolean ignoreEmpty) { |
| | | return split(str, separator, 0, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.0.8 |
| | | */ |
| | | public static List<String> split(CharSequence str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | if (null == str) { |
| | | return new ArrayList<>(0); |
| | | } |
| | | return StrSpliter.split(str.toString(), separator, limit, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符å符 |
| | | * @param limit éå¶åçæ°ï¼-1ä¸éå¶ |
| | | * @param isTrim æ¯å¦å»é¤åååç¬¦ä¸²åæ¯ä¸ªå
ç´ ä¸¤è¾¹çç©ºæ ¼ |
| | | * @param ignoreEmpty æ¯å¦å¿½ç¥ç©ºä¸² |
| | | * @return åååçéå |
| | | * @since 3.2.0 |
| | | */ |
| | | public static List<String> split(CharSequence str, CharSequence separator, int limit, boolean isTrim, boolean ignoreEmpty) { |
| | | if (null == str) { |
| | | return new ArrayList<>(0); |
| | | } |
| | | final String separatorStr = (null == separator) ? null : separator.toString(); |
| | | return StrSpliter.split(str.toString(), separatorStr, limit, isTrim, ignoreEmpty); |
| | | } |
| | | |
| | | /** |
| | | * ååå符串 |
| | | * |
| | | * @param str 被ååçå符串 |
| | | * @param separator åé符 |
| | | * @return å符串 |
| | | */ |
| | | public static String[] split(CharSequence str, CharSequence separator) { |
| | | if (str == null) { |
| | | return new String[]{}; |
| | | } |
| | | |
| | | final String separatorStr = (null == separator) ? null : separator.toString(); |
| | | return StrSpliter.splitToArray(str.toString(), separatorStr, 0, false, false); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®ç»å®é¿åº¦ï¼å°ç»å®å符串æªå为å¤ä¸ªé¨å |
| | | * |
| | | * @param str å符串 |
| | | * @param len æ¯ä¸ä¸ªå°èçé¿åº¦ |
| | | * @return æªååçå符串æ°ç» |
| | | * @see StrSpliter#splitByLength(String, int) |
| | | */ |
| | | public static String[] split(CharSequence str, int len) { |
| | | if (null == str) { |
| | | return new String[]{}; |
| | | } |
| | | return StrSpliter.splitByLength(str.toString(), len); |
| | | } |
| | | |
| | | /** |
| | | * æå®å符æ¯å¦å¨å符串ä¸åºç°è¿ |
| | | * |
| | | * @param str å符串 |
| | | * @param searchChar è¢«æ¥æ¾çå符 |
| | | * @return æ¯å¦å
å« |
| | | * @since 3.1.2 |
| | | */ |
| | | public static boolean contains(CharSequence str, char searchChar) { |
| | | return indexOf(str, searchChar) > -1; |
| | | } |
| | | |
| | | /** |
| | | * æ¥æ¾æå®å符串æ¯å¦å
嫿å®å符串å表ä¸çä»»æä¸ä¸ªå符串 |
| | | * |
| | | * @param str æå®å符串 |
| | | * @param testStrs éè¦æ£æ¥çå符串æ°ç» |
| | | * @return æ¯å¦å
å«ä»»æä¸ä¸ªå符串 |
| | | * @since 3.2.0 |
| | | */ |
| | | public static boolean containsAny(CharSequence str, CharSequence... testStrs) { |
| | | return null != getContainsStr(str, testStrs); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æ¾æå®å符串æ¯å¦å
嫿å®å符串å表ä¸çä»»æä¸ä¸ªå符串ï¼å¦æå
å«è¿åæ¾å°ç第ä¸ä¸ªå符串 |
| | | * |
| | | * @param str æå®å符串 |
| | | * @param testStrs éè¦æ£æ¥çå符串æ°ç» |
| | | * @return 被å
å«ç第ä¸ä¸ªå符串 |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String getContainsStr(CharSequence str, CharSequence... testStrs) { |
| | | if (isEmpty(str) || Func.isEmpty(testStrs)) { |
| | | return null; |
| | | } |
| | | for (CharSequence checkStr : testStrs) { |
| | | if (str.toString().contains(checkStr)) { |
| | | return checkStr.toString(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦å
å«ç¹å®å符ï¼å¿½ç¥å¤§å°åï¼å¦æç»å®ä¸¤ä¸ªåæ°é½ä¸º<code>null</code>ï¼è¿åtrue |
| | | * |
| | | * @param str è¢«æ£æµå符串 |
| | | * @param testStr 被æµè¯æ¯å¦å
å«çå符串 |
| | | * @return æ¯å¦å
å« |
| | | */ |
| | | public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr) { |
| | | if (null == str) { |
| | | // å¦æè¢«çæµå符串å |
| | | return null == testStr; |
| | | } |
| | | return str.toString().toLowerCase().contains(testStr.toString().toLowerCase()); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æ¾æå®å符串æ¯å¦å
嫿å®å符串å表ä¸çä»»æä¸ä¸ªå符串<br> |
| | | * 忽ç¥å¤§å°å |
| | | * |
| | | * @param str æå®å符串 |
| | | * @param testStrs éè¦æ£æ¥çå符串æ°ç» |
| | | * @return æ¯å¦å
å«ä»»æä¸ä¸ªå符串 |
| | | * @since 3.2.0 |
| | | */ |
| | | public static boolean containsAnyIgnoreCase(CharSequence str, CharSequence... testStrs) { |
| | | return null != getContainsStrIgnoreCase(str, testStrs); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æ¾æå®å符串æ¯å¦å
嫿å®å符串å表ä¸çä»»æä¸ä¸ªå符串ï¼å¦æå
å«è¿åæ¾å°ç第ä¸ä¸ªå符串<br> |
| | | * 忽ç¥å¤§å°å |
| | | * |
| | | * @param str æå®å符串 |
| | | * @param testStrs éè¦æ£æ¥çå符串æ°ç» |
| | | * @return 被å
å«ç第ä¸ä¸ªå符串 |
| | | * @since 3.2.0 |
| | | */ |
| | | public static String getContainsStrIgnoreCase(CharSequence str, CharSequence... testStrs) { |
| | | if (isEmpty(str) || Func.isEmpty(testStrs)) { |
| | | return null; |
| | | } |
| | | for (CharSequence testStr : testStrs) { |
| | | if (containsIgnoreCase(str, testStr)) { |
| | | return testStr.toString(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * æ¹è¿JDK subString<br> |
| | | * indexä»0å¼å§è®¡ç®ï¼æåä¸ä¸ªå符为-1<br> |
| | | * 妿fromåtoä½ç½®ä¸æ ·ï¼è¿å "" <br> |
| | | * 妿fromætoä¸ºè´æ°ï¼åæç
§lengthä»åååæ°ä½ç½®ï¼å¦æç»å¯¹å¼å¤§äºå符串é¿åº¦ï¼åfromå½å°0ï¼toå½å°length<br> |
| | | * 妿ç»è¿ä¿®æ£çindexä¸from大äºtoï¼åäºæ¢fromåto example: <br> |
| | | * abcdefgh 2 3 =ã c <br> |
| | | * abcdefgh 2 -3 =ã cde <br> |
| | | * |
| | | * @param str String |
| | | * @param fromIndex å¼å§çindexï¼å
æ¬ï¼ |
| | | * @param toIndex ç»æçindexï¼ä¸å
æ¬ï¼ |
| | | * @return å串 |
| | | */ |
| | | public static String sub(CharSequence str, int fromIndex, int toIndex) { |
| | | if (isEmpty(str)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | int len = str.length(); |
| | | |
| | | if (fromIndex < 0) { |
| | | fromIndex = len + fromIndex; |
| | | if (fromIndex < 0) { |
| | | fromIndex = 0; |
| | | } |
| | | } else if (fromIndex > len) { |
| | | fromIndex = len; |
| | | } |
| | | |
| | | if (toIndex < 0) { |
| | | toIndex = len + toIndex; |
| | | if (toIndex < 0) { |
| | | toIndex = len; |
| | | } |
| | | } else if (toIndex > len) { |
| | | toIndex = len; |
| | | } |
| | | |
| | | if (toIndex < fromIndex) { |
| | | int tmp = fromIndex; |
| | | fromIndex = toIndex; |
| | | toIndex = tmp; |
| | | } |
| | | |
| | | if (fromIndex == toIndex) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | |
| | | return str.toString().substring(fromIndex, toIndex); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æªååéå符串ä¹åçå符串ï¼ä¸å
æ¬åéå符串<br> |
| | | * 妿ç»å®çå符串为空串ï¼nullæ""ï¼æè
åéå符串为nullï¼è¿ååå符串<br> |
| | | * 妿åéå符串为空串""ï¼åè¿å空串ï¼å¦æåéåç¬¦ä¸²æªæ¾å°ï¼è¿ååå符串 |
| | | * <p> |
| | | * æ åï¼ |
| | | * |
| | | * <pre> |
| | | * StringUtil.subBefore(null, *) = null |
| | | * StringUtil.subBefore("", *) = "" |
| | | * StringUtil.subBefore("abc", "a") = "" |
| | | * StringUtil.subBefore("abcba", "b") = "a" |
| | | * StringUtil.subBefore("abc", "c") = "ab" |
| | | * StringUtil.subBefore("abc", "d") = "abc" |
| | | * StringUtil.subBefore("abc", "") = "" |
| | | * StringUtil.subBefore("abc", null) = "abc" |
| | | * </pre> |
| | | * |
| | | * @param string è¢«æ¥æ¾çå符串 |
| | | * @param separator åéå符串ï¼ä¸å
æ¬ï¼ |
| | | * @param isLastSeparator æ¯å¦æ¥æ¾æåä¸ä¸ªåéå符串ï¼å¤æ¬¡åºç°åéå符串æ¶éåæåä¸ä¸ªï¼ï¼true为éåæåä¸ä¸ª |
| | | * @return åå²åçå符串 |
| | | * @since 3.1.1 |
| | | */ |
| | | public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator) { |
| | | if (isEmpty(string) || separator == null) { |
| | | return null == string ? null : string.toString(); |
| | | } |
| | | |
| | | final String str = string.toString(); |
| | | final String sep = separator.toString(); |
| | | if (sep.isEmpty()) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | final int pos = isLastSeparator ? str.lastIndexOf(sep) : str.indexOf(sep); |
| | | if (pos == INDEX_NOT_FOUND) { |
| | | return str; |
| | | } |
| | | return str.substring(0, pos); |
| | | } |
| | | |
| | | /** |
| | | * æªååéå符串ä¹åçå符串ï¼ä¸å
æ¬åéå符串<br> |
| | | * 妿ç»å®çå符串为空串ï¼nullæ""ï¼ï¼è¿ååå符串<br> |
| | | * 妿åéå符串为空串ï¼nullæ""ï¼ï¼åè¿å空串ï¼å¦æåéåç¬¦ä¸²æªæ¾å°ï¼è¿å空串 |
| | | * <p> |
| | | * æ åï¼ |
| | | * |
| | | * <pre> |
| | | * StringUtil.subAfter(null, *) = null |
| | | * StringUtil.subAfter("", *) = "" |
| | | * StringUtil.subAfter(*, null) = "" |
| | | * StringUtil.subAfter("abc", "a") = "bc" |
| | | * StringUtil.subAfter("abcba", "b") = "cba" |
| | | * StringUtil.subAfter("abc", "c") = "" |
| | | * StringUtil.subAfter("abc", "d") = "" |
| | | * StringUtil.subAfter("abc", "") = "abc" |
| | | * </pre> |
| | | * |
| | | * @param string è¢«æ¥æ¾çå符串 |
| | | * @param separator åéå符串ï¼ä¸å
æ¬ï¼ |
| | | * @param isLastSeparator æ¯å¦æ¥æ¾æåä¸ä¸ªåéå符串ï¼å¤æ¬¡åºç°åéå符串æ¶éåæåä¸ä¸ªï¼ï¼true为éåæåä¸ä¸ª |
| | | * @return åå²åçå符串 |
| | | * @since 3.1.1 |
| | | */ |
| | | public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator) { |
| | | if (isEmpty(string)) { |
| | | return null == string ? null : string.toString(); |
| | | } |
| | | if (separator == null) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | final String str = string.toString(); |
| | | final String sep = separator.toString(); |
| | | final int pos = isLastSeparator ? str.lastIndexOf(sep) : str.indexOf(sep); |
| | | if (pos == INDEX_NOT_FOUND) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | return str.substring(pos + separator.length()); |
| | | } |
| | | |
| | | /** |
| | | * æªåæå®å符串ä¸é´é¨åï¼ä¸å
æ¬æ è¯å符串<br> |
| | | * <p> |
| | | * æ åï¼ |
| | | * |
| | | * <pre> |
| | | * StringUtil.subBetween("wx[b]yz", "[", "]") = "b" |
| | | * StringUtil.subBetween(null, *, *) = null |
| | | * StringUtil.subBetween(*, null, *) = null |
| | | * StringUtil.subBetween(*, *, null) = null |
| | | * StringUtil.subBetween("", "", "") = "" |
| | | * StringUtil.subBetween("", "", "]") = null |
| | | * StringUtil.subBetween("", "[", "]") = null |
| | | * StringUtil.subBetween("yabcz", "", "") = "" |
| | | * StringUtil.subBetween("yabcz", "y", "z") = "abc" |
| | | * StringUtil.subBetween("yabczyabcz", "y", "z") = "abc" |
| | | * </pre> |
| | | * |
| | | * @param str 被åå²çå符串 |
| | | * @param before æªåå¼å§çå符串æ è¯ |
| | | * @param after æªåå°çå符串æ è¯ |
| | | * @return æªååçå符串 |
| | | * @since 3.1.1 |
| | | */ |
| | | public static String subBetween(CharSequence str, CharSequence before, CharSequence after) { |
| | | if (str == null || before == null || after == null) { |
| | | return null; |
| | | } |
| | | |
| | | final String str2 = str.toString(); |
| | | final String before2 = before.toString(); |
| | | final String after2 = after.toString(); |
| | | |
| | | final int start = str2.indexOf(before2); |
| | | if (start != INDEX_NOT_FOUND) { |
| | | final int end = str2.indexOf(after2, start + before2.length()); |
| | | if (end != INDEX_NOT_FOUND) { |
| | | return str2.substring(start + before2.length(), end); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * æªåæå®å符串ä¸é´é¨åï¼ä¸å
æ¬æ è¯å符串<br> |
| | | * <p> |
| | | * æ åï¼ |
| | | * |
| | | * <pre> |
| | | * StringUtil.subBetween(null, *) = null |
| | | * StringUtil.subBetween("", "") = "" |
| | | * StringUtil.subBetween("", "tag") = null |
| | | * StringUtil.subBetween("tagabctag", null) = null |
| | | * StringUtil.subBetween("tagabctag", "") = "" |
| | | * StringUtil.subBetween("tagabctag", "tag") = "abc" |
| | | * </pre> |
| | | * |
| | | * @param str 被åå²çå符串 |
| | | * @param beforeAndAfter æªåå¼å§åç»æçå符串æ è¯ |
| | | * @return æªååçå符串 |
| | | * @since 3.1.1 |
| | | */ |
| | | public static String subBetween(CharSequence str, CharSequence beforeAndAfter) { |
| | | return subBetween(str, beforeAndAfter, beforeAndAfter); |
| | | } |
| | | |
| | | /** |
| | | * 廿æå®åç¼ |
| | | * |
| | | * @param str å符串 |
| | | * @param prefix åç¼ |
| | | * @return åæåçå符串ï¼è¥åç¼ä¸æ¯ preffixï¼ è¿ååå符串 |
| | | */ |
| | | public static String removePrefix(CharSequence str, CharSequence prefix) { |
| | | if (isEmpty(str) || isEmpty(prefix)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | |
| | | final String str2 = str.toString(); |
| | | if (str2.startsWith(prefix.toString())) { |
| | | return subSuf(str2, prefix.length()); |
| | | } |
| | | return str2; |
| | | } |
| | | |
| | | /** |
| | | * 忽ç¥å¤§å°å廿æå®åç¼ |
| | | * |
| | | * @param str å符串 |
| | | * @param prefix åç¼ |
| | | * @return åæåçå符串ï¼è¥åç¼ä¸æ¯ prefixï¼ è¿ååå符串 |
| | | */ |
| | | public static String removePrefixIgnoreCase(CharSequence str, CharSequence prefix) { |
| | | if (isEmpty(str) || isEmpty(prefix)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | |
| | | final String str2 = str.toString(); |
| | | if (str2.toLowerCase().startsWith(prefix.toString().toLowerCase())) { |
| | | return subSuf(str2, prefix.length()); |
| | | } |
| | | return str2; |
| | | } |
| | | |
| | | /** |
| | | * 廿æå®åç¼ |
| | | * |
| | | * @param str å符串 |
| | | * @param suffix åç¼ |
| | | * @return åæåçå符串ï¼è¥åç¼ä¸æ¯ suffixï¼ è¿ååå符串 |
| | | */ |
| | | public static String removeSuffix(CharSequence str, CharSequence suffix) { |
| | | if (isEmpty(str) || isEmpty(suffix)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | |
| | | final String str2 = str.toString(); |
| | | if (str2.endsWith(suffix.toString())) { |
| | | return subPre(str2, str2.length() - suffix.length()); |
| | | } |
| | | return str2; |
| | | } |
| | | |
| | | /** |
| | | * 廿æå®åç¼ï¼å¹¶å°åé¦åæ¯ |
| | | * |
| | | * @param str å符串 |
| | | * @param suffix åç¼ |
| | | * @return åæåçå符串ï¼è¥åç¼ä¸æ¯ suffixï¼ è¿ååå符串 |
| | | */ |
| | | public static String removeSufAndLowerFirst(CharSequence str, CharSequence suffix) { |
| | | return firstCharToLower(removeSuffix(str, suffix)); |
| | | } |
| | | |
| | | /** |
| | | * 忽ç¥å¤§å°å廿æå®åç¼ |
| | | * |
| | | * @param str å符串 |
| | | * @param suffix åç¼ |
| | | * @return åæåçå符串ï¼è¥åç¼ä¸æ¯ suffixï¼ è¿ååå符串 |
| | | */ |
| | | public static String removeSuffixIgnoreCase(CharSequence str, CharSequence suffix) { |
| | | if (isEmpty(str) || isEmpty(suffix)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | |
| | | final String str2 = str.toString(); |
| | | if (str2.toLowerCase().endsWith(suffix.toString().toLowerCase())) { |
| | | return subPre(str2, str2.length() - suffix.length()); |
| | | } |
| | | return str2; |
| | | } |
| | | |
| | | /** |
| | | * é¦åæ¯åå°å |
| | | * |
| | | * @param str å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String firstCharToLower(String str) { |
| | | char firstChar = str.charAt(0); |
| | | if (firstChar >= CharPool.UPPER_A && firstChar <= CharPool.UPPER_Z) { |
| | | char[] arr = str.toCharArray(); |
| | | arr[0] += (CharPool.LOWER_A - CharPool.UPPER_A); |
| | | return new String(arr); |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * é¦åæ¯å大å |
| | | * |
| | | * @param str å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String firstCharToUpper(String str) { |
| | | char firstChar = str.charAt(0); |
| | | if (firstChar >= CharPool.LOWER_A && firstChar <= CharPool.LOWER_Z) { |
| | | char[] arr = str.toCharArray(); |
| | | arr[0] -= (CharPool.LOWER_A - CharPool.UPPER_A); |
| | | return new String(arr); |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * å岿å®ä½ç½®ä¹åé¨åçå符串 |
| | | * |
| | | * @param string å符串 |
| | | * @param toIndex åå²å°çä½ç½®ï¼ä¸å
æ¬ï¼ |
| | | * @return åå²åçå©ä½çååé¨åå符串 |
| | | */ |
| | | public static String subPre(CharSequence string, int toIndex) { |
| | | return sub(string, 0, toIndex); |
| | | } |
| | | |
| | | /** |
| | | * å岿å®ä½ç½®ä¹åé¨åçå符串 |
| | | * |
| | | * @param string å符串 |
| | | * @param fromIndex åå²å¼å§çä½ç½®ï¼å
æ¬ï¼ |
| | | * @return åå²ååå©ä½çååé¨åå符串 |
| | | */ |
| | | public static String subSuf(CharSequence string, int fromIndex) { |
| | | if (isEmpty(string)) { |
| | | return null; |
| | | } |
| | | return sub(string, fromIndex, string.length()); |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾æå®å符 |
| | | * |
| | | * @param str å符串 |
| | | * @param searchChar è¢«æ¥æ¾çå符 |
| | | * @return ä½ç½® |
| | | */ |
| | | public static int indexOf(final CharSequence str, char searchChar) { |
| | | return indexOf(str, searchChar, 0); |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾æå®å符 |
| | | * |
| | | * @param str å符串 |
| | | * @param searchChar è¢«æ¥æ¾çå符 |
| | | * @param start èµ·å§ä½ç½®ï¼å¦æå°äº0ï¼ä»0å¼å§æ¥æ¾ |
| | | * @return ä½ç½® |
| | | */ |
| | | public static int indexOf(final CharSequence str, char searchChar, int start) { |
| | | if (str instanceof String) { |
| | | return ((String) str).indexOf(searchChar, start); |
| | | } else { |
| | | return indexOf(str, searchChar, start, -1); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾æå®å符 |
| | | * |
| | | * @param str å符串 |
| | | * @param searchChar è¢«æ¥æ¾çå符 |
| | | * @param start èµ·å§ä½ç½®ï¼å¦æå°äº0ï¼ä»0å¼å§æ¥æ¾ |
| | | * @param end ç»æ¢ä½ç½®ï¼å¦æè¶
è¿str.length()åé»è®¤æ¥æ¾å°å符串æ«å°¾ |
| | | * @return ä½ç½® |
| | | */ |
| | | public static int indexOf(final CharSequence str, char searchChar, int start, int end) { |
| | | final int len = str.length(); |
| | | if (start < 0 || start > len) { |
| | | start = 0; |
| | | } |
| | | if (end > len || end < 0) { |
| | | end = len; |
| | | } |
| | | for (int i = start; i < end; i++) { |
| | | if (str.charAt(i) == searchChar) { |
| | | return i; |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾å符串ï¼å¿½ç¥å¤§å°å<br> |
| | | * |
| | | * <pre> |
| | | * StringUtil.indexOfIgnoreCase(null, *, *) = -1 |
| | | * StringUtil.indexOfIgnoreCase(*, null, *) = -1 |
| | | * StringUtil.indexOfIgnoreCase("", "", 0) = 0 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "A", 0) = 0 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", 0) = 2 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", 3) = 5 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", 9) = -1 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "", 2) = 2 |
| | | * StringUtil.indexOfIgnoreCase("abc", "", 9) = -1 |
| | | * </pre> |
| | | * |
| | | * @param str å符串 |
| | | * @param searchStr éè¦æ¥æ¾ä½ç½®çå符串 |
| | | * @return ä½ç½® |
| | | * @since 3.2.1 |
| | | */ |
| | | public static int indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr) { |
| | | return indexOfIgnoreCase(str, searchStr, 0); |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾å符串 |
| | | * |
| | | * <pre> |
| | | * StringUtil.indexOfIgnoreCase(null, *, *) = -1 |
| | | * StringUtil.indexOfIgnoreCase(*, null, *) = -1 |
| | | * StringUtil.indexOfIgnoreCase("", "", 0) = 0 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "A", 0) = 0 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", 0) = 2 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", 3) = 5 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", 9) = -1 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2 |
| | | * StringUtil.indexOfIgnoreCase("aabaabaa", "", 2) = 2 |
| | | * StringUtil.indexOfIgnoreCase("abc", "", 9) = -1 |
| | | * </pre> |
| | | * |
| | | * @param str å符串 |
| | | * @param searchStr éè¦æ¥æ¾ä½ç½®çå符串 |
| | | * @param fromIndex èµ·å§ä½ç½® |
| | | * @return ä½ç½® |
| | | * @since 3.2.1 |
| | | */ |
| | | public static int indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr, int fromIndex) { |
| | | return indexOf(str, searchStr, fromIndex, true); |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
å忥æ¾å符串 |
| | | * |
| | | * @param str å符串 |
| | | * @param searchStr éè¦æ¥æ¾ä½ç½®çå符串 |
| | | * @param fromIndex èµ·å§ä½ç½® |
| | | * @param ignoreCase æ¯å¦å¿½ç¥å¤§å°å |
| | | * @return ä½ç½® |
| | | * @since 3.2.1 |
| | | */ |
| | | public static int indexOf(final CharSequence str, CharSequence searchStr, int fromIndex, boolean ignoreCase) { |
| | | if (str == null || searchStr == null) { |
| | | return INDEX_NOT_FOUND; |
| | | } |
| | | if (fromIndex < 0) { |
| | | fromIndex = 0; |
| | | } |
| | | |
| | | final int endLimit = str.length() - searchStr.length() + 1; |
| | | if (fromIndex > endLimit) { |
| | | return INDEX_NOT_FOUND; |
| | | } |
| | | if (searchStr.length() == 0) { |
| | | return fromIndex; |
| | | } |
| | | |
| | | if (false == ignoreCase) { |
| | | // ä¸å¿½ç¥å¤§å°åè°ç¨JDKæ¹æ³ |
| | | return str.toString().indexOf(searchStr.toString(), fromIndex); |
| | | } |
| | | |
| | | for (int i = fromIndex; i < endLimit; i++) { |
| | | if (isSubEquals(str, i, searchStr, 0, searchStr.length(), true)) { |
| | | return i; |
| | | } |
| | | } |
| | | return INDEX_NOT_FOUND; |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾å符串ï¼å¿½ç¥å¤§å°å<br> |
| | | * |
| | | * @param str å符串 |
| | | * @param searchStr éè¦æ¥æ¾ä½ç½®çå符串 |
| | | * @return ä½ç½® |
| | | * @since 3.2.1 |
| | | */ |
| | | public static int lastIndexOfIgnoreCase(final CharSequence str, final CharSequence searchStr) { |
| | | return lastIndexOfIgnoreCase(str, searchStr, str.length()); |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾å符串ï¼å¿½ç¥å¤§å°å<br> |
| | | * |
| | | * @param str å符串 |
| | | * @param searchStr éè¦æ¥æ¾ä½ç½®çå符串 |
| | | * @param fromIndex èµ·å§ä½ç½®ï¼ä»åå¾åè®¡æ° |
| | | * @return ä½ç½® |
| | | * @since 3.2.1 |
| | | */ |
| | | public static int lastIndexOfIgnoreCase(final CharSequence str, final CharSequence searchStr, int fromIndex) { |
| | | return lastIndexOf(str, searchStr, fromIndex, true); |
| | | } |
| | | |
| | | /** |
| | | * æå®èå´å
æ¥æ¾å符串<br> |
| | | * |
| | | * @param str å符串 |
| | | * @param searchStr éè¦æ¥æ¾ä½ç½®çå符串 |
| | | * @param fromIndex èµ·å§ä½ç½®ï¼ä»åå¾åè®¡æ° |
| | | * @param ignoreCase æ¯å¦å¿½ç¥å¤§å°å |
| | | * @return ä½ç½® |
| | | * @since 3.2.1 |
| | | */ |
| | | public static int lastIndexOf(final CharSequence str, final CharSequence searchStr, int fromIndex, boolean ignoreCase) { |
| | | if (str == null || searchStr == null) { |
| | | return INDEX_NOT_FOUND; |
| | | } |
| | | if (fromIndex < 0) { |
| | | fromIndex = 0; |
| | | } |
| | | fromIndex = Math.min(fromIndex, str.length()); |
| | | |
| | | if (searchStr.length() == 0) { |
| | | return fromIndex; |
| | | } |
| | | |
| | | if (false == ignoreCase) { |
| | | // ä¸å¿½ç¥å¤§å°åè°ç¨JDKæ¹æ³ |
| | | return str.toString().lastIndexOf(searchStr.toString(), fromIndex); |
| | | } |
| | | |
| | | for (int i = fromIndex; i > 0; i--) { |
| | | if (isSubEquals(str, i, searchStr, 0, searchStr.length(), true)) { |
| | | return i; |
| | | } |
| | | } |
| | | return INDEX_NOT_FOUND; |
| | | } |
| | | |
| | | /** |
| | | * è¿åå符串 searchStr å¨å符串 str ä¸ç¬¬ ordinal 次åºç°çä½ç½®ã<br> |
| | | * æ¤æ¹æ³æ¥èªï¼Apache-Commons-Lang |
| | | * <p> |
| | | * æ åï¼*代表任æå符ï¼ï¼ |
| | | * |
| | | * <pre> |
| | | * StringUtil.ordinalIndexOf(null, *, *) = -1 |
| | | * StringUtil.ordinalIndexOf(*, null, *) = -1 |
| | | * StringUtil.ordinalIndexOf("", "", *) = 0 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "a", 1) = 0 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "a", 2) = 1 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "b", 1) = 2 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "b", 2) = 5 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "ab", 1) = 1 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "ab", 2) = 4 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "", 1) = 0 |
| | | * StringUtil.ordinalIndexOf("aabaabaa", "", 2) = 0 |
| | | * </pre> |
| | | * |
| | | * @param str è¢«æ£æ¥çå符串ï¼å¯ä»¥ä¸ºnull |
| | | * @param searchStr è¢«æ¥æ¾çå符串ï¼å¯ä»¥ä¸ºnull |
| | | * @param ordinal ç¬¬å æ¬¡åºç°çä½ç½® |
| | | * @return æ¥æ¾å°çä½ç½® |
| | | * @since 3.2.3 |
| | | */ |
| | | public static int ordinalIndexOf(String str, String searchStr, int ordinal) { |
| | | if (str == null || searchStr == null || ordinal <= 0) { |
| | | return INDEX_NOT_FOUND; |
| | | } |
| | | if (searchStr.length() == 0) { |
| | | return 0; |
| | | } |
| | | int found = 0; |
| | | int index = INDEX_NOT_FOUND; |
| | | do { |
| | | index = str.indexOf(searchStr, index + 1); |
| | | if (index < 0) { |
| | | return index; |
| | | } |
| | | found++; |
| | | } while (found < ordinal); |
| | | return index; |
| | | } |
| | | |
| | | /** |
| | | * æªå两个å符串çä¸åé¨åï¼é¿åº¦ä¸è´ï¼ï¼å¤ææªåçå串æ¯å¦ç¸å<br> |
| | | * ä»»æä¸ä¸ªå符串为nullè¿åfalse |
| | | * |
| | | * @param str1 第ä¸ä¸ªå符串 |
| | | * @param start1 第ä¸ä¸ªå符串å¼å§çä½ç½® |
| | | * @param str2 第äºä¸ªå符串 |
| | | * @param start2 第äºä¸ªå符串å¼å§çä½ç½® |
| | | * @param length æªåé¿åº¦ |
| | | * @param ignoreCase æ¯å¦å¿½ç¥å¤§å°å |
| | | * @return å串æ¯å¦ç¸å |
| | | * @since 3.2.1 |
| | | */ |
| | | public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase) { |
| | | if (null == str1 || null == str2) { |
| | | return false; |
| | | } |
| | | |
| | | return str1.toString().regionMatches(ignoreCase, start1, str2.toString(), start2, length); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾ä¸¤ä¸ªå符串ï¼å¤§å°åææï¼ã |
| | | * |
| | | * <pre> |
| | | * equalsIgnoreCase(null, null) = true |
| | | * equalsIgnoreCase(null, "abc") = false |
| | | * equalsIgnoreCase("abc", null) = false |
| | | * equalsIgnoreCase("abc", "abc") = true |
| | | * equalsIgnoreCase("abc", "ABC") = true |
| | | * </pre> |
| | | * |
| | | * @param str1 è¦æ¯è¾çå符串1 |
| | | * @param str2 è¦æ¯è¾çå符串2 |
| | | * @return å¦æä¸¤ä¸ªå符串ç¸åï¼æè
齿¯<code>null</code>ï¼åè¿å<code>true</code> |
| | | */ |
| | | public static boolean equals(CharSequence str1, CharSequence str2) { |
| | | return equals(str1, str2, false); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾ä¸¤ä¸ªå符串ï¼å¤§å°å䏿æï¼ã |
| | | * |
| | | * <pre> |
| | | * equalsIgnoreCase(null, null) = true |
| | | * equalsIgnoreCase(null, "abc") = false |
| | | * equalsIgnoreCase("abc", null) = false |
| | | * equalsIgnoreCase("abc", "abc") = true |
| | | * equalsIgnoreCase("abc", "ABC") = true |
| | | * </pre> |
| | | * |
| | | * @param str1 è¦æ¯è¾çå符串1 |
| | | * @param str2 è¦æ¯è¾çå符串2 |
| | | * @return å¦æä¸¤ä¸ªå符串ç¸åï¼æè
齿¯<code>null</code>ï¼åè¿å<code>true</code> |
| | | */ |
| | | public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) { |
| | | return equals(str1, str2, true); |
| | | } |
| | | |
| | | /** |
| | | * æ¯è¾ä¸¤ä¸ªå符串æ¯å¦ç¸çã |
| | | * |
| | | * @param str1 è¦æ¯è¾çå符串1 |
| | | * @param str2 è¦æ¯è¾çå符串2 |
| | | * @param ignoreCase æ¯å¦å¿½ç¥å¤§å°å |
| | | * @return å¦æä¸¤ä¸ªå符串ç¸åï¼æè
齿¯<code>null</code>ï¼åè¿å<code>true</code> |
| | | * @since 3.2.0 |
| | | */ |
| | | public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase) { |
| | | if (null == str1) { |
| | | // åªæä¸¤ä¸ªé½ä¸ºnullæå¤æç¸ç |
| | | return str2 == null; |
| | | } |
| | | if (null == str2) { |
| | | // å符串2空ï¼å符串1é空ï¼ç´æ¥false |
| | | return false; |
| | | } |
| | | |
| | | if (ignoreCase) { |
| | | return str1.toString().equalsIgnoreCase(str2.toString()); |
| | | } else { |
| | | return str1.equals(str2); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å建StringBuilder对象 |
| | | * |
| | | * @return {String}Builder对象 |
| | | */ |
| | | public static StringBuilder builder() { |
| | | return new StringBuilder(); |
| | | } |
| | | |
| | | /** |
| | | * å建StringBuilder对象 |
| | | * |
| | | * @param capacity åå§å¤§å° |
| | | * @return {String}Builder对象 |
| | | */ |
| | | public static StringBuilder builder(int capacity) { |
| | | return new StringBuilder(capacity); |
| | | } |
| | | |
| | | /** |
| | | * å建StringBuilder对象 |
| | | * |
| | | * @param strs åå§å符串å表 |
| | | * @return {String}Builder对象 |
| | | */ |
| | | public static StringBuilder builder(CharSequence... strs) { |
| | | final StringBuilder sb = new StringBuilder(); |
| | | for (CharSequence str : strs) { |
| | | sb.append(str); |
| | | } |
| | | return sb; |
| | | } |
| | | |
| | | /** |
| | | * å建StringBuilder对象 |
| | | * |
| | | * @param sb åå§StringBuilder |
| | | * @param strs åå§å符串å表 |
| | | * @return {String}Builder对象 |
| | | */ |
| | | public static StringBuilder appendBuilder(StringBuilder sb, CharSequence... strs) { |
| | | for (CharSequence str : strs) { |
| | | sb.append(str); |
| | | } |
| | | return sb; |
| | | } |
| | | |
| | | /** |
| | | * è·å¾StringReader |
| | | * |
| | | * @param str å符串 |
| | | * @return {String}Reader |
| | | */ |
| | | public static StringReader getReader(CharSequence str) { |
| | | if (null == str) { |
| | | return null; |
| | | } |
| | | return new StringReader(str.toString()); |
| | | } |
| | | |
| | | /** |
| | | * è·å¾StringWriter |
| | | * |
| | | * @return {String}Writer |
| | | */ |
| | | public static StringWriter getWriter() { |
| | | return new StringWriter(); |
| | | } |
| | | |
| | | /** |
| | | * ç»è®¡æå®å
容ä¸å
嫿å®åç¬¦ä¸²çæ°é<br> |
| | | * åæ°ä¸º {@code null} æè
"" è¿å {@code 0}. |
| | | * |
| | | * <pre> |
| | | * StringUtil.count(null, *) = 0 |
| | | * StringUtil.count("", *) = 0 |
| | | * StringUtil.count("abba", null) = 0 |
| | | * StringUtil.count("abba", "") = 0 |
| | | * StringUtil.count("abba", "a") = 2 |
| | | * StringUtil.count("abba", "ab") = 1 |
| | | * StringUtil.count("abba", "xxx") = 0 |
| | | * </pre> |
| | | * |
| | | * @param content è¢«æ¥æ¾çå符串 |
| | | * @param strForSearch éè¦æ¥æ¾çå符串 |
| | | * @return æ¥æ¾å°çä¸ªæ° |
| | | */ |
| | | public static int count(CharSequence content, CharSequence strForSearch) { |
| | | if (Func.hasEmpty(content, strForSearch) || strForSearch.length() > content.length()) { |
| | | return 0; |
| | | } |
| | | |
| | | int count = 0; |
| | | int idx = 0; |
| | | final String content2 = content.toString(); |
| | | final String strForSearch2 = strForSearch.toString(); |
| | | while ((idx = content2.indexOf(strForSearch2, idx)) > -1) { |
| | | count++; |
| | | idx += strForSearch.length(); |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | | * ç»è®¡æå®å
容ä¸å
嫿å®åç¬¦çæ°é |
| | | * |
| | | * @param content å
容 |
| | | * @param charForSearch 被ç»è®¡çå符 |
| | | * @return å
嫿°é |
| | | */ |
| | | public static int count(CharSequence content, char charForSearch) { |
| | | int count = 0; |
| | | if (isEmpty(content)) { |
| | | return 0; |
| | | } |
| | | int contentLength = content.length(); |
| | | for (int i = 0; i < contentLength; i++) { |
| | | if (charForSearch == content.charAt(i)) { |
| | | count++; |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | | * ä¸å线转驼峰 |
| | | * |
| | | * @param para å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String underlineToHump(String para) { |
| | | if (isBlank(para)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | StringBuilder result = new StringBuilder(); |
| | | String[] a = para.split("_"); |
| | | for (String s : a) { |
| | | if (result.length() == 0) { |
| | | result.append(s.toLowerCase()); |
| | | } else { |
| | | result.append(s.substring(0, 1).toUpperCase()); |
| | | result.append(s.substring(1).toLowerCase()); |
| | | } |
| | | } |
| | | return result.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰转ä¸å线 |
| | | * |
| | | * @param para å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String humpToUnderline(String para) { |
| | | if (isBlank(para)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | para = firstCharToLower(para); |
| | | StringBuilder sb = new StringBuilder(para); |
| | | int temp = 0; |
| | | for (int i = 0; i < para.length(); i++) { |
| | | if (Character.isUpperCase(para.charAt(i))) { |
| | | sb.insert(i + temp, "_"); |
| | | temp += 1; |
| | | } |
| | | } |
| | | return sb.toString().toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | | * 横线转驼峰 |
| | | * |
| | | * @param para å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String lineToHump(String para) { |
| | | if (isBlank(para)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | StringBuilder result = new StringBuilder(); |
| | | String[] a = para.split("-"); |
| | | for (String s : a) { |
| | | if (result.length() == 0) { |
| | | result.append(s.toLowerCase()); |
| | | } else { |
| | | result.append(s.substring(0, 1).toUpperCase()); |
| | | result.append(s.substring(1).toLowerCase()); |
| | | } |
| | | } |
| | | return result.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰转横线 |
| | | * |
| | | * @param para å符串 |
| | | * @return {String} |
| | | */ |
| | | public static String humpToLine(String para) { |
| | | if (isBlank(para)) { |
| | | return StringPool.EMPTY; |
| | | } |
| | | para = firstCharToLower(para); |
| | | StringBuilder sb = new StringBuilder(para); |
| | | int temp = 0; |
| | | for (int i = 0; i < para.length(); i++) { |
| | | if (Character.isUpperCase(para.charAt(i))) { |
| | | sb.insert(i + temp, "-"); |
| | | temp += 1; |
| | | } |
| | | } |
| | | return sb.toString().toLowerCase(); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.vci.web.util.jackson; |
| | | |
| | | import com.fasterxml.jackson.databind.module.SimpleModule; |
| | | import com.fasterxml.jackson.datatype.jsr310.PackageVersion; |
| | | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; |
| | | import com.vci.web.util.DateTimeUtil; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | |
| | | /** |
| | | * java 8 æ¶é´é»è®¤åºåå |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class BladeJavaTimeModule extends SimpleModule { |
| | | public static final BladeJavaTimeModule INSTANCE = new BladeJavaTimeModule(); |
| | | |
| | | public BladeJavaTimeModule() { |
| | | super(PackageVersion.VERSION); |
| | | this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeUtil.DATETIME_FORMAT)); |
| | | this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeUtil.DATE_FORMAT)); |
| | | this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeUtil.TIME_FORMAT)); |
| | | this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeUtil.DATETIME_FORMAT)); |
| | | this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeUtil.DATE_FORMAT)); |
| | | this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeUtil.TIME_FORMAT)); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package com.vci.web.util.jackson; |
| | | |
| | | import com.fasterxml.jackson.core.JsonParser; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.core.TreeNode; |
| | | import com.fasterxml.jackson.core.json.JsonReadFeature; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.*; |
| | | import com.fasterxml.jackson.databind.type.CollectionLikeType; |
| | | import com.fasterxml.jackson.databind.type.MapType; |
| | | import com.vci.web.util.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.ZoneId; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Jacksonå·¥å
·ç±» |
| | | * |
| | | * @author Chill |
| | | */ |
| | | @Slf4j |
| | | public class JsonUtil { |
| | | |
| | | /** |
| | | * å°å¯¹è±¡åºååæjsonå符串 |
| | | * |
| | | * @param value javaBean |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static <T> String toJson(T value) { |
| | | try { |
| | | return getInstance().writeValueAsString(value); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡åºååæ json byte æ°ç» |
| | | * |
| | | * @param object javaBean |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static byte[] toJsonAsBytes(Object object) { |
| | | try { |
| | | return getInstance().writeValueAsBytes(object); |
| | | } catch (JsonProcessingException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param content content |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T parse(String content, Class<T> valueType) { |
| | | try { |
| | | return getInstance().readValue(content, valueType); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param content content |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T parse(String content, TypeReference<T> typeReference) { |
| | | try { |
| | | return getInstance().readValue(content, typeReference); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°json byte æ°ç»ååºååæå¯¹è±¡ |
| | | * |
| | | * @param bytes json bytes |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T parse(byte[] bytes, Class<T> valueType) { |
| | | try { |
| | | return getInstance().readValue(bytes, valueType); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param bytes bytes |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T parse(byte[] bytes, TypeReference<T> typeReference) { |
| | | try { |
| | | return getInstance().readValue(bytes, typeReference); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param in InputStream |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T parse(InputStream in, Class<T> valueType) { |
| | | try { |
| | | return getInstance().readValue(in, valueType); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param in InputStream |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | public static <T> T parse(InputStream in, TypeReference<T> typeReference) { |
| | | try { |
| | | return getInstance().readValue(in, typeReference); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæList对象 |
| | | * |
| | | * @param content content |
| | | * @param valueTypeRef class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return List<T> |
| | | */ |
| | | public static <T> List<T> parseArray(String content, Class<T> valueTypeRef) { |
| | | try { |
| | | |
| | | if (!StringUtil.startsWithIgnoreCase(content, StringPool.LEFT_SQ_BRACKET)) { |
| | | content = StringPool.LEFT_SQ_BRACKET + content + StringPool.RIGHT_SQ_BRACKET; |
| | | } |
| | | |
| | | List<Map<String, Object>> list = getInstance().readValue(content, new TypeReference<List<Map<String, Object>>>() { |
| | | }); |
| | | |
| | | List<T> result = new ArrayList<>(); |
| | | for (Map<String, Object> map : list) { |
| | | result.add(toPojo(map, valueTypeRef)); |
| | | } |
| | | return result; |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param jsonString jsonString |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(String jsonString) { |
| | | Objects.requireNonNull(jsonString, "jsonString is null"); |
| | | try { |
| | | return getInstance().readTree(jsonString); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param in InputStream |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(InputStream in) { |
| | | Objects.requireNonNull(in, "InputStream in is null"); |
| | | try { |
| | | return getInstance().readTree(in); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param content content |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(byte[] content) { |
| | | Objects.requireNonNull(content, "byte[] content is null"); |
| | | try { |
| | | return getInstance().readTree(content); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonå符串转æ JsonNode |
| | | * |
| | | * @param jsonParser JsonParser |
| | | * @return jsonString jsonå符串 |
| | | */ |
| | | public static JsonNode readTree(JsonParser jsonParser) { |
| | | Objects.requireNonNull(jsonParser, "jsonParser is null"); |
| | | try { |
| | | return getInstance().readTree(jsonParser); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å°json byte æ°ç»ååºååæå¯¹è±¡ |
| | | * |
| | | * @param content json bytes |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | @Nullable |
| | | public static <T> T readValue(@Nullable byte[] content, Class<T> valueType) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, valueType); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param jsonString jsonString |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | @Nullable |
| | | public static <T> T readValue(@Nullable String jsonString, Class<T> valueType) { |
| | | if (StringUtil.isBlank(jsonString)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return getInstance().readValue(jsonString, valueType); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param in InputStream |
| | | * @param valueType class |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | @Nullable |
| | | public static <T> T readValue(@Nullable InputStream in, Class<T> valueType) { |
| | | if (in == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return getInstance().readValue(in, valueType); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param content bytes |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | @Nullable |
| | | public static <T> T readValue(@Nullable byte[] content, TypeReference<T> typeReference) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, typeReference); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param jsonString jsonString |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | @Nullable |
| | | public static <T> T readValue(@Nullable String jsonString, TypeReference<T> typeReference) { |
| | | if (StringUtil.isBlank(jsonString)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return getInstance().readValue(jsonString, typeReference); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°jsonååºååæå¯¹è±¡ |
| | | * |
| | | * @param in InputStream |
| | | * @param typeReference æ³åç±»å |
| | | * @param <T> T æ³åæ è®° |
| | | * @return Bean |
| | | */ |
| | | @Nullable |
| | | public static <T> T readValue(@Nullable InputStream in, TypeReference<T> typeReference) { |
| | | if (in == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return getInstance().readValue(in, typeReference); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°è£
map type |
| | | * |
| | | * @param keyClass key ç±»å |
| | | * @param valueClass value ç±»å |
| | | * @return MapType |
| | | */ |
| | | public static MapType getMapType(Class<?> keyClass, Class<?> valueClass) { |
| | | return getInstance().getTypeFactory().constructMapType(Map.class, keyClass, valueClass); |
| | | } |
| | | |
| | | /** |
| | | * å°è£
map type |
| | | * |
| | | * @param elementClass éåå¼ç±»å |
| | | * @return CollectionLikeType |
| | | */ |
| | | public static CollectionLikeType getListType(Class<?> elementClass) { |
| | | return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass); |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content bytes |
| | | * @param elementClass elementClass |
| | | * @param <T> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <T> List<T> readList(@Nullable byte[] content, Class<T> elementClass) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, getListType(elementClass)); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content InputStream |
| | | * @param elementClass elementClass |
| | | * @param <T> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <T> List<T> readList(@Nullable InputStream content, Class<T> elementClass) { |
| | | if (content == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, getListType(elementClass)); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content bytes |
| | | * @param elementClass elementClass |
| | | * @param <T> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <T> List<T> readList(@Nullable String content, Class<T> elementClass) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, getListType(elementClass)); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content bytes |
| | | * @param keyClass keyç±»å |
| | | * @param valueClass å¼ç±»å |
| | | * @param <K> æ³å |
| | | * @param <V> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <K, V> Map<K, V> readMap(@Nullable byte[] content, Class<?> keyClass, Class<?> valueClass) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, getMapType(keyClass, valueClass)); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content InputStream |
| | | * @param keyClass keyç±»å |
| | | * @param valueClass å¼ç±»å |
| | | * @param <K> æ³å |
| | | * @param <V> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <K, V> Map<K, V> readMap(@Nullable InputStream content, Class<?> keyClass, Class<?> valueClass) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, getMapType(keyClass, valueClass)); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content bytes |
| | | * @param keyClass keyç±»å |
| | | * @param valueClass å¼ç±»å |
| | | * @param <K> æ³å |
| | | * @param <V> æ³å |
| | | * @return éå |
| | | */ |
| | | public static <K, V> Map<K, V> readMap(@Nullable String content, Class<?> keyClass, Class<?> valueClass) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, getMapType(keyClass, valueClass)); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content bytes |
| | | * @return éå |
| | | */ |
| | | public static Map<String, Object> readMap(@Nullable String content) { |
| | | return readMap(content, String.class, Object.class); |
| | | } |
| | | |
| | | /** |
| | | * 读åéå |
| | | * |
| | | * @param content bytes |
| | | * @return éå |
| | | */ |
| | | public static List<Map<String, Object>> readListMap(@Nullable String content) { |
| | | if (ObjectUtil.isEmpty(content)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | try { |
| | | return getInstance().readValue(content, new TypeReference<List<Map<String, Object>>>() { |
| | | }); |
| | | } catch (IOException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * jackson çç±»åè½¬æ¢ |
| | | * |
| | | * @param fromValue æ¥æºå¯¹è±¡ |
| | | * @param toValueType 转æ¢çç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return 转æ¢ç»æ |
| | | */ |
| | | public static <T> T convertValue(Object fromValue, Class<T> toValueType) { |
| | | return getInstance().convertValue(fromValue, toValueType); |
| | | } |
| | | |
| | | /** |
| | | * jackson çç±»åè½¬æ¢ |
| | | * |
| | | * @param fromValue æ¥æºå¯¹è±¡ |
| | | * @param toValueType 转æ¢çç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return 转æ¢ç»æ |
| | | */ |
| | | public static <T> T convertValue(Object fromValue, JavaType toValueType) { |
| | | return getInstance().convertValue(fromValue, toValueType); |
| | | } |
| | | |
| | | /** |
| | | * jackson çç±»åè½¬æ¢ |
| | | * |
| | | * @param fromValue æ¥æºå¯¹è±¡ |
| | | * @param toValueTypeRef æ³åç±»å |
| | | * @param <T> æ³åæ è®° |
| | | * @return 转æ¢ç»æ |
| | | */ |
| | | public static <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef) { |
| | | return getInstance().convertValue(fromValue, toValueTypeRef); |
| | | } |
| | | |
| | | /** |
| | | * tree 转对象 |
| | | * |
| | | * @param treeNode TreeNode |
| | | * @param valueType valueType |
| | | * @param <T> æ³åæ è®° |
| | | * @return 转æ¢ç»æ |
| | | */ |
| | | public static <T> T treeToValue(TreeNode treeNode, Class<T> valueType) { |
| | | try { |
| | | return getInstance().treeToValue(treeNode, valueType); |
| | | } catch (JsonProcessingException e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 对象转为 json node |
| | | * |
| | | * @param value 对象 |
| | | * @return JsonNode |
| | | */ |
| | | public static JsonNode valueToTree(@Nullable Object value) { |
| | | return getInstance().valueToTree(value); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦å¯ä»¥åºåå |
| | | * |
| | | * @param value 对象 |
| | | * @return æ¯å¦å¯ä»¥åºåå |
| | | */ |
| | | public static boolean canSerialize(@Nullable Object value) { |
| | | if (value == null) { |
| | | return true; |
| | | } |
| | | return getInstance().canSerialize(value.getClass()); |
| | | } |
| | | |
| | | public static Map<String, Object> toMap(String content) { |
| | | try { |
| | | return getInstance().readValue(content, Map.class); |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static <T> Map<String, T> toMap(String content, Class<T> valueTypeRef) { |
| | | try { |
| | | Map<String, Map<String, Object>> map = getInstance().readValue(content, new TypeReference<Map<String, Map<String, Object>>>() { |
| | | }); |
| | | Map<String, T> result = new HashMap<>(16); |
| | | for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) { |
| | | result.put(entry.getKey(), toPojo(entry.getValue(), valueTypeRef)); |
| | | } |
| | | return result; |
| | | } catch (IOException e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static <T> T toPojo(Map fromValue, Class<T> toValueType) { |
| | | return getInstance().convertValue(fromValue, toValueType); |
| | | } |
| | | |
| | | public static ObjectMapper getInstance() { |
| | | return JacksonHolder.INSTANCE; |
| | | } |
| | | |
| | | private static class JacksonHolder { |
| | | private static final ObjectMapper INSTANCE = new JacksonObjectMapper(); |
| | | } |
| | | |
| | | private static class JacksonObjectMapper extends ObjectMapper { |
| | | private static final long serialVersionUID = 4288193147502386170L; |
| | | |
| | | private static final Locale CHINA = Locale.CHINA; |
| | | |
| | | public JacksonObjectMapper(ObjectMapper src) { |
| | | super(src); |
| | | } |
| | | |
| | | public JacksonObjectMapper() { |
| | | super(); |
| | | //设置å°ç¹ä¸ºä¸å½ |
| | | super.setLocale(CHINA); |
| | | //廿é»è®¤çæ¶é´æ³æ ¼å¼ |
| | | super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
| | | //设置为ä¸å½ä¸æµ·æ¶åº |
| | | super.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); |
| | | //åºååæ¶ï¼æ¥æçç»ä¸æ ¼å¼ |
| | | super.setDateFormat(new SimpleDateFormat(DateUtil.PATTERN_DATETIME, Locale.CHINA)); |
| | | // åå¼å· |
| | | super.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); |
| | | // å
许JSONå符串å
å«éå¼å·æ§å¶å符ï¼å¼å°äº32çASCIIå符ï¼å
å«å¶è¡¨ç¬¦åæ¢è¡ç¬¦ï¼ |
| | | super.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true); |
| | | super.configure(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER.mappedFeature(), true); |
| | | super.findAndRegisterModules(); |
| | | //失败å¤ç |
| | | super.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
| | | super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| | | //åå¼å·å¤ç |
| | | super.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), true); |
| | | //ååºååæ¶ï¼å±æ§ä¸åå¨çå
¼å®¹å¤çs |
| | | super.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| | | //æ¥ææ ¼å¼å |
| | | super.registerModule(new BladeJavaTimeModule()); |
| | | super.findAndRegisterModules(); |
| | | } |
| | | |
| | | @Override |
| | | public ObjectMapper copy() { |
| | | return new JacksonObjectMapper(this); |
| | | } |
| | | } |
| | | |
| | | } |