PageRenderTime 79ms CodeModel.GetById 68ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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