/sitebricks/src/main/java/com/google/sitebricks/ActionDescriptor.java

http://github.com/dhanji/sitebricks · Java · 94 lines · 72 code · 17 blank · 5 comment · 10 complexity · 2d538bc8983a3144b331c31d91754982 MD5 · raw file

  1. package com.google.sitebricks;
  2. import com.google.common.base.Preconditions;
  3. import com.google.common.collect.Maps;
  4. import com.google.common.collect.Sets;
  5. import com.google.inject.Key;
  6. import com.google.sitebricks.routing.Action;
  7. import java.lang.annotation.Annotation;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import java.util.regex.Pattern;
  11. /**
  12. * Describes an action binding in the SPI for actions.
  13. * @author dhanji@gmail.com (Dhanji R. Prasanna)
  14. */
  15. public class ActionDescriptor implements PageBinder.ActionBinder {
  16. private final Action action;
  17. private final Key<? extends Action> actionKey;
  18. private final Map<String, String> selectParams = Maps.newHashMap();
  19. private final Map<String, String> selectHeaders = Maps.newHashMap();
  20. private final Set<Class<? extends Annotation>> methods = Sets.newHashSet();
  21. // Pass thru for builder config.
  22. private final PageBinder.PerformBinder performBinder;
  23. public ActionDescriptor(Action action, PageBinder.PerformBinder performBinder) {
  24. this.action = action;
  25. this.performBinder = performBinder;
  26. this.actionKey = null;
  27. }
  28. public ActionDescriptor(Key<? extends Action> action, PageBinder.PerformBinder performBinder) {
  29. this.performBinder = performBinder;
  30. this.action = null;
  31. this.actionKey = action;
  32. }
  33. @Override
  34. public PageBinder.PerformBinder on(Class<? extends Annotation>... method) {
  35. Preconditions.checkArgument(null != method && method.length > 0,
  36. "Must specify at least one method");
  37. methods.addAll(Sets.newHashSet(method));
  38. return performBinder;
  39. }
  40. @Override
  41. public PageBinder.ActionBinder select(String param, String value) {
  42. Preconditions.checkArgument(param != null && !param.isEmpty(),
  43. "Parameter to select() must be a non-empty string");
  44. Preconditions.checkArgument(value != null && !value.isEmpty(),
  45. "Value to select() must be a non-empty string");
  46. selectParams.put(param, value);
  47. return this;
  48. }
  49. @Override
  50. public PageBinder.ActionBinder selectHeader(String header, String value) {
  51. Preconditions.checkArgument(header != null && !header.isEmpty(),
  52. "Header to selectHeader() must be a non-empty string");
  53. Preconditions.checkArgument(value != null && !value.isEmpty(),
  54. "Value to selectHeader() must be a non-empty string");
  55. selectHeaders.put(header, value);
  56. return this;
  57. }
  58. @Override
  59. public PageBinder.ActionBinder selectHeader(String param, Pattern regex) {
  60. throw new UnsupportedOperationException("To be implemented");
  61. }
  62. public Action getAction() {
  63. return action;
  64. }
  65. public Key<? extends Action> getActionKey() {
  66. return actionKey;
  67. }
  68. public Map<String, String> getSelectParams() {
  69. return selectParams;
  70. }
  71. public Map<String, String> getSelectHeaders() {
  72. return selectHeaders;
  73. }
  74. public Set<Class<? extends Annotation>> getMethods() {
  75. return methods;
  76. }
  77. }