Skip to content

Instantly share code, notes, and snippets.

@oharsta
oharsta / StopWatchAdvice.java
Created March 31, 2016 15:56
StopWatch - without AspectJ - for every request (do not use in production)
package aa.web;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.filter.OncePerRequestFilter;
@oharsta
oharsta / JsonMapper.java
Created March 27, 2016 15:46
Inject singleton instance into Java (+8) classes
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
public interface JsonMapper {
ObjectMapper mapper = ObjectMapperWrapper.init();
class ObjectMapperWrapper {
private static com.fasterxml.jackson.databind.ObjectMapper init() {
@oharsta
oharsta / AngularNonLatin1
Created January 31, 2015 06:15
Angular Validate non latin-1 chars
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form latin-1 input validation</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<script type="text/javascript">
(function(angular) {
'use strict';
'use strict';
angular.module('portal')
.factory('PrefixCalculator', function () {
/**
* Sums up the prefixLengths given a specified bitSize
*
* @param prefixes prefixLength integers (e.g. 22, 39 etc)
* @param bitSize the bitSize of the IP (e.g. 32 or 128)
@oharsta
oharsta / IPvXPrefixCalculator.scala
Last active August 29, 2015 14:07
Calculate the addition of IPv4 and IPv6 prefix lengths (e.g. a IPv4 /23 and a /23 result in a /22)
object PrefixCalculator {
def sumIpv4(ipv4Prefixes: Seq[Int]) : Int = doSumIpvX(ipv4Prefixes, 32)
def sumIpv6(ipv6Prefixes: Seq[Int]) : Int = doSumIpvX(ipv6Prefixes, 128)
private def doSumIpvX(prefixes: Seq[Int], bitSize: Int) : Int = {
val addressSize = prefixes.map(prefix => Math.pow(2, bitSize - prefix)).sum
(bitSize - (Math.log(addressSize) / Math.log(2))).floor.toInt
}