Skip to content

Commit 9c0db66

Browse files
authored
Migrate to using enums for Smartrate delivery accuracy choices (#152)
- New SmartrateAccuracy enum (similar to C# library) - New functions to use SmartrateAccuracy when dealing with smartrates - Deprecate old smartrate functions using strings
1 parent c6625c6 commit 9c0db66

File tree

4 files changed

+101
-28
lines changed

4 files changed

+101
-28
lines changed

src/main/java/com/easypost/model/Shipment.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import com.easypost.exception.EasyPostException;
44
import com.easypost.net.EasyPostResource;
55

6-
import java.util.Arrays;
76
import java.util.HashMap;
8-
import java.util.HashSet;
97
import java.util.List;
108
import java.util.Map;
119

@@ -1035,8 +1033,22 @@ public Shipment insure(final String apiKey) throws EasyPostException {
10351033
* @param deliveryAccuracy Delivery days accuracy restriction to use when filtering.
10361034
* @return lowest Smartrate object
10371035
* @throws EasyPostException when the request fails.
1036+
* @deprecated use {@link #lowestSmartRate(int, SmartrateAccuracy)} instead.
10381037
*/
1038+
@Deprecated
10391039
public Smartrate lowestSmartRate(int deliveryDay, String deliveryAccuracy) throws EasyPostException {
1040+
return this.lowestSmartRate(deliveryDay, SmartrateAccuracy.getByKeyName(deliveryAccuracy));
1041+
}
1042+
1043+
/**
1044+
* Get the lowest smartrate for this Shipment.
1045+
*
1046+
* @param deliveryDay Delivery days restriction to use when filtering.
1047+
* @param deliveryAccuracy Delivery days accuracy restriction to use when filtering.
1048+
* @return lowest Smartrate object
1049+
* @throws EasyPostException when the request fails.
1050+
*/
1051+
public Smartrate lowestSmartRate(int deliveryDay, SmartrateAccuracy deliveryAccuracy) throws EasyPostException {
10401052
List<Smartrate> smartrates = this.getSmartrates();
10411053

10421054
Smartrate lowestSmartrate = findLowestSmartrate(smartrates, deliveryDay, deliveryAccuracy);
@@ -1074,12 +1086,12 @@ public List<Smartrate> smartrates() throws EasyPostException {
10741086
* @param deliveryAccuracy Delivery days accuracy restriction to use when filtering.
10751087
* @return lowest Smartrate object
10761088
* @throws EasyPostException when the request fails.
1077-
* @deprecated Use {@link #findLowestSmartrate(List, int, String)} instead.
1089+
* @deprecated Use {@link #findLowestSmartrate(List, int, SmartrateAccuracy)} instead.
10781090
*/
10791091
@Deprecated
10801092
public static Smartrate getLowestSmartRate(final List<Smartrate> smartrates, int deliveryDay,
10811093
String deliveryAccuracy) throws EasyPostException {
1082-
return findLowestSmartrate(smartrates, deliveryDay, deliveryAccuracy);
1094+
return findLowestSmartrate(smartrates, deliveryDay, SmartrateAccuracy.getByKeyName(deliveryAccuracy));
10831095
}
10841096

10851097
/**
@@ -1092,19 +1104,11 @@ public static Smartrate getLowestSmartRate(final List<Smartrate> smartrates, int
10921104
* @throws EasyPostException when the request fails.
10931105
*/
10941106
public static Smartrate findLowestSmartrate(final List<Smartrate> smartrates, int deliveryDay,
1095-
String deliveryAccuracy) throws EasyPostException {
1107+
SmartrateAccuracy deliveryAccuracy) throws EasyPostException {
10961108
Smartrate lowestSmartrate = null;
10971109

1098-
HashSet<String> validDeliveryAccuracies = new HashSet<String>(
1099-
Arrays.asList("percentile_50", "percentile_75", "percentile_85", "percentile_90", "percentile_95",
1100-
"percentile_97", "percentile_99"));
1101-
1102-
if (!validDeliveryAccuracies.contains(deliveryAccuracy.toLowerCase())) {
1103-
throw new EasyPostException("Invalid delivery_accuracy value, must be one of: " + validDeliveryAccuracies);
1104-
}
1105-
11061110
for (Smartrate rate : smartrates) {
1107-
int smartrateDeliveryDay = rate.getTimeInTransit().getSmartRateAccuracy(deliveryAccuracy);
1111+
int smartrateDeliveryDay = rate.getTimeInTransit().getBySmartrateAccuracy(deliveryAccuracy);
11081112

11091113
if (smartrateDeliveryDay > deliveryDay) {
11101114
continue;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.easypost.model;
2+
3+
public enum SmartrateAccuracy {
4+
Percentile50("percentile_50"),
5+
Percentile75("percentile_75"),
6+
Percentile85("percentile_85"),
7+
Percentile90("percentile_90"),
8+
Percentile95("percentile_95"),
9+
Percentile97("percentile_97"),
10+
Percentile99("percentile_99");
11+
12+
private final String keyName;
13+
14+
/**
15+
* Constructor.
16+
*
17+
* @param keyName the internal key name
18+
*/
19+
SmartrateAccuracy(String keyName) {
20+
this.keyName = keyName;
21+
}
22+
23+
/**
24+
* Get the internal key name for this enum value.
25+
*
26+
* @return the internal key name
27+
*/
28+
public String getKeyName() {
29+
return keyName;
30+
}
31+
32+
/**
33+
* Get the enum value for a given internal key name.
34+
*
35+
* @param keyName the internal key name
36+
* @return the enum value
37+
* @throws IllegalArgumentException if the key name is not found
38+
*/
39+
public static SmartrateAccuracy getByKeyName(String keyName) throws IllegalArgumentException {
40+
for (SmartrateAccuracy smartrateAccuracy : values()) {
41+
if (smartrateAccuracy.getKeyName().equals(keyName)) {
42+
return smartrateAccuracy;
43+
}
44+
}
45+
throw new IllegalArgumentException("Invalid SmartrateAccuracy key name.");
46+
}
47+
}

src/main/java/com/easypost/model/TimeInTransit.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ public void setPercentile99(final Integer percentile99) {
152152
* @param percentile the percentile to find the corresponding accuracy for
153153
* @return the delivery accuracy of the specified percentile
154154
* @throws EasyPostException when the percentile is not valid
155+
* @deprecated Use {@link #getBySmartrateAccuracy(SmartrateAccuracy)} instead.
155156
*/
157+
@Deprecated
156158
public int getSmartRateAccuracy(final String percentile) throws EasyPostException {
157159
switch (percentile) {
158160
case "percentile_50":
@@ -173,4 +175,33 @@ public int getSmartRateAccuracy(final String percentile) throws EasyPostExceptio
173175
throw new EasyPostException("Invalid percentile value");
174176
}
175177
}
178+
179+
/**
180+
* Get the delivery accuracy of a specific percentile of this TimeInTransit.
181+
*
182+
* @param accuracy the SmartrateAccuracy to find the corresponding accuracy for
183+
* @return the delivery accuracy of the specified percentile
184+
* @throws EasyPostException when the percentile is not valid
185+
*/
186+
public int getBySmartrateAccuracy(SmartrateAccuracy accuracy) throws EasyPostException {
187+
switch (accuracy) {
188+
case Percentile50:
189+
return this.percentile50;
190+
case Percentile75:
191+
return this.percentile75;
192+
case Percentile85:
193+
return this.percentile85;
194+
case Percentile90:
195+
return this.percentile90;
196+
case Percentile95:
197+
return this.percentile95;
198+
case Percentile97:
199+
return this.percentile97;
200+
case Percentile99:
201+
return this.percentile99;
202+
default:
203+
throw new EasyPostException("Invalid SmartrateAccuracy enum value.");
204+
}
205+
}
206+
176207
}

src/test/java/com/easypost/ShipmentTest.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.easypost.model.Shipment;
88
import com.easypost.model.ShipmentCollection;
99
import com.easypost.model.Smartrate;
10+
import com.easypost.model.SmartrateAccuracy;
1011
import org.junit.jupiter.api.BeforeAll;
1112
import org.junit.jupiter.api.Disabled;
1213
import org.junit.jupiter.api.Test;
@@ -384,7 +385,7 @@ public void testInstanceLowestSmartRate() throws EasyPostException {
384385
vcr.setUpTest("lowest_smartrate");
385386

386387
Shipment shipment = createBasicShipment();
387-
Smartrate lowestSmartRateFilters = shipment.lowestSmartRate(1, "percentile_90");
388+
Smartrate lowestSmartRateFilters = shipment.lowestSmartRate(1, SmartrateAccuracy.Percentile90);
388389

389390
// Test lowest smartrate with valid filters
390391
assertEquals("First", lowestSmartRateFilters.getService());
@@ -393,12 +394,7 @@ public void testInstanceLowestSmartRate() throws EasyPostException {
393394

394395
// Test lowest smartrate with invalid filters (should error due to strict delivery days)
395396
assertThrows(EasyPostException.class, () -> {
396-
shipment.lowestSmartRate(0, "percentile_90");
397-
});
398-
399-
// Test lowest smartrate with invalid filters (should error due to invalid delivery accuracy)
400-
assertThrows(EasyPostException.class, () -> {
401-
shipment.lowestSmartRate(1, "BAD ACCURACY");
397+
shipment.lowestSmartRate(0, SmartrateAccuracy.Percentile90);
402398
});
403399
}
404400

@@ -415,19 +411,14 @@ public void testStaticLowestSmartRates() throws EasyPostException {
415411
List<Smartrate> smartrates = shipment.smartrates();
416412

417413
// Test lowest smartrate with valid filters
418-
Smartrate lowestSmartRate = Shipment.findLowestSmartrate(smartrates, 1, "percentile_90");
414+
Smartrate lowestSmartRate = Shipment.findLowestSmartrate(smartrates, 1, SmartrateAccuracy.Percentile90);
419415
assertEquals("First", lowestSmartRate.getService());
420416
assertEquals(5.49, lowestSmartRate.getRate(), 0.01);
421417
assertEquals("USPS", lowestSmartRate.getCarrier());
422418

423419
// Test lowest smartrate with invalid filters (should error due to strict delivery days)
424420
assertThrows(EasyPostException.class, () -> {
425-
Shipment.findLowestSmartrate(smartrates, 0, "percentile_90");
426-
});
427-
428-
// Test lowest smartrate with invalid filters (should error due to bad delivery accuracy)
429-
assertThrows(EasyPostException.class, () -> {
430-
Shipment.findLowestSmartrate(smartrates, 1, "BAD ACCURACY");
421+
Shipment.findLowestSmartrate(smartrates, 0, SmartrateAccuracy.Percentile90);
431422
});
432423
}
433424
}

0 commit comments

Comments
 (0)