Skip to content

Instantly share code, notes, and snippets.

@alexradzin
alexradzin / CQLTest
Last active August 29, 2015 14:11
CQLTest that reproduces FSWriteError in Cassandra: https://issues.apache.org/jira/browse/CASSANDRA-8390
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Session;
import com.google.common.util.concurrent.Futures;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
public class CQLTest {
@alexradzin
alexradzin / selftar.sh
Last active November 18, 2023 21:24
Script that creates self extracting executable script from tar.gz file.
#!/bin/sh
if [ $# -eq 0 ]; then
echo "This script creates self extractable executable"
echo Usage: $0 TAR.GZ [COMMAND]
exit;
fi
if [ $# -gt 0 ]; then
TAR_FILE=$1
@alexradzin
alexradzin / FormatterExercise.java
Created February 24, 2016 14:58
LocalesFormat Iterates over all locale available on current system and prints locale that use special characters to present digits. It also counts how many locales use dot and comma as a decimal delimiter. FormatterExercise compares performance of String.format() vs direct usage of Formatter class.
import java.util.Formatter;
public class FormatterExercise {
private static int n = 1_000_000;
public static void main(String[] args) throws InterruptedException {
long before = System.nanoTime();
str();
long after = System.nanoTime();
System.out.println((after - before)/1_000_000);
@alexradzin
alexradzin / TryPerf.java
Created March 17, 2016 13:48
try/catch/finally performance
public class TryPerf {
private static int foo(int a) {
return a * 2;
}
private static int tryFinally(int a) {
try {
return a * 2;
} finally {
@alexradzin
alexradzin / String.format.vm
Created December 3, 2018 18:27
Velocity template for IntelliJ Idea Java IDE. This template generates toString() implementations using String.format()
public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
return String.format("$classname{#foreach( $member in $members )$member.name=%s#if ( $i < $members.size() - 1 ), #end#set ( $i = $i + 1 )#end}"#foreach( $member in $members ),#if ( $member.objectArray )$member.accessor == null ? null : java.util.Arrays.asList($member.accessor)#else$member.accessor#end#end);
#else
return "$classname{}";
#end
}
@alexradzin
alexradzin / Color.java
Last active March 12, 2019 06:30
Simple utility that helps to create mirror enums safe for modification of the source enum.
public enum Color {
RED, GREEN, BLUE,;
static {
Mirror.of(RGB.class);
}
}
enum Action {ONE, TWO, THREE}
// ..........
Action a = ...
switch (a) {
case ONE: one(); break;
case TWO: two(); break;
case THREE: three(); break;
default: throw new UnsupportedOperationException(String.format("Operation %s is not supported", a));
}
@alexradzin
alexradzin / SyntaxHighlighter by reference
Last active February 1, 2019 20:35
SyntaxHighlighter
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http:/
@alexradzin
alexradzin / Book.java
Created February 12, 2019 00:21
Improved Visitor
package visitor;
public class Book implements Visitable {
private final String title;
private final int pageCount;
public Book(String title, int pageCount) {
this.title = title;
this.pageCount = pageCount;
}
@alexradzin
alexradzin / build_patch.sh
Last active September 12, 2019 07:44
Patch for the Hive JDBC driver under spark
#!/bin/sh
# This file shoule be copied to src/java/ and executed from this directory
JARS=../../jars
CP=$JARS/hive-service-1.2.1.jar:$JARS/hive-jdbc-1.2.1.jar:$JARS/commons-logging-1.1.3.jar:$JARS/libthrift-0.9.3.jar
javac -cp $CP org/apache/hive/jdbc/HiveStatement.java
javac -cp $CP org/apache/hive/jdbc/HivePreparedStatement.java
javac -cp $CP org/apache/hive/jdbc/HiveResultSetMetaData.java
cp $JARS/hive-jdbc-1.2.1.jar $JARS/hive-jdbc-1.2.1_patch.jar