Skip to content

Instantly share code, notes, and snippets.

View mdread's full-sized avatar

Daniel Camarda mdread

View GitHub Profile
@mdread
mdread / mkString.java
Created July 23, 2012 16:53
make a string from an iterator or array using an optional separator and prefix-suffix values
public static String mkString(Iterable<?> values, String start, String sep, String end){
// if the array is null or empty return an empty string
if(values == null || !values.iterator().hasNext())
return "";
// move all non-empty values from the original array to a new list (empty is a null, empty or all-whitespace string)
List<String> nonEmptyVals = new LinkedList<String>();
for (Object val : values) {
if(val != null && val.toString().trim().length() > 0){
nonEmptyVals.add(val.toString());
@mdread
mdread / AppContext.java
Created July 25, 2012 12:44
get access to the Spring application context from a non-managed bean
import org.springframework.context.ApplicationContext;
public class AppContext {
private static ApplicationContext context;
public static void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
@mdread
mdread / gist:3176035
Created July 25, 2012 12:54
unescape characters encoded in the form "&#3456;"
function unescapeSpecialChars(str){
var result = str;
var pattern = /\&#\d{1,4};/g;
var arrMatch = null;
while (arrMatch = pattern.exec( str )){
result = result.replace(arrMatch[0], String.fromCharCode(/\&#(\d{1,4});/.exec(arrMatch[0])[1]));
}
return result;
}
@mdread
mdread / gist:3176970
Created July 25, 2012 16:06
some useful solr REST commands
#reload core
curl {server-name}/admin/cores -H "Content-Type: text/xml" -d "action=RELOAD" -d "core={core-name}" -G
#empty core
curl {server-name}/{core-name}/update -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>'
curl {server-name}/{core-name}/update -H "Content-Type: text/xml" --data-binary '<commit />'
curl {server-name}/{core-name}/update -H "Content-Type: text/xml" --data-binary '<optimize />'
@mdread
mdread / EscapeSpecialChars.java
Created July 25, 2012 16:17
escape characters that are outside of the specified range in the form &#digits;
public class EscapeSpecialChars {
public static final char HEXA = 'H';
public static final char DECIMAL = 'D';
private int minRange;
private int maxRange;
private String prefixEscape;
private String suffixEscape;
private String exclusionCharList;
private char formatMethod;
@mdread
mdread / gist:3348152
Created August 14, 2012 10:53
Extract all non empty child text nodes
def extractNonEmptyTextChilds(n: Node): String = n map { e => e.child.filter({f => f.text.trim() != "" && f.label == "#PCDATA"}) text } mkString
@mdread
mdread / elvis.scala
Created October 22, 2012 00:05
An Elvis operator aware of empty strings
object ElvisOP {
implicit def any2elvis(x: Any) = new ElvisOP(Option(x))
}
class ElvisOP(me: Option[Any]) {
def ?> (that: Any) = me match{
case Some(s: String) => s match {
case _ if s.isEmpty() => that
case _ => s
}
case Some(b: Boolean) => if(!b) that else b
@mdread
mdread / gist:4110880
Created November 19, 2012 14:18
function zipAll for iterables
public static <A, B> Iterable<Pair<A, B>> zipAll(Iterable<A> iter1, Iterable<B> iter2, A def1, B def2){
List<Pair<A, B>> zipped = new ArrayList<TitleParser.Pair<A,B>>();
Iterator<A> these = iter1.iterator();
Iterator<B> those = iter2.iterator();
while(these.hasNext() && those.hasNext()){
zipped.add(new Pair<A, B>(these.next(), those.next()));
}
while(these.hasNext()){
zipped.add(new Pair<A, B>(these.next(), def2));
}
@mdread
mdread / AccessCondition.java
Last active December 15, 2015 13:09
Spring interceptor for conditional access to a request action
package net.caoticode.spring.interceptors.conditional;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
public interface AccessCondition {
boolean test(HttpServletRequest request, ApplicationContext context);
}
@mdread
mdread / DateConverter.java
Last active December 16, 2015 12:49
convert between string rappresentations of dates. example: from("yyyy-MM-dd").to("dd/MM/yyyy").convert("1987-12-01"); // results in 01/12/1987
import java.text.ParseException;
import java.text.SimpleDateFormat;
public final class DateConverter {
private SimpleDateFormat formatterFrom;
private SimpleDateFormat formatterTo;
private DateConverter(){
super();
}