Skip to content

Instantly share code, notes, and snippets.

View sscovil's full-sized avatar

Shaun Scovil sscovil

View GitHub Profile
@sscovil
sscovil / PolymorphicDeserialization.java
Created January 31, 2014 16:44
This is the incorrect way to perform polymorphic JSON deserialization using Jackson's ObjectMapper. The correct way can be seen here: https://gist.github.com/sscovil/8788339. This gist was created for the following StackOverflow question: http://stackoverflow.com/questions/21485923/java-jackson-polymorphic-json-deserialization-of-an-object-with-…
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.annotate.JsonTypeName;
import org.codehaus.jackson.map.ObjectMapper;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "uuid")
private String uuid;
@sscovil
sscovil / enumStrategyPatternTest.java
Last active August 29, 2015 13:57
This gist illustrates implementation of the strategy pattern using an enum.
import org.apache.commons.lang.math.RandomUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
public class enumStrategyPatternTest() {
public enum Strategy {
ADD {
@Override
public int execute(int a, int b) {
public class DateTimeUtils {
public static final int SECONDS_IN_DAY = 86400;
public static long currentTimeInSecondsGMT() {
return System.currentTimeMillis()/1000;
}
public static long lastMidnightTimeInSecondsGMT() {
return (currentTimeInSecondsGMT() / SECONDS_IN_DAY) * SECONDS_IN_DAY;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SqlStringUtility {
/**
@sscovil
sscovil / Required.java
Last active August 29, 2015 13:57
Java Object Validation via Annotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Use this annotation to mark properties of a class as required in one or more validation contexts (e.g. CREATE,
* UPDATE), then validate instances of that class using the static Validator.validate(object, context) method.
*/
@Target(ElementType.FIELD)
@sscovil
sscovil / functions.php
Created May 14, 2014 02:52
WordPress function to use with WP-Members plugin. Replaces the post index with a login page. To use, create a page and add the shortcode `[wp-members page="login"] ` to it, then get the ID of that page and put it on line 7 as the value of `page_id`.
<?php
//* Replace post index with login page for non-members
function premium_posts_05132014sms( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
if ( current_user_can( 'subscriber' ) || !is_user_logged_in() ) {
$query->set( 'page_id', '12' ); // Replace 12 with the ID of your login page
$query->set( 'post_type', 'page' );
}
}
@sscovil
sscovil / 0_reuse_code.js
Created June 2, 2014 13:22
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@sscovil
sscovil / fa-classes-no-aliases.json
Last active August 29, 2015 14:02
Complete list of FontAwesome 4.1.0 font classes, in various formats.
{
"fa-classes": ["fa-adjust","fa-adn","fa-align-center","fa-align-justify","fa-align-left","fa-align-right","fa-ambulance","fa-anchor","fa-android","fa-angle-double-down","fa-angle-double-left","fa-angle-double-right","fa-angle-double-up","fa-angle-down","fa-angle-left","fa-angle-right","fa-angle-up","fa-apple","fa-archive","fa-arrow-circle-down","fa-arrow-circle-left","fa-arrow-circle-o-down","fa-arrow-circle-o-left","fa-arrow-circle-o-right","fa-arrow-circle-o-up","fa-arrow-circle-right","fa-arrow-circle-up","fa-arrow-down","fa-arrow-left","fa-arrow-right","fa-arrow-up","fa-arrows","fa-arrows-alt","fa-arrows-h","fa-arrows-v","fa-asterisk","fa-backward","fa-ban","fa-bar-chart-o","fa-barcode","fa-bars","fa-beer","fa-behance","fa-behance-square","fa-bell","fa-bell-o","fa-bitbucket","fa-bitbucket-square","fa-bold","fa-bolt","fa-bomb","fa-book","fa-bookmark","fa-bookmark-o","fa-briefcase","fa-btc","fa-bug","fa-building","fa-building-o","fa-bullhorn","fa-bullseye","fa-calendar","fa-calendar-o","fa-camera","fa
// Create Base64 Object
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r