2

When I can use it, I am a big fan of using Guava's Preconditions. However, the Guava jar is 2 MB, which can be quite sizeable...

I have a project whose jar weighs 26k, therefore the question is, is there a lightweight library having such a utility class, with no dependencies other than the JDK (6+)? While I could create one, I'd rather not reinvent the wheel!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
fge
  • 119,121
  • 33
  • 254
  • 329
  • 3
    Why do you care about the size of the jar? The JDK is pretty large on its own. 2MB in the grand scheme of things isn't very large. If 2MB is really going to break your app then the best approach is probably to create your own. – robert_difalco Jun 06 '13 at 06:02
  • I know that... However, requiring 2 MB for a 26k jar is a little excessive. – fge Jun 06 '13 at 06:04

4 Answers4

4

I would use the whole library as suggested in the comments, but if you really want the small size, there is a recommended way specified in guava's docs - Shrinking JARs with ProGuard

jmruc
  • 5,714
  • 3
  • 22
  • 41
0

You can use valid4j with hamcrest-matchers instead (found on Maven Central as org.valid4j:valid4j).

For input validation (throwing recoverable exception):

import static org.valid4j.Validation.*;

validate(argument, isValid(), otherwiseThrowing(InvalidException.class));

Or for preconditions (like assertions really):

import static org.valid4j.Assertive.*;

require(x, greaterThan(0)); // throws RequireViolation extends AssertionError

Links:

keyoxy
  • 4,423
  • 2
  • 21
  • 18
0

Take a look at the Requirements API that I authored. The upcoming 3.0.0 release weighs in at 167k. It is very well maintained and is very easy to use:

Requirements API

String actual = "foosball";
String expected = "ballroom";
requireThat(actual, "actual").isEqualTo(expected, "expected")

gives you this:

output

(If your terminal does not support colors, you will get a textual diff instead)

Gili
  • 86,244
  • 97
  • 390
  • 689
0

You might want to check Fernando Cejas Arrow Library. It has Preconditions, Guava Strings, Collections, etc.

aldok
  • 17,295
  • 5
  • 53
  • 64