Skip to content

Instantly share code, notes, and snippets.

View sscovil's full-sized avatar

Shaun Scovil sscovil

View GitHub Profile
public static String mapToString(List<String> list, String separator, String encapsulator) {
StringBuilder stringBuilder = new StringBuilder();
for (String item : list) {
if (stringBuilder.length() > 0) stringBuilder.append(separator);
stringBuilder.append(String.format("%1$s%2$s%1$s", encapsulator, item));
}
return stringBuilder.toString();
}
/**
* List To HashMap
*
* Converts a list of key/value pairs (in the form of String arrays) to a HashMap with values merged based on key.
*
* For example, if given the following list of arrays:
*
* [0] => { [0] => "Project A", [1] => "Asset 1" },
* [1] => { [0] => "Project A", [1] => "Asset 2" },
* [2] => { [0] => "Project B", [1] => "Asset 3" }
<?php
/**
* Is Valid Timezone ID
*
* @param string $timezone_id Timezone ID to validate.
*
* @return bool True if timezone ID is valid; otherwise false.
*/
function is_valid_timezone_id( $timezone_id ){
<?php
private function map( $entity, $model ) {
$mapped = $entity;
foreach ( $entity as $key => $value ) {
if ( is_object( $entity ) ) {
if ( is_object( $value ) || is_array( $value ) )
$mapped->$key = $this->map( $value, $model );
else if ( property_exists( $model, $value ) )
@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;
@sscovil
sscovil / Asset.java
Created February 3, 2014 17:31
This is the correct implementation of @JsonTypeInfo and related annotations when performing polymorphic JSON deserialization on an embedded interface property. It also illustrates the use of an Enum as a type name. See StackOverflow question: http://stackoverflow.com/questions/21485923/java-jackson-polymorphic-json-deserialization-of-an-object-w…
public class Asset {
private AssetType type;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = ImageAssetProperties.class, name = "image"),
@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 {
/**