Skip to content

Instantly share code, notes, and snippets.

View michalbcz's full-sized avatar

Michal Bernhard michalbcz

View GitHub Profile
@michalbcz
michalbcz / gist:4167914
Created November 29, 2012 09:58
groovy - connect to URL through proxy
import java.net.*;
import java.io.*;
/* PROXY SETTINGS */
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "some.proxyserver.com");
System.getProperties().put("proxyPort", "8080");
Authenticator.setDefault(new MyAuthenticator());
@michalbcz
michalbcz / gist:4170520
Created November 29, 2012 17:19
java - https url connection - trust any certificate
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
@michalbcz
michalbcz / github-issue-migrator.py
Last active December 14, 2015 08:39
Migration of issues from RHoK-November-2012/demagog-cz to michalbcz/demagog-cz. Script found through http://stackoverflow.com/questions/9720718/how-do-i-move-an-issue-on-github-to-another-repo on https://github.com/collective/collective.developermanual/blob/master/gh-issues-import.py. This version has modified username and target and destination…
#!/usr/bin/python
"""
Import Github issues from one repo to another.
The orignal work: https://github.com/mkorenkov/tools/blob/master/gh-issues-import/gh-issues-import.py
(assuming public domain license).
Used to migrate Github's Plone Conference 2012 temporary repository
to collective.developermanual issues.
@michalbcz
michalbcz / gist:5866983
Created June 26, 2013 12:24
groovy - Map#collectWithIndex implementation
Map.metaClass.collectWithIndex = { yield ->
def collected = []
delegate.eachWithIndex { key, value, index ->
collected << yield(key, value, index)
}
return collected
}
assert [a: 1, b: 1, c: 1].collectWithIndex { key, value, index -> key + value + index } == ["a10", "b11", "c12"]
@michalbcz
michalbcz / gist:6229572
Last active December 21, 2015 01:39
emulating window.console to prevent undefined errors for older browser (like IE7/8) which have no native console object or haven't got appropriate log methods
/*
* Initialize console object used for logging in javascript code.
* For case when console object is not present (old browsers) use dump implementation.
*/
window.console = window.console || {}; // in old browsers like IE7 there is no console object
console.info = console.info || function() {};
console.error = console.error || function() {};
console.warn = console.warn || function() {};
@michalbcz
michalbcz / md5.groovy
Created September 17, 2013 06:55 — forked from ikarius/md5.groovy
def generateMD5(String s) {
MessageDigest digest = MessageDigest.getInstance("MD5")
digest.update(s.bytes);
new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
}
@michalbcz
michalbcz / gist:6775946
Created October 1, 2013 09:25
eclipse template definition - adding LOG declaration to class
${import1:import(org.slf4j.Logger)}
${import2:import(org.slf4j.LoggerFactory)}
private static final Logger LOG = LoggerFactory.getLogger(${enclosing_type}.class);
import java.text.Collator;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
@michalbcz
michalbcz / StringUtils
Last active March 17, 2017 12:18
replacing characters with diacritic (national characters) with variant without diacritic eg. replace Á to A..etc.
public final class StringUtils {
private StringUtils() { /* cannot be instantiated */ }
/**
* <b>WORKS ONLY IN JAVA 1.6 AND ABOVE !!!</b>
*
* @param textWithDiacritic
*/
public static String removeDiacritics(String textWithDiacritic) {