Skip to content

Instantly share code, notes, and snippets.

@maggandalf
maggandalf / ShowFooWithNull.java
Created April 8, 2011 17:35
Equals that throws a Null Pointer Exception
public void showFoo(Foo foo) { Foo foo2 = new Foo();
//lot of code
if(foo.equals(foo2)) {
System.out.println("Two Men");
} else {
System.out.println("And a Half");
}
}
@maggandalf
maggandalf / EqualsWithNullProtection.java
Created April 9, 2011 09:49
Implementing Equals with Objects class.
public void showFoo(Foo foo) {
Foo foo2 = new Foo();
//lot of code
if(Objects.equals(foo,foo2)) {
System.out.println("Two Men");
} else {
System.out.println("And a Half");
@maggandalf
maggandalf / CalculateHash.java
Created April 9, 2011 10:06
Calculates hash using Objects class.
@Override public int hashCode() {
return Objects.hash(name, age, job);
}
@maggandalf
maggandalf / RequireNotNullParameter.java
Created April 9, 2011 17:44
Requires Not Null Parameter Check.
public void showFoo(Foo foo) {
this.bar = Objects.requireNonNull(foo);
}
@maggandalf
maggandalf / LogNullParameter.java
Created April 9, 2011 17:50
Logging Null Parameter
String log = null;
if(foo == null) {
log = "Foo is Empty";
} else {
log = foo.toString();
}
logger.info(log);
@maggandalf
maggandalf / LoggingWithObjectsToString.java
Created April 9, 2011 17:54
Logging Information Using Objects.toString Null Values.
logger.info(Objects.toString(foo, “Foo is Empty”));
@maggandalf
maggandalf / NightVoter.java
Created April 15, 2011 15:57
SpringSecurity Voter granting only access when it is night.
public class NightVoter implements AccessDecisionVoter {
private String rolePrefix = "ROLE_";
private static final int SUNRISE_HOUR = 9;
private static final int SUNSET_HOUR = 21;
public String getRolePrefix() {
return rolePrefix;
}
@maggandalf
maggandalf / application-security.xml
Created April 15, 2011 16:12
Configuring Custom Voter In Access Decision Manager.
<security:http auto-config="true" access-decision-manager-ref="accessDecision">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
<bean id="accessDecision" class="org.springframework.security.access.vote.AffirmativeBased">
<property name="decisionVoters">
<set>
<bean class="org.springframework.security.access.vote.RoleVoter"></bean>
<bean class="org.example.security.voter.NightVoter"></bean>
</set>
@maggandalf
maggandalf / trader_alert_service.story
Created April 20, 2011 17:00
JBehave Story file.
Scenario: trader should be not alerted below threshold and should be alerted above threshold
Given a stock of symbol <symbol> and a threshold of <threshold>
When the stock is traded at <price>
Then the alert status should be <status>
Examples:
|symbol|threshold|price|status|
|STK1|5|4|OFF|
|STK1|5|14|ON|
@maggandalf
maggandalf / Steps.java
Created April 20, 2011 17:01
Steps Annotation
@Target(value=ElementType.TYPE)
@Retention(value=RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Steps {
}