This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Provides for nice formatting of tabular data. | |
| */ | |
| public class TextTable { | |
| private final List<Integer> maxWidths = Lists.newArrayList(); // widths for each column | |
| private final List<List<String>> rows = Lists.newArrayList(); | |
| public TextTable addRow(Iterable<? extends Object> items) { | |
| return addRow(Lists.newArrayList(items)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // we want a return value here. | |
| int i = wrap(new Callable<Integer>() { | |
| public Integer call() { | |
| return 42; | |
| } | |
| }) | |
| // if we don't have a return value, we're forced to return null | |
| wrap(new Callable<Void>() { | |
| public Void call() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <T> T wrap(Callable<T> callback) throws Exception { | |
| // do stuff | |
| try { | |
| return callback.call() | |
| } finally { | |
| // cleanup | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is a test. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Pair<U,T> { | |
| private final U left; | |
| private final T right; | |
| public static <U,T> Pair<U,T> of(U u, T t) { | |
| return new Pair<U, T>(u, t); | |
| } | |
| public Pair(U left, T right) { | |
| this.left = left; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func getDecodedAuthorizationHeader(headers http.Header) (aType string, aValue string, err error) { | |
| auth := strings.Split(headers.Get("Authorization"), " ") | |
| if len(auth) != 2 { | |
| return aType, aValue, errors.New("Bad auth header") | |
| } | |
| if auth[0] == "Basic" { | |
| reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(auth[1])) | |
| bytes, err := ioutil.ReadAll(reader) | |
| if err != nil { | |
| return aType, aValue, err |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AuthService extends Service<AuthConfiguration> { | |
| // methods elided | |
| @Override | |
| protected void initialize(AuthConfiguration config, Environment environment) throws Exception { | |
| final DatabaseFactory factory = new DatabaseFactory(environment); | |
| final Database db = factory.build(config.getDatabaseConfiguration(), "postgresql"); | |
| final UserDAO userDAO = db.onDemand(UserDAO.class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type User struct { | |
| Id string `json:"id"` | |
| Email string `json:"email,omitempty"` | |
| CreatedAt time.Time `json:"created_at"` | |
| UpdatedAt time.Time `json:"updated_at"` | |
| Name string `json:"name,omitempty"` | |
| Admin bool `json:"admin"` | |
| Active bool `json:"-"` | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bytes, err := json.Marshal(user) | |
| if err != nil { | |
| http.Error(w, err.Error(), Unauthorized) | |
| return | |
| } | |
| w.Write(bytes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
| public class User { | |
| private static final DateFormat ISO_8601_FORMATTER = new ISO8601DateFormat(); | |
| public static String dateToISO8601Format(Date date) { | |
| if (date == null) { | |
| return null; | |
| } | |
| return ISO_8601_FORMATTER.format(date); | |
| } |