Skip to content

Instantly share code, notes, and snippets.

@newfront
Created December 11, 2016 20:00
Show Gist options
  • Save newfront/935f7f9d67953949bcc777e9681a85cb to your computer and use it in GitHub Desktop.
Save newfront/935f7f9d67953949bcc777e9681a85cb to your computer and use it in GitHub Desktop.
SecretSanta
public class SecretSantaSelection {
private final long phoneNumber;
private final String message;
public SecretSantaSelection(final long phoneNumber, final String message) {
this.phoneNumber = phoneNumber;
this.message = message;
}
public long getPhoneNumber() {
return phoneNumber;
}
public String getMessage() {
return message;
}
}
public static List<SecretSantaSelection> generateSecretSantaList() {
final List<String> allFolks = Arrays.asList("phil", "melissa", "lacey", "meredith");
final Map<String, Boolean> people = new HashMap<>();
people.put("phil", false);
people.put("melissa", false);
people.put("lacey", false);
people.put("meredith", false);
final Map<String, Long> phoneNumbers = new HashMap<>();
phoneNumbers.put("phil", 7137037691L);
phoneNumbers.put("melissa", 4128418334L);
phoneNumbers.put("meredith", 6177946733L);
phoneNumbers.put("lacey", 4088230751L);
final Set<String> secretSantaSelections = new HashSet<>();
final Map<String, String> selections = new HashMap<>();
boolean filled = false;
while (!filled) {
final Optional<String> nextSelection = people.entrySet().parallelStream()
.filter(entry -> !people.get(entry.getKey())) // ensure this person hasn't received a secret santa yet
.map(Map.Entry::getKey)
.findAny();
if (nextSelection.isPresent()) {
final String selection = nextSelection.get();
boolean foundSelection = false;
while (!foundSelection) {
Collections.shuffle(allFolks);
final Optional<String> secretSantaSelection = allFolks.stream()
.filter(secretSanta -> !secretSanta.equals(selection))
.filter(secretSanta -> !secretSantaSelections.contains(secretSanta))
//.filter(secretSanta -> !couples.get(selection).equals(secretSanta))
.findAny();
if (secretSantaSelection.isPresent()) {
secretSantaSelections.add(secretSantaSelection.get()); // selected a match
selections.put(selection, secretSantaSelection.get());
people.put(selection, true);
foundSelection = true;
}
}
} else {
filled = true;
}
}
final Map<String, String> couplesNames = new HashMap<>();
couplesNames.put("phil", "Roxanne and Phil");
couplesNames.put("melissa", "Matt and Melissa");
couplesNames.put("lacey", "Scott and Lacey");
couplesNames.put("meredith", "Meredith");
final String secretSantaMessage = "Dear %s. Your secret santa selection for this christmas is %s. The total budget is $100";
return selections.entrySet().stream().map(entry -> {
final String coupleSending = couplesNames.get(entry.getKey());
final String coupleReceiving = couplesNames.get(entry.getValue());
final String message = String.format(secretSantaMessage, coupleSending, coupleReceiving);
final long phoneNumber = phoneNumbers.get(entry.getKey());
return new SecretSantaSelection(phoneNumber, message);
}).collect(Collectors.toList());
}
@GET
@Timed
@Path("/sms/test")
@Produces(MediaType.APPLICATION_JSON)
public Response runSecretSanta(final @QueryParam("to") String toNumber, final @QueryParam("message") String message) {
final String to = "+1" + toNumber;
final String from = "+14087676479";
final Message sms = Message
.creator(new PhoneNumber(to), new PhoneNumber(from), message)
.setMediaUrl("https://farm8.staticflickr.com/7402/11570925325_fdbe0b3f78_z_d.jpg")
.create();
logger.info("smsSent: sid={}", sms.getSid());
return Response.ok().entity("{\"id\": " + sms.getSid() + "}").build();
}
@GET
@Timed
@Path("/secretsanta")
@Produces(MediaType.APPLICATION_JSON)
public Response secretSanta() {
final List<SecretSantaSelection> selections = generateSecretSantaList();
selections.forEach(selection -> runSecretSanta(String.valueOf(selection.getPhoneNumber()), selection.getMessage()));
return Response.ok().entity("{\"status\": \"complete\"}").build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment