Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
VenkataRaju / JaxbPartialObjectParser.java
Last active September 7, 2017 17:47
JAXB Partial Object Parsing
public class JaxbPartialObjectParser
{
public static void main(String[] args) throws Exception
{
String xmlStr = "<root><student><name>Raju</name></student></root>";
// Assuming Student class is JAXB generated
Iterator<Student> it = extractPartialObjects(new StringReader(xmlStr), QName.valueOf("student"), Student.class);
while (it.hasNext())
@VenkataRaju
VenkataRaju / basic_nashorn_script.js
Created August 5, 2015 08:42
Basic Nashorn Script
if (typeof $ARG === "undefined" || $ARG.length !== 2)
print("Usage: jjs -scripting --language=es6 test.js -- arg1 arg2");
else
doSomething.apply(null, $ARG);
function doSomething(first, second)
{
print("first: " + first + ", second: " + second);
for (let i = 0; i < 2; i++)
@VenkataRaju
VenkataRaju / comparison_function_by_operator.js
Last active October 6, 2015 08:39
JavaScript comparison function by operator
var comparisonFunctionByOperator = (function ()
{
var hash = {
"<" : (left, right) => left < right,
"<=" : (left, right) => left <= right,
">" : (left, right) => left > right,
">=" : (left, right) => left >= right,
"===": (left, right) => left === right,
"!==": (left, right) => left !== right,
"==" : (left, right) => left == right,
@VenkataRaju
VenkataRaju / AnyThrowableMatchesPredicateInThrowableChain.java
Last active November 17, 2015 09:26
Check if any Throwable matches the Predicate in Throwable chain
package test;
import java.io.IOException;
import java.util.function.Predicate;
public class AnyThrowableMatchesPredicateInThrowableChain
{
public static void main(String[] args) throws IOException
{
Throwable throwable = new Throwable("sdsdf");
Path path = Paths.get("D:\\Directory");
WatchService watchService = path.getFileSystem().newWatchService();
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
System.out.println("Watching...");
for (;;)
{
WatchKey wk = watchService.take();
wk.pollEvents().forEach(we -> System.out.println(we.context() + ", " + we.kind() + ", " + we.count()));
@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;
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 / 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;
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 / 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>");