Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(name, quantity);
}

@Override
public String toString() {
return "AddOn{" +
"name='" + name + '\'' +
", quantity=" + quantity +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ public static Builder builder(String name, String version) {
return new Builder(name, version);
}

@Override
public String toString() {
return name + ": " + version + " plan" + plan + " addOns " + addOns;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Expand All @@ -63,6 +58,16 @@ public int hashCode() {
return Objects.hash(name, version, addOns, plan);
}

@Override
public String toString() {
return "Service{" +
"name='" + name + '\'' +
", version='" + version + '\'' +
", addOns=" + addOns +
", plan='" + plan + '\'' +
'}';
}

public static final class Builder {

private final String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package io.github.pgmarc.space.contracts;

import java.time.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;

public final class Subscription {
Expand All @@ -24,12 +19,12 @@ public final class Subscription {

private Subscription(Builder builder) {
this.userContact = builder.userContact;
this.services = builder.services;
this.startDate = builder.startDate;
this.endDate = builder.endDate;
this.renewalPeriod = builder.renewalPeriod;
this.services = Collections.unmodifiableMap(builder.services);
this.history = Collections.unmodifiableList(builder.history);
this.usageLevels = Collections.unmodifiableMap(builder.usageLevels);
this.history = builder.history;
this.usageLevels = builder.usageLevels;
}

public static Builder builder(UserContact userContact, ZonedDateTime startDate, ZonedDateTime endDate,
Expand All @@ -41,6 +36,14 @@ public static Builder builder(UserContact userContact, ZonedDateTime startDate,
return new Builder(userContact, startDate, endDate).subscribeAll(services);
}

public String getUserId() {
return userContact.getUserId();
}

public String getUsername() {
return userContact.getUsername();
}

public LocalDateTime getStartDate() {
return startDate.toLocalDateTime();
}
Expand All @@ -58,7 +61,8 @@ public boolean isAutoRenewable() {
}

public Optional<LocalDateTime> getRenewalDate() {
return Optional.ofNullable(isAutoRenewable() ? endDate.plus(renewalPeriod).toLocalDateTime() : null);
return Optional.ofNullable(renewalPeriod)
.map( renewalPeriod -> endDate.plus(renewalPeriod).toLocalDateTime());
}

/**
Expand All @@ -84,33 +88,52 @@ public boolean isExpired(LocalDateTime date) {
return startDate.isBefore(utcDate) && endDate.isBefore(utcDate);
}


public String getUserId() {
return userContact.getUserId();
public Map<String, Service> getServicesMap() {
return Collections.unmodifiableMap(services);
}

public String getUsername() {
return userContact.getUsername();
public Collection<Service> getServices() {
return Collections.unmodifiableCollection(services.values());
}

public Optional<Service> getService(String serviceName) {
return Optional.ofNullable(this.services.get(serviceName));
}

public Map<String, Service> getServicesMap() {
return services;
public List<Snapshot> getHistory() {
return Collections.unmodifiableList(history);
}

public Set<Service> getServices() {
return Set.copyOf(services.values());
public Map<String, Map<String, UsageLevel>> getUsageLevels() {
return Collections.unmodifiableMap(usageLevels);
}

public List<Snapshot> getHistory() {
return history;
public Optional<Map<String, UsageLevel>> getServiceUsageLevels(String service) {
Objects.requireNonNull(service, "service name must not be null");
return Optional.ofNullable(usageLevels.get(service));
}

public Map<String, Map<String, UsageLevel>> getUsageLevels() {
return usageLevels;
public Optional<UsageLevel> getUsageLevel(String service, String usageLimit) {
Objects.requireNonNull(service, "service name must not be null");
Objects.requireNonNull(usageLimit, "usage limit name must not be null");
if (!usageLevels.containsKey(service)) {
return Optional.empty();
}
return Optional.ofNullable(usageLevels.get(service).get(usageLimit));

}

@Override
public String toString() {
return "Subscription{" +
"userContact=" + userContact +
", services=" + services +
", startDate=" + startDate +
", endDate=" + endDate +
", renewalPeriod=" + renewalPeriod +
", history=" + history +
", usageLevels=" + usageLevels +
'}';
}

public enum Keys {
Expand Down Expand Up @@ -256,5 +279,26 @@ public static Snapshot of(ZonedDateTime startDateTime, ZonedDateTime endDateTime
Map<String, Service> services) {
return new Snapshot(startDateTime, endDateTime, services);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Snapshot snapshot = (Snapshot) o;
return Objects.equals(starDateTime, snapshot.starDateTime) && Objects.equals(endDateTime, snapshot.endDateTime) && Objects.equals(services, snapshot.services);
}

@Override
public int hashCode() {
return Objects.hash(starDateTime, endDateTime, services);
}

@Override
public String toString() {
return "Snapshot{" +
"starDateTime=" + starDateTime +
", endDateTime=" + endDateTime +
", services=" + services +
'}';
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.pgmarc.space.contracts;

import java.time.Duration;
import java.time.Period;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -11,12 +11,12 @@ public final class SubscriptionRequest {

private final UserContact userContact;
private final Set<Service> services;
private final Duration renewalDays;
private final Period renewalPeriod;

private SubscriptionRequest(Builder builder) {
this.userContact = builder.userContact;
this.renewalDays = builder.renewalDays;
this.services = Collections.unmodifiableSet(builder.services);
this.renewalPeriod = builder.renewalPeriod;
this.services = builder.services;
}

public static Builder builder(UserContact userContact) {
Expand All @@ -27,20 +27,20 @@ public UserContact getUserContact() {
return userContact;
}

public Set<Service> getServices() {
return services;
public Collection<Service> getServices() {
return Collections.unmodifiableCollection(services);
}

public Duration getRenewalDays() {
return renewalDays;
public Period getRenewalPeriod() {
return renewalPeriod;
}

public static final class Builder {

private final UserContact userContact;
private final Set<Service> services = new HashSet<>();
private Service.Builder serviceBuilder;
private Duration renewalDays;
private Period renewalPeriod;

private Builder(UserContact userContact) {
this.userContact = userContact;
Expand All @@ -53,12 +53,18 @@ public Builder subscribe(Service service) {

public Builder subscribeAll(Collection<Service> services) {
Objects.requireNonNull(services, "services must not be null");
if (services.isEmpty()) {
throw new IllegalArgumentException("services must not be empty");
}
this.services.addAll(services);
return this;
}

public Builder renewIn(Duration renewalDays) {
this.renewalDays = renewalDays;
public Builder renewInDays(int days) {
if (days < 1) {
throw new IllegalArgumentException("renewal period given in days must be positive");
}
this.renewalPeriod = Period.ofDays(days);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String getName() {
}

public Optional<LocalDateTime> getResetTimestamp() {
return resetTimestamp != null ? Optional.of(resetTimestamp.toLocalDateTime()) : Optional.empty();
return Optional.ofNullable(resetTimestamp).map(ZonedDateTime::toLocalDateTime);
}

public boolean isRenewableUsageLimit() {
Expand All @@ -51,8 +51,8 @@ public double getConsumption() {

private static void validateUsageLevel(String name, double consumed) {
Objects.requireNonNull(name, "usage limit name must not be null");
if (consumed <= 0) {
throw new IllegalArgumentException("consumption must be greater than 0");
if (consumed < 0) {
throw new IllegalArgumentException("usage level consumption must be positive");
}
}

Expand All @@ -76,4 +76,13 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(name, consumed, resetTimestamp);
}

@Override
public String toString() {
return "UsageLevel{" +
"name='" + name + '\'' +
", consumed=" + consumed +
", resetTimestamp=" + resetTimestamp +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ private UserContact(Builder builder) {
this.phone = builder.phone;
}

public static Builder builder(String userId, String username) {
Objects.requireNonNull(userId, "userId must not be null");
Objects.requireNonNull(username, "username must not be null");
return new Builder(userId, username);
}

public String getUserId() {
return userId;
}
Expand All @@ -51,35 +45,34 @@ public Optional<String> getPhone() {
return Optional.ofNullable(phone);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
UserContact that = (UserContact) o;
return Objects.equals(userId, that.userId) && Objects.equals(username, that.username);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
return Objects.hash(userId, username);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserContact other = (UserContact) obj;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
public String toString() {
return "UserContact{" +
"userId='" + userId + '\'' +
", username='" + username + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}

public static Builder builder(String userId, String username) {
Objects.requireNonNull(userId, "userId must not be null");
Objects.requireNonNull(username, "username must not be null");
return new Builder(userId, username);
}

public static class Builder {
Expand Down Expand Up @@ -145,7 +138,7 @@ public enum Keys {

private final String name;

private Keys(String name) {
Keys(String name) {
this.name = name;
}

Expand Down
Loading