Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
VenkataRaju / SchedulesThreadPoolCongestionPreventionTest.java
Last active July 9, 2016 05:15
Cancel the next excution in case, the previous execution of the task is not completed yet, while using ScheduledThreadPool's scheduleAtFixedRate method
package test;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@VenkataRaju
VenkataRaju / renamer.js
Created July 3, 2016 16:09
File renaming using Nashorn script
var FILE_PATTERNS = /^(Channel_Play_Success_Ratio|Video_Play_Success_Ratio|Login_Success_Ratio|Play_Success_Ratio).+\-1\.csv$/;
var Files = java.nio.file.Files;
var Paths = java.nio.file.Paths;
function printWithCarriageReturn(str) java.lang.System.out['print(String)'](str + '\r');
function throwError(msg) { throw new Error(msg); }; // Brackets are required
if (typeof $ARG === 'undefined' || $ARG.length === 0)
@VenkataRaju
VenkataRaju / MaximumOccurrenceOfAnyEventInTimeRange.java
Last active March 12, 2018 00:20
Maximum occurrence of any event in time range
package test;
import java.time.LocalTime;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@VenkataRaju
VenkataRaju / RateLimiter.kt
Last active April 10, 2016 11:28
Releases only provided number of tokens per provided timeunit, otherwise forces the caller to wait until the next set of tokens available
class RateLimiter(val time: Long, val timeUnit: TimeUnit, val noOfTokensPerSlot: Int)
{
private val timeInMillis = timeUnit.toMillis(time)
private var tokensReleasedTime = (System.currentTimeMillis() / 1000) * 1000
private var availableTokens = noOfTokensPerSlot
init
{
require(time > 0, { "time[$time] should be positive" })
@VenkataRaju
VenkataRaju / custom-array-sorter.js
Last active March 5, 2016 13:04
JavaScript Custom Array Sorter
function CustomArraySorter()
{
this.sort = function(arr, orderReference /* Optional */)
{
orderRef = orderReference;
orderRef ? arr.sort(customSortFun) : arr.sort();
};
let orderRef;
let customSortFun = (x, y) => orderRef.indexOf(x) - orderRef.indexOf(y);
}
@VenkataRaju
VenkataRaju / proxy_from_pac.js
Last active December 31, 2015 11:58
Finds proxy for the given url from pac file.
var debug = false;
start();
function start()
{
if (typeof $ARG === "undefined" || $ARG.length !== 2)
{
log("Usage: jjs -scripting proxy_from_pac.js -- <pacfile> <url>");
function minNonNegative(num1, num2)
{
if (num1 >= 0 && (num1 <= num2 || num2 < 0))
return num1;
if (num2 >= 0)
return num2;
throw new Error(`Both num1[${num1}] and num2[${num2}] are negative`);
}
@VenkataRaju
VenkataRaju / IterableFlattener.java
Last active October 17, 2017 14:29
Flattens elements in the Iterable of Iterable of ...
package test;
import static java.util.Arrays.asList;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
public abstract class AbstractIterator<T> implements Iterator<T>
{
private boolean hasMoreData = true, nextComputed;
private T data;
@Override
public final boolean hasNext()
{
if (hasMoreData && !nextComputed)
{
@VenkataRaju
VenkataRaju / PeekingIterator.java
Created December 14, 2015 14:38
Peek an element without moving the pointer (Untested)
public final class PeekingIterator<T> implements Iterator<T>
{
private Iterator<T> iterator;
private boolean elementPeeked;
private T peekedElement;
public PeekingIterator(Iterator<T> iterator)
{
this.iterator = iterator;