DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

checkNotNull in Preconditions with multiple values in one call (e.g. as varargs) #1447

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi Guavians,

I´d prefer if there was a "concluded" checkNotNull-method in Preconditions to 
save a few lines of code.
I often uses constructors like these:

public Person (@Nonnull final String name,@Nonnull  final String surname, final 
Adress adress) {
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(surname);
    Preconditions.checkNotNull(adress);

    // constructor stuff like this.name = name  and so on....
}

I would like to write something like this:
public Person (@Nonnull final String name,@Nonnull  final String surname, final 
Adress adress) {
    Preconditions.checkNotNullForAll(name,surname,adress);

// constructor stuff like this.name = name  and so on....
}

By the way: Thanks for all the amazing stuff in guava - it´s awesome!!!

Original issue reported on code.google.com by stefan.b...@gmail.com on 13 Jun 2013 at 12:23

GoogleCodeExporter commented 9 years ago
Hey Stefan,

You're best bet is to just inline the checkNotNull with your assignments. In 
fact, you'll end up with even fewer lines of code doing this than if we had 
checkNotNullForAll(). For example:

public Person (@Nonnull final String name,@Nonnull  final String surname, final 
Adress adress) {
    this.name = Preconditions.checkNotNull(name);
    this.surname = Preconditions.checkNotNull(surname);
    this.address = Preconditions.checkNotNull(adress);
}

A common approach is to actually static import checkNotNull as well. Then you 
end up with just:

public Person (@Nonnull final String name,@Nonnull  final String surname, final 
Adress adress) {
    this.name = checkNotNull(name);
    this.surname = checkNotNull(surname);
    this.address = checkNotNull(adress);
}

Thanks,
-Kurt

Original comment by kurt.kluever on 13 Jun 2013 at 3:12

GoogleCodeExporter commented 9 years ago
Hey Kurt,

that´s a pretty solution - I missed the return value of 
Preconditions.checkNotNull().

Thanks a lot!

Stefan

Original comment by stefan.b...@gmail.com on 13 Jun 2013 at 3:52

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08