I have a method in which I am accepting a String clientid
and that has below requirements:
clientid
can be a positive number greater than zero. But if it is negative number or zero, then throw IllegalArgumentException
with a message.clientid
cannot be a null
or empty string. But if it is, then throw IllegalArgumentException
with a message.clientid
can be a normal string as well. For example - it can be abcdefgh
or any other string.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);
without using guava or may be with older guava version 11. Since Longs.tryParse
method was added in Guava 14 or later.
What is the best way to do that? Anything we can use from Apache Commons?
I recommend repackaging Guava using the Apache Maven Shade Plugin by relocating classes. 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.
By doing so you can use the latest version of Guava without causing dependency conflicts for your customer.
Here is an example POM based on jersey-repackaged-guava
:
<?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:
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 (e.g. see 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.