Skip to content

Instantly share code, notes, and snippets.

View holyjak's full-sized avatar
💓
Loving Clojure

Jakub Holý holyjak

💓
Loving Clojure
View GitHub Profile
@holyjak
holyjak / pom.xml
Created May 22, 2013 15:42
Include project name & version in the generated MANIFEST.MF
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
@holyjak
holyjak / pom.xml
Created May 22, 2013 15:45
Pom setup to include git/svn/mercurial revision in the generated MANIFEST.MF file
<project>
<build>
<plugins>
<plugin>
<!-- Create the property $buildNumber holding the current Git revision -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
@holyjak
holyjak / PersistentDatafileSetStraightforward.java
Created May 26, 2013 12:11
Wonders of Code - Straightforward vs. Structured, Non-repetitive Code: Which Would You Choose? (DB-Backed Set) - STRAIGHTFORWARD (http://wp.me/p3ui6n-Q)
/** A set backed by a database table */
public class PersistentDatafileSetStraightforward extends AbstractSet<Datafile> {
public enum StorageType {Hive, JDBC}
public static final String JDBC_URL = "jdbc.url";
public static final String JDBC_USERNAME = "jdbc.username";
public static final String JDBC_PASSWORD = "jdbc.password";
private final Configuration conf;
@holyjak
holyjak / PersistentDatafileSetStructured.java
Created May 26, 2013 12:13
Wonders of Code - Straightforward vs. Structured, Non-repetitive Code: Which Would You Choose? (DB-Backed Set) - DRY & SRP (http://wp.me/p3ui6n-Q)
public class PersistentDatafileSetStructured extends AbstractSet<Datafile> {
public static enum StorageType {Hive, JDBC};
private final StatementExecutor db;
private final String tableName;
private final StorageType storageType;
private final String selectAll;
@holyjak
holyjak / SimpleSingletonBatchJob.java
Created September 20, 2013 06:57
Wonders of Code - Simplicity vs. Robustness In Lock File Handling (http://wp.me/p3ui6n-1k) - the (overly) simple design
public class SimpleSingletonBatchJob {
private static boolean getLock() {
File file = new File(LOCK_DIRECTORY+File.separatorChar+Configuration.getGroupPrefix());
try {
return file.createNewFile();
} catch (IOException e) {
return false;
}
}
@holyjak
holyjak / RobustSingletonBatchJob.java
Last active December 23, 2015 12:19
Wonders of Code - Simplicity vs. Robustness In Lock File Handling (http://wp.me/p3ui6n-1k) - the robust re-design
public class RobustSingletonBatchJob {
// Note: We could use File.deleteOnExit() but the docs says it is not 100% reliable and recommends to
// use java.nio.channels.FileLock; however this code works well enough for us
static synchronized boolean getLock() {
File file = new File(LOCK_DIRECTORY, StaticConfiguration.getGroupPrefix());
try {
// Will try to create path to lockfile if it does not exist.
file.getParentFile().mkdirs(); // #1 Create the lock dir if it doesn't exist
if (file.createNewFile()) {
@holyjak
holyjak / EpidemySimulator_Primitive.scala
Created November 17, 2013 23:20
[Surfacing Hidden Design: A Better Alternative To Interrelated Mutable Fields](http://wp.me/p3ui6n-1q) - 1) Design hidden in primitive mutable fields and methods
class Person (val id: Int) {
var infected = false
var sick = false
var immune = false
var dead = false
// to complete with simulation logic
// [infection progression]
}
@holyjak
holyjak / EpidemySimulator_Explicit.scala
Last active December 28, 2015 15:09
[Surfacing Hidden Design: A Better Alternative To Interrelated Mutable Fields](http://wp.me/p3ui6n-1q) - 1) Explicit and defect-preventing expression of the states and transitions
def randomBelow = ...
/** When should the health state change and what to */
class HealthChange(val time: Int, val newHealth: Health) {
def immediate = (time == 0)
}
/** The root of the health hierarchy; the health (i.e. infection) evolves in stages */
sealed abstract class Health(val infectious: Boolean, val visiblyInfectious: Boolean) {
def evolve(): Option[HealthChange] // returns None if no further change possibly/expected
}
sealed class Health(val infectious: Boolean, val visiblyInfectious: Boolean) {
def evolve(implicit randomGenerator: RandomIntGenerator, config: MySimConfig): Option[HealthChange] =
this match {
case Healthy =>
if (randomGenerator.randomBelow(101) <= config.transmissibilityPct)
Some(new HealthChange(0, Incubatious))
else None
case Incubatious =>
Some(new HealthChange(6, Sick))
@holyjak
holyjak / EpidemySimulator_PrimitiveFull.scala
Created November 18, 2013 18:38
[Surfacing Hidden Design: A Better Alternative To Interrelated Mutable Fields](http://wp.me/p3ui6n-1q) - 1) Explicit and defect-preventing expression of the states and transitions - full code
// P.S.: Excuse the underscores ...
class Person (val id: Int) {
// Simplified primitive version, assuming the information when should the evolution
// happen is handled somewhere else
def evolveHealth(): Unit = {
if (!_infected)
if (randomGenerator.randomBelow(101) <= config.transmissibilityPct)
this._infected = true
if (_infected && !_sick && !_immune)