Skip to content

Instantly share code, notes, and snippets.

View collinvandyck's full-sized avatar
🏠
Working from home

Collin Van Dyck collinvandyck

🏠
Working from home
View GitHub Profile
/**
* 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));
}
// 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() {
<T> T wrap(Callable<T> callback) throws Exception {
// do stuff
try {
return callback.call()
} finally {
// cleanup
}
}
This is a test.
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;
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
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);
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:"-"`
}
bytes, err := json.Marshal(user)
if err != nil {
http.Error(w, err.Error(), Unauthorized)
return
}
w.Write(bytes)
@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);
}