[英]Validating a string for a positive number, null/empty string
我有一個方法,我接受一個String clientid
並具有以下要求:
clientid
可以是大於零的正數。 但如果它是負數或零,則拋出IllegalArgumentException
並帶有消息。 clientid
不能為null
或空字符串。 但如果是,則拋出IllegalArgumentException
並帶有消息。 clientid
也可以是正常的字符串。 例如 - 它可以是abcdefgh
或任何其他字符串。 import static com.google.common.base.Preconditions.checkArgument;
public Builder setClientId(String clientid) {
checkArgument(!Strings.isNullOrEmpty(clientid), "clientid cannot not be null or an empty string, found '%s'.",
clientid);
final Long id = Longs.tryParse(clientid);
if (id != null) {
checkArgument(id.longValue() > 0, "clientid must not be negative or zero, found '%s'.", clientid);
}
this.clientid = clientid;
return this;
}
這段代碼工作正常。 現在的問題是,我不能使用比版本11更大的番石榴庫。如果我確實使用它,那么它final Long id = Longs.tryParse(clientid);
我們使用這個庫的客戶帶來問題,所以總之我正在尋找替代這一行的final Long id = Longs.tryParse(clientid);
不使用番石榴或可能使用較舊的番石榴版本11.由於在Guava 14或更高版本中添加了Longs.tryParse
方法。
最好的方法是什么? 我們可以從Apache Commons使用的任何東西?
我建議通過重新定位類,使用Apache Maven Shade插件重新打包Guava。 簡而言之,您可以將Guava中的包重命名為com.example.mypackage.com.google.common
,然后在項目中使用它們。
通過這樣做,您可以使用最新版本的Guava,而不會導致客戶的依賴性沖突。
這是一個基於jersey-repackaged-guava
POM示例:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.mypackage</groupId>
<artifactId>repackged-guava-example</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<inherited>true</inherited>
<configuration>
<minimizeJar>false</minimizeJar>
<createSourcesJar>true</createSourcesJar>
<shadeSourcesContent>true</shadeSourcesContent>
<artifactSet>
<includes>
<include>com.google.guava:guava:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.google.common</pattern>
<shadedPattern>${repackaged.prefix}.com.google.common</shadedPattern>
</relocation>
<relocation>
<pattern>com.google.thirdparty</pattern>
<shadedPattern>${repackaged.prefix}.com.google.thirdparty</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
<optional>true</optional>
</dependency>
</dependencies>
<properties>
<repackaged.prefix>com.example.mypackage</repackaged.prefix>
</properties>
</project>
然后依賴repackged-guava-example
並更改你的導入:
import com.example.mypackage.com.google.common.primitives.Longs;
請注意,如果在具有IDE的多模塊項目中使用此項,則需要將IDE配置為忽略重新打包模塊的目標類(例如,請參閱https://youtrack.jetbrains.com/issue/IDEA-126596 )。 否則,IDE將使用原始類與原始包名稱而不是重新包裝的類。
有點難看,但解決了你的問題,因為它不需要任何庫
try {
final long id = Long.parseLong(clientId);
checkArgument(id > 0, "clientid must not be negative or zero, found '%s'.", clientid);
} catch (NumberFormatException e) {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.