简体   繁体   English

验证字符串的正数,空/空字符串

[英]Validating a string for a positive number, null/empty string

I have a method in which I am accepting a String clientid and that has below requirements: 我有一个方法,我接受一个String clientid并具有以下要求:

  • clientid can be a positive number greater than zero. clientid可以是大于零的正数。 But if it is negative number or zero, then throw IllegalArgumentException with a message. 但如果它是负数或零,则抛出IllegalArgumentException并带有消息。
  • clientid cannot be a null or empty string. clientid不能为null或空字符串。 But if it is, then throw IllegalArgumentException with a message. 但如果是,则抛出IllegalArgumentException并带有消息。
  • clientid can be a normal string as well. clientid也可以是正常的字符串。 For example - it can be abcdefgh or any other string. 例如 - 它可以是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;
}

This code works fine. 这段代码工作正常。 Now the problem is, I cannot use guava library greater than version 11. If I do use it, then it cause problem to our customer who use this library so in short I am looking for substitute for this line final Long id = Longs.tryParse(clientid); 现在的问题是,我不能使用比版本11更大的番石榴库。如果我确实使用它,那么它final Long id = Longs.tryParse(clientid);我们使用这个库的客户带来问题,所以总之我正在寻找替代这一行的final Long id = Longs.tryParse(clientid); without using guava or may be with older guava version 11. Since Longs.tryParse method was added in Guava 14 or later. 不使用番石榴或可能使用较旧的番石榴版本11.由于在Guava 14或更高版本中添加了Longs.tryParse方法。

What is the best way to do that? 最好的方法是什么? Anything we can use from Apache Commons? 我们可以从Apache Commons使用的任何东西?

I recommend repackaging Guava using the Apache Maven Shade Plugin by relocating classes . 我建议通过重新定位类,使用Apache Maven Shade插件重新打包Guava。 In short, you can rename the packages from Guava to something like com.example.mypackage.com.google.common and then use those in your project. 简而言之,您可以将Guava中的包重命名为com.example.mypackage.com.google.common ,然后在项目中使用它们。

By doing so you can use the latest version of Guava without causing dependency conflicts for your customer. 通过这样做,您可以使用最新版本的Guava,而不会导致客户的依赖性冲突。

Here is an example POM based on jersey-repackaged-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>

Then depend on repackged-guava-example and change your import: 然后依赖repackged-guava-example并更改你的导入:

import com.example.mypackage.com.google.common.primitives.Longs;

Note that if you use this in a multi-module project with an IDE you'll want to configure your IDE to ignore your repackaged module's target classes (eg see https://youtrack.jetbrains.com/issue/IDEA-126596 ). 请注意,如果在具有IDE的多模块项目中使用此项,则需要将IDE配置为忽略重新打包模块的目标类(例如,请参阅https://youtrack.jetbrains.com/issue/IDEA-126596 )。 Otherwise your IDE will use the original classes with the original package names instead of the repackaged ones. 否则,IDE将使用原始类与原始包名称而不是重新包装的类。

A bit ugly, but solves your problem, since it doesn't require any library 有点难看,但解决了你的问题,因为它不需要任何库

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM