Skip to content
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brennaswitzer.cookbook.config;

import com.brennaswitzer.cookbook.util.ValueUtils;
import com.nimbusds.jose.JOSEObjectType;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.jwk.JWK;
Expand Down Expand Up @@ -34,7 +35,7 @@ public class JwtConfig {
public JWKSet jwkSet(AppProperties appProperties) {
List<AppProperties.Secret> tokenSecrets = appProperties.getAuth()
.getTokenSecrets();
if (tokenSecrets == null || tokenSecrets.isEmpty()) {
if (ValueUtils.noValue(tokenSecrets)) {
throw new IllegalStateException("At least one token secret must be provided in app.auth.token-secrets[]");
}
Base64.Decoder decoder = Base64.getDecoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface AggregateIngredient {
Collection<IngredientRef> getIngredients();

default IngredientRef addIngredient(Ingredient ingredient) {
return addIngredient(Quantity.ONE, ingredient, null);
return addIngredient(null, ingredient, null);
}

default IngredientRef addIngredient(Quantity quantity, Ingredient ingredient) {
Expand Down
35 changes: 3 additions & 32 deletions src/main/java/com/brennaswitzer/cookbook/domain/IngredientRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,51 +60,22 @@ public IngredientRef(String raw) {
setRaw(raw);
}

public boolean hasIngredient() {
return ingredient != null;
}

public String getRaw() {
return raw == null ? toString() : raw;
}

public Quantity getQuantity() {
if (quantity == null) return Quantity.ONE;
return quantity;
}

public boolean hasQuantity() {
return quantity != null;
}

public IngredientRef scale(Double scale) {
if (!hasQuantity() || scale == 1) return this;
if (scale <= 0) throw new IllegalArgumentException("Scaling by " + scale + " makes no sense?!");
if (scale == 1 || !hasQuantity()) return this;
return new IngredientRef(
getQuantity().times(scale),
getIngredient(),
getPreparation());
}

public boolean hasPreparation() {
return preparation != null && !preparation.isEmpty();
}

@Override
public String toString() {
return toString(true);
}

public String toString(boolean includePrep) {
if (!hasIngredient()) return raw;
StringBuilder sb = new StringBuilder();
if (hasQuantity()) {
sb.append(quantity).append(' ');
}
sb.append(ingredient.getName());
if (includePrep && hasPreparation()) {
sb.append(", ").append(preparation);
}
return sb.toString();
return toRaw(true);
}

}
32 changes: 32 additions & 0 deletions src/main/java/com/brennaswitzer/cookbook/domain/Item.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.brennaswitzer.cookbook.domain;

import com.brennaswitzer.cookbook.util.ValueUtils;

/**
* Describes a single, atomic unit for a recipe, which could be
* something like "3 oz Parmesan, shredded" or "2 each Pizza Dough, thawed".
Expand All @@ -21,16 +23,46 @@ public interface Item {
*/
Quantity getQuantity();

default boolean hasQuantity() {
return getQuantity() != null;
}

/**
* The rest of the info needed to prep a plan item
* @return instructions on prep
*/
String getPreparation();

default boolean hasPreparation() {
return ValueUtils.hasValue(getPreparation());
}

/**
* Reference to an ingredient, in this case most likely
* a pantry item.
* @return an ingredient
*/
Ingredient getIngredient();

default boolean hasIngredient() {
return getIngredient() != null;
}

default String toRaw(boolean includePrep) {
StringBuilder sb = new StringBuilder();
if (hasQuantity()) {
sb.append(getQuantity()).append(' ');
}
if (hasIngredient()) {
sb.append(getIngredient().getName());
}
if (includePrep && hasPreparation()) {
if (hasIngredient()) {
sb.append(", ");
}
sb.append(getPreparation());
}
if (sb.isEmpty()) return getRaw();
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.brennaswitzer.cookbook.domain;

import com.brennaswitzer.cookbook.repositories.PantryItemSearchRepository;
import com.brennaswitzer.cookbook.util.ValueUtils;
import com.fasterxml.jackson.annotation.JsonTypeName;
import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorValue;
Expand Down Expand Up @@ -86,7 +87,7 @@ private void ensureNameIsNotSynonym() {
return;
}
var name = getName();
if (name == null || name.isBlank()) return;
if (ValueUtils.noValue(name)) return;
if (!synonyms.remove(name)) {
synonyms.removeIf(name::equalsIgnoreCase);
}
Expand All @@ -102,7 +103,7 @@ private void ensureNameIsNotSynonym() {
public void setName(String name) {
name = name.trim();
var oldName = getName();
if (oldName != null && !oldName.equalsIgnoreCase(name) && !oldName.isBlank()) {
if (ValueUtils.hasValue(oldName) && !oldName.equalsIgnoreCase(name)) {
addSynonym(oldName);
}
super.setName(name);
Expand All @@ -116,7 +117,7 @@ public boolean answersTo(String name) {

public boolean hasSynonym(String synonym) {
if (synonyms == null) return false;
if (synonym == null || synonym.isBlank()) return false;
if (ValueUtils.noValue(synonym)) return false;
synonym = synonym.trim();
if (synonyms.contains(synonym)) return true;
for (var syn : synonyms)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/brennaswitzer/cookbook/domain/Plan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brennaswitzer.cookbook.domain;

import com.brennaswitzer.cookbook.util.ValueUtils;
import jakarta.persistence.CascadeType;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Embedded;
Expand Down Expand Up @@ -87,7 +88,7 @@ public int getBucketCount() {
}

public boolean hasBuckets() {
return buckets != null && !buckets.isEmpty();
return ValueUtils.hasValue(buckets);
}

public Set<PlanItem> getTrashBinItems() {
Expand All @@ -98,7 +99,7 @@ public Set<PlanItem> getTrashBinItems() {
}

public boolean hasTrash() {
return trashBinItems != null && !trashBinItems.isEmpty();
return ValueUtils.hasValue(trashBinItems);
}

public User getOwner() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brennaswitzer.cookbook.domain;

import com.brennaswitzer.cookbook.util.ValueUtils;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -70,7 +71,7 @@ public boolean isDated() {
}

public boolean isNamed() {
return name != null && !name.isBlank();
return ValueUtils.hasValue(name);
}

public void setPlan(Plan plan) {
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/brennaswitzer/cookbook/domain/PlanItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brennaswitzer.cookbook.domain;

import com.brennaswitzer.cookbook.util.ValueUtils;
import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorValue;
Expand Down Expand Up @@ -85,6 +86,7 @@ public class PlanItem extends BaseEntity implements Named, MutableItem, CorePlan
private PlanItemStatus status = PlanItemStatus.NEEDED;

@Embedded
@Getter
@Setter
private Quantity quantity;

Expand Down Expand Up @@ -449,22 +451,12 @@ public String getRaw() {
return getName();
}

@Override
public Quantity getQuantity() {
if (quantity == null) return Quantity.ONE;
return quantity;
}

public boolean hasIngredient() {
return ingredient != null;
}

public boolean hasBucket() {
return bucket != null;
}

public boolean hasNotes() {
return notes != null && !notes.isEmpty();
return ValueUtils.hasValue(notes);
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/brennaswitzer/cookbook/domain/S3File.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brennaswitzer.cookbook.domain;

import com.brennaswitzer.cookbook.util.ValueUtils;
import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -17,7 +18,7 @@ public class S3File {

private static final Pattern FILENAME_SANITIZER = Pattern.compile("[^a-zA-Z0-9.\\-]+");
public static String sanitizeFilename(String filename) {
if (filename == null || filename.isBlank()) {
if (ValueUtils.noValue(filename)) {
return "unnamed";
}
// Opera supplies a full path, not just a filename
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.brennaswitzer.cookbook.repositories.impl.SortDir;
import com.brennaswitzer.cookbook.services.IngredientService;
import com.brennaswitzer.cookbook.services.PantryItemService;
import com.brennaswitzer.cookbook.util.ValueUtils;
import graphql.relay.Connection;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -66,7 +67,7 @@ Long getDuplicateOf() {
}

Sort getSort() {
if (sortBy == null || sortBy.isBlank()) return null;
if (ValueUtils.noValue(sortBy)) return null;
Sort.Direction dir = SortDir.DESC == sortDir
? Sort.Direction.DESC
: Sort.Direction.ASC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.brennaswitzer.cookbook.security.CurrentUser;
import com.brennaswitzer.cookbook.security.UserPrincipal;
import com.brennaswitzer.cookbook.util.ShareHelper;
import com.brennaswitzer.cookbook.util.ValueUtils;
import org.dataloader.DataLoader;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -72,7 +73,7 @@ public List<IngredientRef> ingredients(Recipe recipe,
Stream<IngredientRef> allIngredients = recipe.getIngredients()
.stream()
.filter(not(Section::isSection));
if (ingredientIds != null && !ingredientIds.isEmpty()) {
if (ValueUtils.hasValue(ingredientIds)) {
allIngredients = allIngredients
.filter(r -> r.hasIngredient()
&& ingredientIds.contains(r.getIngredient().getId()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.brennaswitzer.cookbook.payload;

import com.brennaswitzer.cookbook.util.ValueUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -28,7 +29,7 @@ public boolean isCookThis() {
}

public boolean hasSections() {
return getSections() != null && !getSections().isEmpty();
return ValueUtils.hasValue(getSections());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.brennaswitzer.cookbook.domain.IngredientRef;
import com.brennaswitzer.cookbook.domain.Quantity;
import com.brennaswitzer.cookbook.domain.UnitOfMeasure;
import com.brennaswitzer.cookbook.util.ValueUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.EntityManager;
import lombok.Getter;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void setUnits(String units) {

@Deprecated
public boolean hasUnits() {
return units != null && !units.isBlank();
return ValueUtils.hasValue(units);
}

public boolean hasUomId() {
Expand Down
Loading