Skip to content

Instantly share code, notes, and snippets.

@bcalmac
bcalmac / SwingDrawingExample.java
Last active May 16, 2016 13:51
Minimal Swing application that draws a line
package util.graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
@bcalmac
bcalmac / MemoryMappedFileTest.java
Last active April 28, 2016 17:38
Write and then read back a memory mapped file
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.assertEquals;
@bcalmac
bcalmac / Numbers.java
Created February 20, 2016 16:10
Round a double
/** Rounds a double value to a specified number of decimal places. */
public static double round(double value, int scale) {
return new BigDecimal(value).setScale(scale, RoundingMode.HALF_UP).doubleValue();
}
@bcalmac
bcalmac / TestApplicationConfiguration.java
Last active August 29, 2015 14:26
Spring Boot Test configuration that excludes CommandLineRunners
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
@EnableAutoConfiguration
public class TestApplicationConfiguration {
@bcalmac
bcalmac / JMockitExceptionTest.java
Created June 3, 2015 16:28
Experiment with recording expectations that throw exceptions
import java.sql.Connection;
import java.sql.SQLException;
import mockit.Expectations;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@bcalmac
bcalmac / InfiniteLoop.java
Created June 3, 2015 16:04
Infinite loop in JBoss 7.1.1 on Java 8
package org.jboss.as.server;
import java.util.concurrent.ConcurrentSkipListSet;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.Phase;
import org.jboss.as.server.deployment.SubDeploymentProcessor;
import org.jboss.as.server.deployment.annotation.AnnotationIndexProcessor;
@bcalmac
bcalmac / runin.sh
Last active August 29, 2015 14:19
Bash utility function to run a command in a given directory
#!/usr/bin/env bash
# Runs a command in a given directory and preserves the $? of the command
# Example: "runin /src/project mvn install"
function runin {
pushd $1
shift
"$@"
local STATUS=$?
popd
@bcalmac
bcalmac / TagFilteredByNestedAttribute.xml
Created April 13, 2015 18:36
IntelliJ structural search for XML tag based on nested attributes
<object type="$obj_type$">
<$tag$/>
<property>
<$tag2$/>
<customtag name="poll"/>
<$tag3$/>
</property>
<$tag4$/>
</object>
@bcalmac
bcalmac / .ideavimrc
Created April 7, 2015 17:51
Minimal .vimrc for IDEA Vim
set incsearch
set hlsearch
set ignorecase
set smartcase
set tabstop=4 shiftwidth=4 expandtab
set clipboard=unnamed
@bcalmac
bcalmac / JavaStringSplittingQuirks.groovy
Created March 19, 2015 23:38
Java string splitting quirks
// Splitting an empty string returns an array with an empty string
assert "".split(";") == [""]
// Splitting a string consisting of just the delimiter returns an empty array
assert ";".split(";") == []
// Trailing delimiters are ignored
assert "1;;2;;".split(";") == ["1", "", "2"]
// ... but the behavior becomes consistent with a -1 extra argument
assert ";".split(";", -1) == ["", ""]
assert "1;;2;;".split(";", -1) == ["1", "", "2", "", ""]