From cce88f819ae0ef84b5a86c6a826f88ce0208107f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 17 Jan 2013 12:59:30 +0100 Subject: [PATCH] =?utf8?q?Replace=20utils=E2=80=99=20Validation=20by=20Gua?= =?utf8?q?va=E2=80=99s=20Preconditions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/Core.java | 56 +++++++++++++++------- .../net/pterodactylus/sone/core/ImageInserter.java | 8 +++- .../pterodactylus/sone/core/WebOfTrustUpdater.java | 8 ++-- .../java/net/pterodactylus/sone/data/Album.java | 54 ++++++++++++++------- .../java/net/pterodactylus/sone/data/Image.java | 33 +++++++------ .../java/net/pterodactylus/sone/data/Profile.java | 31 +++++++----- .../java/net/pterodactylus/sone/data/Sone.java | 23 +++++---- .../pterodactylus/sone/data/TemporaryImage.java | 11 +++-- .../net/pterodactylus/sone/fcp/FcpInterface.java | 6 +-- .../sone/freenet/SimpleFieldSetBuilder.java | 6 +-- .../sone/notify/ListNotificationFilters.java | 7 +-- 11 files changed, 156 insertions(+), 87 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 82fb4c5..94908cb 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,6 +17,9 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; @@ -81,7 +84,6 @@ import net.pterodactylus.util.thread.Ticker; import net.pterodactylus.util.validation.EqualityValidator; import net.pterodactylus.util.validation.IntegerRangeValidator; import net.pterodactylus.util.validation.OrValidator; -import net.pterodactylus.util.validation.Validation; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; @@ -296,7 +298,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return The Sone rescuer for the given Sone */ public SoneRescuer getSoneRescuer(Sone sone) { - Validation.begin().isNotNull("Sone", sone).check().is("Local Sone", sone.isLocal()).check(); + checkNotNull(sone, "sone must not be null"); + checkArgument(sone.isLocal(), "sone must be local"); synchronized (sones) { SoneRescuer soneRescuer = soneRescuers.get(sone); if (soneRescuer == null) { @@ -500,7 +503,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return {@code true} if the target Sone is trusted by the origin Sone */ public boolean isSoneTrusted(Sone origin, Sone target) { - Validation.begin().isNotNull("Origin", origin).isNotNull("Target", target).check().isInstanceOf("Origin’s OwnIdentity", origin.getIdentity(), OwnIdentity.class).check(); + checkNotNull(origin, "origin must not be null"); + checkNotNull(target, "target must not be null"); + checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin’s identity must be an OwnIdentity"); return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity()); } @@ -546,7 +551,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return All posts that have the given Sone as recipient */ public Set getDirectedPosts(Sone recipient) { - Validation.begin().isNotNull("Recipient", recipient).check(); + checkNotNull(recipient, "recipient must not be null"); Set directedPosts = new HashSet(); synchronized (posts) { for (Post post : posts.values()) { @@ -911,7 +916,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The ID of the Sone to follow */ public void followSone(Sone sone, String soneId) { - Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check(); + checkNotNull(sone, "sone must not be null"); + checkNotNull(soneId, "soneId must not be null"); Sone followedSone = getSone(soneId, true); if (followedSone == null) { logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId)); @@ -932,7 +938,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The Sone that should be followed */ public void followSone(Sone sone, Sone followedSone) { - Validation.begin().isNotNull("Sone", sone).isNotNull("Followed Sone", followedSone).check(); + checkNotNull(sone, "sone must not be null"); + checkNotNull(followedSone, "followedSone must not be null"); sone.addFriend(followedSone.getId()); synchronized (soneFollowingTimes) { if (!soneFollowingTimes.containsKey(followedSone)) { @@ -962,7 +969,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The ID of the Sone being unfollowed */ public void unfollowSone(Sone sone, String soneId) { - Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check(); + checkNotNull(sone, "sone must not be null"); + checkNotNull(soneId, "soneId must not be null"); unfollowSone(sone, getSone(soneId, false)); } @@ -977,7 +985,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The Sone being unfollowed */ public void unfollowSone(Sone sone, Sone unfollowedSone) { - Validation.begin().isNotNull("Sone", sone).isNotNull("Unfollowed Sone", unfollowedSone).check(); + checkNotNull(sone, "sone must not be null"); + checkNotNull(unfollowedSone, "unfollowedSone must not be null"); sone.removeFriend(unfollowedSone.getId()); boolean unfollowedSoneStillFollowed = false; for (Sone localSone : getLocalSones()) { @@ -1002,7 +1011,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The trust value (from {@code -100} to {@code 100}) */ public void setTrust(Sone origin, Sone target, int trustValue) { - Validation.begin().isNotNull("Trust Origin", origin).check().isInstanceOf("Trust Origin", origin.getIdentity(), OwnIdentity.class).isNotNull("Trust Target", target).isLessOrEqual("Trust Value", trustValue, 100).isGreaterOrEqual("Trust Value", trustValue, -100).check(); + checkNotNull(origin, "origin must not be null"); + checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone"); + checkNotNull(target, "target must not be null"); + checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]"); webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment()); } @@ -1015,7 +1027,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The trust target */ public void removeTrust(Sone origin, Sone target) { - Validation.begin().isNotNull("Trust Origin", origin).isNotNull("Trust Target", target).check().isInstanceOf("Trust Origin Identity", origin.getIdentity(), OwnIdentity.class).check(); + checkNotNull(origin, "origin must not be null"); + checkNotNull(target, "target must not be null"); + checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone"); webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null); } @@ -1517,7 +1531,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return The created post */ public Post createPost(Sone sone, Sone recipient, long time, String text) { - Validation.begin().isNotNull("Text", text).check().isGreater("Text Length", text.length(), 0).check(); + checkNotNull(text, "text must not be null"); + checkArgument(text.trim().length() > 0, "text must not be empty"); if (!sone.isLocal()) { logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone)); return null; @@ -1658,7 +1673,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return The created reply */ public PostReply createReply(Sone sone, Post post, long time, String text) { - Validation.begin().isNotNull("Text", text).check().isGreater("Text Length", text.trim().length(), 0).check(); + checkNotNull(text, "text must not be null"); + checkArgument(text.trim().length() > 0, "text must not be empty"); if (!sone.isLocal()) { logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone)); return null; @@ -1768,7 +1784,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The album to remove */ public void deleteAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().is("Local Sone", album.getSone().isLocal()).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone"); if (!album.isEmpty()) { return; } @@ -1795,7 +1812,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * @return The newly created image */ public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) { - Validation.begin().isNotNull("Sone", sone).isNotNull("Album", album).isNotNull("Temporary Image", temporaryImage).check().is("Local Sone", sone.isLocal()).check().isEqual("Owner and Album Owner", sone, album.getSone()).check(); + checkNotNull(sone, "sone must not be null"); + checkNotNull(album, "album must not be null"); + checkNotNull(temporaryImage, "temporaryImage must not be null"); + checkArgument(sone.isLocal(), "sone must be a local Sone"); + checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone"); Image image = new Image(temporaryImage.getId()).setSone(sone).setCreationTime(System.currentTimeMillis()); album.addImage(image); synchronized (images) { @@ -1814,7 +1835,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The image to delete */ public void deleteImage(Image image) { - Validation.begin().isNotNull("Image", image).check().is("Local Sone", image.getSone().isLocal()).check(); + checkNotNull(image, "image must not be null"); + checkArgument(image.getSone().isLocal(), "image must belong to a local Sone"); deleteTemporaryImage(image.getId()); image.getAlbum().removeImage(image); synchronized (images) { @@ -1848,7 +1870,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The temporary image to delete */ public void deleteTemporaryImage(TemporaryImage temporaryImage) { - Validation.begin().isNotNull("Temporary Image", temporaryImage).check(); + checkNotNull(temporaryImage, "temporaryImage must not be null"); deleteTemporaryImage(temporaryImage.getId()); } @@ -1859,7 +1881,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider * The ID of the temporary image to delete */ public void deleteTemporaryImage(String imageId) { - Validation.begin().isNotNull("Temporary Image ID", imageId).check(); + checkNotNull(imageId, "imageId must not be null"); synchronized (temporaryImages) { temporaryImages.remove(imageId); } diff --git a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java index 4655c33..4ec424e 100644 --- a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java @@ -17,6 +17,9 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -27,7 +30,6 @@ import net.pterodactylus.sone.core.FreenetInterface.InsertToken; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.util.logging.Logging; -import net.pterodactylus.util.validation.Validation; /** * The image inserter is responsible for inserting images using @@ -67,7 +69,9 @@ public class ImageInserter { * The image */ public void insertImage(TemporaryImage temporaryImage, Image image) { - Validation.begin().isNotNull("Temporary Image", temporaryImage).isNotNull("Image", image).check().isEqual("Image IDs", image.getId(), temporaryImage.getId()).check(); + checkNotNull(temporaryImage, "temporaryImage must not be null"); + checkNotNull(image, "image must not be null"); + checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match"); try { InsertToken insertToken = freenetInterface.new InsertToken(image); insertTokens.put(image.getId(), insertToken); diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index 3d4ad7d..2328c74 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.core; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.logging.Level; @@ -31,7 +33,6 @@ import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector; import net.pterodactylus.sone.freenet.wot.WebOfTrustException; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; -import net.pterodactylus.util.validation.Validation; import com.google.inject.Inject; @@ -474,9 +475,8 @@ public class WebOfTrustUpdater extends AbstractService { */ @SuppressWarnings("synthetic-access") public WebOfTrustContextUpdateJob(OwnIdentity ownIdentity, String context) { - Validation.begin().isNotNull("OwnIdentity", ownIdentity).isNotNull("Context", context).check(); - this.ownIdentity = ownIdentity; - this.context = context; + this.ownIdentity = checkNotNull(ownIdentity, "ownIdentity must not be null"); + this.context = checkNotNull(context, "context must not be null"); } // diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 7c62e33..94af8ec 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -17,6 +17,10 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; @@ -25,7 +29,6 @@ import java.util.Map; import java.util.UUID; import net.pterodactylus.util.object.Default; -import net.pterodactylus.util.validation.Validation; import com.google.common.base.Function; import com.google.common.base.Predicates; @@ -88,8 +91,7 @@ public class Album implements Fingerprintable { * The ID of the album */ public Album(String id) { - Validation.begin().isNotNull("Album ID", id).check(); - this.id = id; + this.id = checkNotNull(id, "id must not be null"); } // @@ -123,7 +125,8 @@ public class Album implements Fingerprintable { * @return This album */ public Album setSone(Sone sone) { - Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check(); + checkNotNull(sone, "sone must not be null"); + checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone"); this.sone = sone; return this; } @@ -144,7 +147,9 @@ public class Album implements Fingerprintable { * The album to add */ public void addAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); + checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone"); album.setParent(this); if (!albums.contains(album)) { albums.add(album); @@ -158,7 +163,9 @@ public class Album implements Fingerprintable { * The album to remove */ public void removeAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.sone.equals(sone), "album must belong this album’s Sone"); + checkArgument(equals(album.parent), "album must belong to this album"); albums.remove(album); album.removeParent(); } @@ -173,7 +180,9 @@ public class Album implements Fingerprintable { * null if the album did not change its place */ public Album moveAlbumUp(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album"); + checkArgument(equals(album.parent), "album must belong to this album"); int oldIndex = albums.indexOf(album); if (oldIndex <= 0) { return null; @@ -193,7 +202,9 @@ public class Album implements Fingerprintable { * null if the album did not change its place */ public Album moveAlbumDown(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album"); + checkArgument(equals(album.parent), "album must belong to this album"); int oldIndex = albums.indexOf(album); if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) { return null; @@ -226,7 +237,9 @@ public class Album implements Fingerprintable { * The image to add */ public void addImage(Image image) { - Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check(); + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); if (image.getAlbum() != null) { image.getAlbum().removeImage(image); } @@ -247,7 +260,9 @@ public class Album implements Fingerprintable { * The image to remove */ public void removeImage(Image image) { - Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check(); + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); imageIds.remove(image.getId()); images.remove(image.getId()); if (image.getId().equals(albumImage)) { @@ -269,7 +284,10 @@ public class Album implements Fingerprintable { * null if the image did not change its place */ public Image moveImageUp(Image image) { - Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check(); + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); + checkArgument(image.getAlbum().equals(this), "image must belong to this album"); int oldIndex = imageIds.indexOf(image.getId()); if (oldIndex <= 0) { return null; @@ -289,7 +307,10 @@ public class Album implements Fingerprintable { * null if the image did not change its place */ public Image moveImageDown(Image image) { - Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check(); + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); + checkArgument(image.getAlbum().equals(this), "image must belong to this album"); int oldIndex = imageIds.indexOf(image.getId()); if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) { return null; @@ -351,8 +372,7 @@ public class Album implements Fingerprintable { * @return This album */ protected Album setParent(Album parent) { - Validation.begin().isNotNull("Album Parent", parent).check(); - this.parent = parent; + this.parent = checkNotNull(parent, "parent must not be null"); return this; } @@ -383,8 +403,7 @@ public class Album implements Fingerprintable { * @return This album */ public Album setTitle(String title) { - Validation.begin().isNotNull("Album Title", title).check(); - this.title = title; + this.title = checkNotNull(title, "title must not be null"); return this; } @@ -405,8 +424,7 @@ public class Album implements Fingerprintable { * @return This album */ public Album setDescription(String description) { - Validation.begin().isNotNull("Album Description", description).check(); - this.description = description; + this.description = checkNotNull(description, "description must not be null"); return this; } diff --git a/src/main/java/net/pterodactylus/sone/data/Image.java b/src/main/java/net/pterodactylus/sone/data/Image.java index 25a36e3..8d5f023 100644 --- a/src/main/java/net/pterodactylus/sone/data/Image.java +++ b/src/main/java/net/pterodactylus/sone/data/Image.java @@ -17,9 +17,11 @@ package net.pterodactylus.sone.data; -import java.util.UUID; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; -import net.pterodactylus.util.validation.Validation; +import java.util.UUID; /** * Container for image metadata. @@ -70,8 +72,7 @@ public class Image implements Fingerprintable { * The ID of the image */ public Image(String id) { - Validation.begin().isNotNull("Image ID", id).check(); - this.id = id; + this.id = checkNotNull(id, "id must not be null"); } // @@ -105,7 +106,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setSone(Sone sone) { - Validation.begin().isNotNull("New Image Owner", sone).isEither("Old Image Owner", this.sone, null, sone).check(); + checkNotNull(sone, "sone must not be null"); + checkArgument((this.sone == null) || this.sone.equals(sone), "sone must not already be set to another sone"); this.sone = sone; return this; } @@ -128,7 +130,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setAlbum(Album album) { - Validation.begin().isNotNull("New Album", album).check().isEqual("Album Owner and Image Owner", album.getSone(), getSone()).check(); + checkNotNull(album, "album must not be null"); + checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image"); this.album = album; return this; } @@ -151,7 +154,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setKey(String key) { - Validation.begin().isNotNull("New Image Key", key).isEither("Old Image Key", this.key, null, key).check(); + checkNotNull(key, "key must not be null"); + checkState((this.key == null) || this.key.equals(key), "key must not be already set to another key"); this.key = key; return this; } @@ -187,7 +191,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setCreationTime(long creationTime) { - Validation.begin().isGreater("New Image Creation Time", creationTime, 0).isEither("Old Image Creation Time", this.creationTime, 0L, creationTime).check(); + checkArgument(creationTime > 0, "creationTime must be > 0"); + checkState((this.creationTime == 0) || (this.creationTime == creationTime), "creationTime must not already be set"); this.creationTime = creationTime; return this; } @@ -210,7 +215,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setWidth(int width) { - Validation.begin().isGreater("New Image Width", width, 0).isEither("Old Image Width", this.width, 0, width).check(); + checkArgument(width > 0, "width must be > 0"); + checkState((this.width == 0) || (this.width == width), "width must not already be set to another width"); this.width = width; return this; } @@ -233,7 +239,8 @@ public class Image implements Fingerprintable { * @return This image */ public Image setHeight(int height) { - Validation.begin().isGreater("New Image Height", height, 0).isEither("Old Image Height", this.height, 0, height).check(); + checkArgument(height > 0, "height must be > 0"); + checkState((this.height == 0) || (this.height == height), "height must not already be set to another height"); this.height = height; return this; } @@ -255,8 +262,7 @@ public class Image implements Fingerprintable { * @return This image */ public Image setTitle(String title) { - Validation.begin().isNotNull("Image Title", title).check(); - this.title = title; + this.title = checkNotNull(title, "title must not be null"); return this; } @@ -277,8 +283,7 @@ public class Image implements Fingerprintable { * @return This image */ public Image setDescription(String description) { - Validation.begin().isNotNull("Image Description", description).check(); - this.description = description; + this.description = checkNotNull(description, "description must not be null"); return this; } diff --git a/src/main/java/net/pterodactylus/sone/data/Profile.java b/src/main/java/net/pterodactylus/sone/data/Profile.java index 6936409..4b4e0f4 100644 --- a/src/main/java/net/pterodactylus/sone/data/Profile.java +++ b/src/main/java/net/pterodactylus/sone/data/Profile.java @@ -17,13 +17,15 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.UUID; -import net.pterodactylus.util.validation.Validation; - /** * A profile stores personal information about a {@link Sone}. All information * is optional and can be {@code null}. @@ -237,7 +239,7 @@ public class Profile implements Fingerprintable { this.avatar = null; return this; } - Validation.begin().isEqual("Image Owner", avatar.getSone(), sone).check(); + checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone"); this.avatar = avatar.getId(); return this; } @@ -283,7 +285,7 @@ public class Profile implements Fingerprintable { * field with the given ID */ public Field getFieldById(String fieldId) { - Validation.begin().isNotNull("Field ID", fieldId).check(); + checkNotNull(fieldId, "fieldId must not be null"); for (Field field : fields) { if (field.getId().equals(fieldId)) { return field; @@ -319,7 +321,9 @@ public class Profile implements Fingerprintable { * if the name is not valid */ public Field addField(String fieldName) throws IllegalArgumentException { - Validation.begin().isNotNull("Field Name", fieldName).check().isGreater("Field Name Length", fieldName.length(), 0).isNull("Field Name Unique", getFieldByName(fieldName)).check(); + checkNotNull(fieldName, "fieldName must not be null"); + checkArgument(fieldName.length() > 0, "fieldName must not be empty"); + checkState(getFieldByName(fieldName) == null, "fieldName must be unique"); @SuppressWarnings("synthetic-access") Field field = new Field().setName(fieldName); fields.add(field); @@ -335,7 +339,9 @@ public class Profile implements Fingerprintable { * The field to move up */ public void moveFieldUp(Field field) { - Validation.begin().isNotNull("Field", field).check().is("Field Existing", hasField(field)).isGreater("Field Index", getFieldIndex(field), 0).check(); + checkNotNull(field, "field must not be null"); + checkArgument(hasField(field), "field must belong to this profile"); + checkArgument(getFieldIndex(field) > 0, "field index must be > 0"); int fieldIndex = getFieldIndex(field); fields.remove(field); fields.add(fieldIndex - 1, field); @@ -350,7 +356,9 @@ public class Profile implements Fingerprintable { * The field to move down */ public void moveFieldDown(Field field) { - Validation.begin().isNotNull("Field", field).check().is("Field Existing", hasField(field)).isLess("Field Index", getFieldIndex(field), fields.size() - 1).check(); + checkNotNull(field, "field must not be null"); + checkArgument(hasField(field), "field must belong to this profile"); + checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1)); int fieldIndex = getFieldIndex(field); fields.remove(field); fields.add(fieldIndex + 1, field); @@ -363,7 +371,8 @@ public class Profile implements Fingerprintable { * The field to remove */ public void removeField(Field field) { - Validation.begin().isNotNull("Field", field).check().is("Field Existing", hasField(field)).check(); + checkNotNull(field, "field must not be null"); + checkArgument(hasField(field), "field must belong to this profile"); fields.remove(field); } @@ -455,8 +464,7 @@ public class Profile implements Fingerprintable { * The ID of the field */ private Field(String id) { - Validation.begin().isNotNull("Field ID", id).check(); - this.id = id; + this.id = checkNotNull(id, "id must not be null"); } /** @@ -487,7 +495,8 @@ public class Profile implements Fingerprintable { * @return This field */ public Field setName(String name) { - Validation.begin().isNotNull("Field Name", name).check().is("Field Unique", (getFieldByName(name) == null) || equals(getFieldByName(name))).check(); + checkNotNull(name, "name must not be null"); + checkArgument(getFieldByName(name) == null, "name must be unique"); this.name = name; return this; } diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 80032ad..d2da454 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -17,6 +17,9 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -33,7 +36,6 @@ import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.util.logging.Logging; -import net.pterodactylus.util.validation.Validation; import com.google.common.base.Predicate; @@ -422,8 +424,7 @@ public class Sone implements Fingerprintable, Comparable { * if {@code status} is {@code null} */ public Sone setStatus(SoneStatus status) { - Validation.begin().isNotNull("Sone Status", status).check(); - this.status = status; + this.status = checkNotNull(status, "status must not be null"); return this; } @@ -808,7 +809,8 @@ public class Sone implements Fingerprintable, Comparable { * The album to add */ public void addAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(this), "album must belong to this Sone"); if (!albums.contains(album)) { albums.add(album); } @@ -821,7 +823,7 @@ public class Sone implements Fingerprintable, Comparable { * The albums of this Sone */ public void setAlbums(Collection albums) { - Validation.begin().isNotNull("Albums", albums).check(); + checkNotNull(albums, "albums must not be null"); this.albums.clear(); for (Album album : albums) { addAlbum(album); @@ -835,7 +837,8 @@ public class Sone implements Fingerprintable, Comparable { * The album to remove */ public void removeAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(this), "album must belong to this Sone"); albums.remove(album); } @@ -849,7 +852,9 @@ public class Sone implements Fingerprintable, Comparable { * null if the album did not change its place */ public Album moveAlbumUp(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(this), "album must belong to this Sone"); + checkArgument(album.getParent() == null, "album must not have a parent"); int oldIndex = albums.indexOf(album); if (oldIndex <= 0) { return null; @@ -869,7 +874,9 @@ public class Sone implements Fingerprintable, Comparable { * null if the album did not change its place */ public Album moveAlbumDown(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.getSone(), this).isNull("Album Parent", album.getParent()).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(this), "album must belong to this Sone"); + checkArgument(album.getParent() == null, "album must not have a parent"); int oldIndex = albums.indexOf(album); if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) { return null; diff --git a/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java b/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java index 5e7c78d..10dea0d 100644 --- a/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java +++ b/src/main/java/net/pterodactylus/sone/data/TemporaryImage.java @@ -17,9 +17,10 @@ package net.pterodactylus.sone.data; -import java.util.UUID; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; -import net.pterodactylus.util.validation.Validation; +import java.util.UUID; /** * A temporary image stores an uploaded image in memory until it has been @@ -82,7 +83,8 @@ public class TemporaryImage { * @return This temporary image */ public TemporaryImage setMimeType(String mimeType) { - Validation.begin().isNotNull("MIME Type", mimeType).isNull("Previous MIME Type", this.mimeType).check(); + checkNotNull(mimeType, "mimeType must not be null"); + checkState(this.mimeType == null, "mime type must not already be set"); this.mimeType = mimeType; return this; } @@ -105,7 +107,8 @@ public class TemporaryImage { * @return This temporary image */ public TemporaryImage setImageData(byte[] imageData) { - Validation.begin().isNotNull("Image Data", imageData).isNull("Previous Image Data", this.imageData).check(); + checkNotNull(imageData, "imageData must not be null"); + checkState(this.imageData == null, "image data must not already be set"); this.imageData = imageData; return this; } diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index a0e3c40..e63f651 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.fcp; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -28,7 +30,6 @@ import net.pterodactylus.sone.freenet.fcp.Command.AccessType; import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse; import net.pterodactylus.sone.freenet.fcp.Command.Response; import net.pterodactylus.util.logging.Logging; -import net.pterodactylus.util.validation.Validation; import com.google.inject.Inject; @@ -122,8 +123,7 @@ public class FcpInterface { * The action level for which full FCP access is required */ public void setFullAccessRequired(FullAccessRequired fullAccessRequired) { - Validation.begin().isNotNull("FullAccessRequired", fullAccessRequired).check(); - this.fullAccessRequired = fullAccessRequired; + this.fullAccessRequired = checkNotNull(fullAccessRequired, "fullAccessRequired must not be null"); } // diff --git a/src/main/java/net/pterodactylus/sone/freenet/SimpleFieldSetBuilder.java b/src/main/java/net/pterodactylus/sone/freenet/SimpleFieldSetBuilder.java index 5507b0d..8cac3e3 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/SimpleFieldSetBuilder.java +++ b/src/main/java/net/pterodactylus/sone/freenet/SimpleFieldSetBuilder.java @@ -17,7 +17,8 @@ package net.pterodactylus.sone.freenet; -import net.pterodactylus.util.validation.Validation; +import static com.google.common.base.Preconditions.checkNotNull; + import freenet.support.SimpleFieldSet; /** @@ -46,8 +47,7 @@ public class SimpleFieldSetBuilder { * The simple field set to build */ public SimpleFieldSetBuilder(SimpleFieldSet simpleFieldSet) { - Validation.begin().isNotNull("Simple Field Set", simpleFieldSet).check(); - this.simpleFieldSet = simpleFieldSet; + this.simpleFieldSet = checkNotNull(simpleFieldSet, "simpleFieldSet must not be null"); } /** diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java index 6efee27..2e2ee18 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.notify; +import static com.google.common.base.Preconditions.checkNotNull; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -28,7 +30,6 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.freenet.wot.Trust; import net.pterodactylus.util.notify.Notification; -import net.pterodactylus.util.validation.Validation; /** * Filter for {@link ListNotification}s. @@ -218,7 +219,7 @@ public class ListNotificationFilters { * otherwise */ public static boolean isPostVisible(Sone sone, Post post) { - Validation.begin().isNotNull("Post", post).check(); + checkNotNull(post, "post must not be null"); Sone postSone = post.getSone(); if (postSone == null) { return false; @@ -280,7 +281,7 @@ public class ListNotificationFilters { * otherwise */ public static boolean isReplyVisible(Sone sone, PostReply reply) { - Validation.begin().isNotNull("Reply", reply).check(); + checkNotNull(reply, "reply must not be null"); Post post = reply.getPost(); if (post == null) { return false; -- 2.7.4