Skip to content

Instantly share code, notes, and snippets.

@kostapc
kostapc / gist:386d5eae540bbbc45dab
Last active August 29, 2015 14:24
mock async call
package net.c0f3.random.tests;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.concurrent.atomic.AtomicInteger;
import static org.mockito.Mockito.*;
@kostapc
kostapc / java-properties.php
Created November 20, 2015 13:10 — forked from alecgorge/java-properties.php
Parse Java properties files in PHP
<?php
function parse_properties($txtProperties) {
$result = array();
$lines = split("\n", $txtProperties);
$key = "";
$isWaitingOtherLine = false;
foreach ($lines as $i => $line) {
if (empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0))
continue;
package selly.spring.data.convert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.config.BeanDefinition;
@kostapc
kostapc / CronUtilsTest.java
Last active November 8, 2016 17:47
testing CronUtils for periods
package ru.infon.azerfon.mas.module;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
<?php
$ytdlPath = '/whereever/your/youtube-dl'; //change this
ob_start();
$url = trim(rawurldecode($_SERVER['QUERY_STRING']));
$qStr = parse_url($url, PHP_URL_QUERY );
parse_str($qStr, $get);
if (!isset($get['v']) || !preg_match('~[a-zA-Z0-9_-]{11}~',$get['v']) ) {
CacheEntity<K,V> execute(CacheEntity<K,V> cacheEntity, Action action) {
long now = System.currentTimeMillis();
if(cacheEntity==null) {
return null;
}
if(cacheEntity.isExpired()) {
storage.remove(cacheEntity);
return null;
}
@kostapc
kostapc / LongNumberTruncate.java
Last active November 28, 2016 17:30
test math and string versions of long truncation
public class LongNumberTruncate {
public static void main(String[] args) {
long[] values = new long[8];
values[0] = 1234567891234567891L; // 16
values[1] = 123456789223456789L; // 15
values[2] = 123456789323456L; // 14
values[3] = 1234567894236L; // 13
values[4] = 1234567895L; // 10
@kostapc
kostapc / nginx.conf
Last active November 24, 2016 18:13 — forked from morhekil/nginx.conf
Full request/response body logging in nginx
http {
log_format bodylog '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time '
'<"$request_body" >"$resp_body"';
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua_block {
@kostapc
kostapc / SingleLatch.java
Created December 6, 2016 16:27
Single Latch - thread wait for "open" or pass through await() instantly if latch open
public class SingleLatch {
static final int STATE_OPEN = 0;
static final int STATE_CLOSED = 1;
public enum State {
OPEN(STATE_OPEN),
CLOSED(STATE_CLOSED);
int state;
@kostapc
kostapc / LogOutputWriter.java
Created February 22, 2017 13:00
Log4j for nashorn (log4j writer for JavaScript)
package net.c0f3.scripting;
import org.apache.commons.logging.Log;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
public class LogOutputWriter extends Writer {