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

时间:2016-02-09 19:53:43

标签: java guava apache-commons preconditions

我有一个方法,我接受字符串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);而不使用番石榴或可能与较旧的番石榴版本11.由于在Guava 14或更高版本中添加了Longs.tryParse方法。

最好的方法是什么?我们可以从Apache Commons使用的任何东西吗?

2 个答案:

答案 0 :(得分:2)

我建议使用Apache Maven Shade Plugin relocating classes重新包装番石榴。简而言之,您可以将包中的包重命名为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将使用原始类与原始包名称而不是重新包装的类。

答案 1 :(得分:1)

有点难看,但解决了你的问题,因为它不需要任何库

try {
    final long id = Long.parseLong(clientId);
    checkArgument(id > 0, "clientid must not be negative or zero, found '%s'.", clientid);
} catch (NumberFormatException e) {}