Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
fuxingloh / 0.0.0.migration.sql
Last active August 29, 2015 14:24
AutoMigration From PuffinCore
-- The first statement you need to run in your sql to setup migration.
CREATE TABLE Versioning (
migrated VARCHAR(255) NOT NULL,
appliedDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (migrated));
INSERT INTO Versioning (migrated) VALUES ('0.0.0.migration.sql');
@fuxingloh
fuxingloh / DateUtil.java
Last active August 29, 2015 14:24
DateUtil From Puffin Core, and stackoverflow Or you could just use DateUtils from apache library, it is much better
// Date Time Utils
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
@fuxingloh
fuxingloh / HttpGet.java
Last active August 29, 2015 14:24
Http Get/Post with Apache Utils
default String connect(String url) throws IOException {
try (CloseableHttpClient client = HttpClients.createDefault()){
// Create http post
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
return EntityUtils.toString(response.getEntity());
}
}
@fuxingloh
fuxingloh / RandomLatLngUtils.java
Last active January 27, 2024 17:58
Generate random lat lng
import co.threelines.puffin.util.GeoSpatialUtil;
import com.spatial4j.core.shape.Point;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Random;
/**
* Created by Fuxing
* Date: 5/7/2015
* Time: 3:22 AM
@fuxingloh
fuxingloh / java8.sh
Last active July 12, 2017 16:26
Install Java 8 on AWS IAM
sudo yum install java-1.8.0
sudo yum remove java-1.7.0-openjdk
@fuxingloh
fuxingloh / Jackson.java
Last active July 11, 2017 18:03
Jackson
ObjectMapper mapper = new ObjectMapper()
// Via class
Hey hey = mapper.readValue("{name:\"hey\"}", Hey.class);
System.out.println(hey.name);
// Via dynamic
JsonNode node = mapper.readTree("{name:\"hey\"}");
// With null safety, allows chaining
String name = node.path("name").asText();
@fuxingloh
fuxingloh / FastImage.java
Last active August 29, 2015 14:24
Java FastImage, finds the size or type of an image given its url by fetching as little as needed.
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;
@fuxingloh
fuxingloh / CamelCaseString.swift
Last active January 9, 2017 13:23
Camel case string extension for swift
extension String {
// Camel case
var camelCased: String {
return self.characters.split{$0 == " "}.map(String.init).map{$0.capitalizedString}.joinWithSeparator(" ")
}
var capitalizeFirst:String {
var result = self
result.replaceRange(startIndex...startIndex, with: String(self[startIndex]).capitalizedString)
return result
@fuxingloh
fuxingloh / UILabelCountLines.swift
Last active October 28, 2023 22:36
iOS Swift: How to check if UILabel is truncated? Calculate number of lines for UILabel
func countLabelLines(label: UILabel) -> Int {
// Call self.layoutIfNeeded() if your view uses auto layout
let myText = label.text! as NSString
let rect = CGSize(width: label.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let labelSize = myText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil)
return Int(ceil(CGFloat(labelSize.height) / label.font.lineHeight))
}
@fuxingloh
fuxingloh / ColorUtils.swift
Created November 15, 2015 06:26
Swift: Blend 2 Color Together
class func blendColor(color1: UIColor, withColor color2: UIColor) -> UIColor {
var r1:CGFloat = 0, g1:CGFloat = 0, b1:CGFloat = 0, a1:CGFloat = 0
var r2:CGFloat = 0, g2:CGFloat = 0, b2:CGFloat = 0, a2:CGFloat = 0
color1.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
color2.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
return UIColor(red: max(r1, r2), green: max(g1, g2), blue: max(b1, b2), alpha: max(a1, a2))
}