【问题标题】:Validating a string for a positive number, null/empty string验证字符串是否为正数、空/空字符串
【发布时间】:2016-02-09 19:53:43
【问题描述】:

我有一个接受字符串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 的 guava 库。如果我确实使用它,那么它会给我们使用这个库的客户带来问题,所以简而言之,我正在寻找替代这条线 final Long id = Longs.tryParse(clientid); 而不使用 guava 或可能与较旧的 guava 版本 11 一起使用。由于 Longs.tryParse 方法是在 Guava 14 或更高版本中添加的。

最好的方法是什么?我们可以从 Apache Commons 使用什么?

【问题讨论】:

  • 为什么不直接使用 try{ }catch(NumberformatException ne){}
  • 那看起来很难看..想避免这种情况..还有其他方法吗?
  • 好吧,公平地说,之前也是 4 行 :)
  • 这里是给猪涂口红的问题,你选哪个色系。
  • 是的,就是这样。我想摆脱 catch 块,所以这就是我之前使用 Longs.tryParse 的原因,但如果除了我之前知道的没有其他方法,那么我将使用它。

标签: java guava apache-commons preconditions


【解决方案1】:

我建议使用 Apache Maven Shade Plugin by relocating classes 重新包装 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 将使用带有原始包名称的原始类,而不是重新打包的类。

【讨论】:

  • 我听说过这个解决方案。你能告诉我这将如何工作吗?你的意思是我只是从你给我的链接中添加了那个 maven-shade 插件?然后我将如何使用它?这条线将保持原样final Long id = Longs.tryParse(clientid);,唯一会改变的是导入?
  • 如果可能的话,你能提供一个这个插件的工作示例,以便我了解它是如何工作的吗?我有点困惑。
  • 是的。基本上就是这么简单。我在答案中添加了一个示例 POM。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2019-07-16
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    相关资源
    最近更新 更多