Skip to content

Instantly share code, notes, and snippets.

View scottashipp's full-sized avatar

Scott Shipp scottashipp

View GitHub Profile
@scottashipp
scottashipp / gist:aa9b606a367e3b08609e
Created October 8, 2014 15:47
Builder example, part 8, the ValidatorBuilder
public class ValidatorBuilder {
private Validator v;
public ValidatorBuilder() {
v = new Validator();
}
public Validator build() {
return v;
}
@scottashipp
scottashipp / gist:acb1d597ed1ad69fc8f7
Last active August 29, 2015 14:07
Builder pattern example, part 9
class Validator {
private Set<String> validationErrors;
private Map<String[], ValidationRule> validationRules;
private boolean validated = false;
Validator() {
validationRules = new HashMap<String[], ValidationRule>();
validationErrors = new HashSet<String>();
}
@scottashipp
scottashipp / gist:b772414a58d519bdcd63
Last active August 29, 2015 14:07
Builder pattern example, validation rule
interface ValidationRule {
/**
* Return the error message for this validation rule,
* which can be retrieved when validate is false.
*
*/
String getErrorMessage();
/**
* Execute the logic for this validation rule and return true
@scottashipp
scottashipp / gist:2cd88d2cfdf935895dd5
Created October 8, 2014 16:18
Builder pattern example, Validator class
class Validator {
private Set<String> validationErrors;
private Map<String[], ValidationRule> validationRules;
private boolean validated = false;
Validator() {
validationRules = new HashMap<String[], ValidationRule>();
validationErrors = new HashSet<String>();
}
@scottashipp
scottashipp / gist:d72a2b567a388bf8516c
Created November 11, 2014 16:14
Members.scala - basic demo of Scala overriding traits
abstract class Member {
def memberId: Int
def firstName: String
def lastName: String
}
class BasicMember(val memberId: Int, val firstName: String, val lastName: String) extends Member
trait BuckooBucks {
var buckooBalance: Int = 1000
@scottashipp
scottashipp / gist:aea21a378575b59adab4
Last active May 28, 2019 19:29
Example trait for stackable traits explanation at https://code.scottshipp.com/?p=1368
trait FivePercentBonus extends BuckooBucks {
abstract override def addBucks(amt: Int) = {
if(amt >= 500) {
val bonus = (0.05 * amt)
buckooBalance += amt
}
super.addBucks(amt)
}
}
@scottashipp
scottashipp / Person.java
Created November 18, 2014 00:21
Example of using static factory methods in Java
public class Person {
static int NEXT_ID = 0;
private String first;
private String last;
private int id;
private Person() {} //prevents instantiation not using "create"
@scottashipp
scottashipp / Person.scala
Created November 18, 2014 00:26
Example of use of a companion object to provide the equivalent to Java's static methods and fields.
/* 3. */ object Person {
var nextId: Int = 0;
/* 4. */ def create(first: String, last: String): Person = {
nextId += 1
new Person(first, last, nextId-1)
}
}
@scottashipp
scottashipp / gist:9e3bf88ea1095b317629
Last active August 29, 2015 14:13
Using Scala extractor with List filter and find
class Member(val id: String, val programId: Long)
class Program3Member(val memberId: String, val program: String)
object Program3Member {
def unapply(c: Member): Option[Member] = { if(c.programId == 3L) Some(c) else None }
}
val pete = new Member("12345", 9L)
val steve = new Member("67891", 3L)
val janet = new Member("23458", 3L)
@scottashipp
scottashipp / gist:74968563149735fa77e5
Last active August 29, 2015 14:13
Scala extractor with "collect" example
object MemberType extends Enumeration {
type MemberType = Value
val Free, Trial, Pay, Premium = Value
}
object MemberStatus extends Enumeration {
type MemberStatus = Value
val Active, Inactive, Disabled = Value
}