Skip to content

Instantly share code, notes, and snippets.

View adohe-zz's full-sized avatar

TonyAdo adohe-zz

View GitHub Profile
@adohe-zz
adohe-zz / gist:7197688
Created October 28, 2013 14:30
NetWork layer problem
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
Utility.log("statuscode", response.statusCode);
try {
if(response.statusCode != 200) {
return Response.error(new VolleyError("Network Error"));
} else {
String data = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
Utility.log("data", data);
T list = mGson.fromJson(data, new TypeToken<T>(){}.getType());
@adohe-zz
adohe-zz / gist:8950691
Created February 12, 2014 05:56
mkdir recursion in node
var path = require('path');
var fs = require('fs');
var mkdirs = function(dir, mode, callback) {
fs.exists(dir, function(exists) {
if(exists) {
callback(dir);
} else {
mkdirs(path.dirname(dir), mode, function() {
fs.mkdir(dir, mode, callback);
@adohe-zz
adohe-zz / reverse
Last active August 29, 2015 13:56
different implementations of string reverse in Java
public String reverse(String str) {
if(str == null)
throw new NullPointerException("null");
if(str.length() <= 1)
return str;
return new StringBuffer(str).reverse().toString();
}
public String reverseTwo(String str) {
@adohe-zz
adohe-zz / singleton
Created February 15, 2014 16:27
use case of singleton class in Java
public class ApplicationCache {
//NO Lazy Initialization
private static final ApplicationCache INSTANCE = new ApplicationCache();
//Check the INSTANCE to avoid the reflect call
private ApplicationCache() {
if(INSTANCE != null)
throw new IllegalStateException("Exist Already");
}
@adohe-zz
adohe-zz / singleton_js
Created February 15, 2014 16:40
singleton implementation in JavaScript
var app = function() {
//Private stuff
var instance;
//Init method just like a constructor
function init() {
//Return an empty object
reutn {};
}
@adohe-zz
adohe-zz / EventLoop
Created February 15, 2014 16:57
node.js event loop
var fs = require('fs');
setTimeout(function() {
console.log('first timer...');
setTimeout(function() {
console.log('second timer...');
}, 0);
@adohe-zz
adohe-zz / gist:9029896
Created February 16, 2014 05:59
node.js event loop explanation
The event loop cycle is timers -> I/O -> immediates, rinse and repeat. But that when you
haven't entered the event loop, then timers come first-but only on the first tick. timers
are based on a time in the future, even if it's 0, while check immediate is always on the
next turn of the loop. So it's possible that the delay of the event loop is low enough for
the timer to fire after the immediate.
setTimeout(function() {
console.log('setTimeout');
}, 0);
setImmediate(function() {
@adohe-zz
adohe-zz / gist:9036429
Created February 16, 2014 16:01
The diamond problem
The "diamond problem" (sometimes referred to as the "deadly diamond of death"[4]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
@adohe-zz
adohe-zz / lazy
Created February 16, 2014 16:26
Lazy initialization
Lazy initialization has two objectives:
1. delay an expensive operation until it's absolutely necessary
2. store the result of that expensive operation, such that you won't need to repeat it again.
To avoid a NullPointerException, a class must self-encapsulate fields that have lazy initialization.
That is, a class cannot refer directly to such fields, but must access them through a method.
The hashCode method of an immutable Model Object is a common candidate for lazy initialization.
public final class Lazyer {
@adohe-zz
adohe-zz / LeftShift
Created February 17, 2014 06:17
left shift string implementations in Java
//One implementation that not consider the
//time and space complexity and use string
//as input
public final class LeftShift {
//brute-force algorithm
private String leftShiftOne(String s) {
if(s == null)
throw new NullPointerException("null");
if(s.length() <= 1)