Skip to content

Instantly share code, notes, and snippets.

View abyx's full-sized avatar

Aviv Ben-Yosef abyx

View GitHub Profile
public class AccountTest {
@Test public void contains() throws Exception {
SortedSet<Account> set = new TreeSet<Account>(Account.NAME_COMPARATOR);
Account first = new Account("first", 1);
set.add(first);
Account otherFirst = new Account("first", 2);
assertFalse(set.contains(otherFirst));
}
}
Comparator<Account> NAME_COMPARATOR = new Comparator<Account>() {
@Override public int compare(Account a1, Account a2) {
if (a1.name == null) {
if (a2.name == null) {
return 0;
}
return -1;
}
return a1.name.compareTo(a2.name);
}
public class Account {
private final String name;
private final int id;
public Account(String name, int id) {
this.name = name;
this.id = id;
}
public int getId() { return id; }
public String getName() { return name; }
@Override public int hashCode() {
@abyx
abyx / bowling2.py
Created November 4, 2010 17:49
Bowling snippet clean code
if isStrike(frameIndex):
score += FULL_FRAME_SCORE + strikeFrameBonus(frameIndex)
frameIndex++
elif isSpare(frameIndex):
score += FULL_FRAME_SCORE + spareFrameBonus(frameIndex)
frameIndex += 2
else:
score += frameScore(frameIndex)
frameIndex += 2
@abyx
abyx / bowling.py
Created November 4, 2010 17:48
Bowling snippet with duplication?
if rolls[frameIndex] == 10:
score += rolls[frameIndex] + rolls[frameIndex + 1] + rolls[frameIndex + 2]
frameIndex++
elif rolls[frameIndex] + rolls[frameIndex + 1] == 10:
score += rolls[frameIndex] + rolls[frameIndex + 1] + rolls[frameIndex + 2]
frameIndex += 2
else:
score += rolls[frameIndex] + rolls[frameIndex + 1]
frameIndex += 2
@abyx
abyx / gof1.rb
Created November 4, 2010 17:46
Game of Life snippet #1
if cell.is_alive? and cell.neighbours in (2,3) then
cell.stay_alive
elsif cell.is_dead? and cell.neighbours == 2 then
cell.live!
end
@abyx
abyx / SimpleDuplication.java
Created November 4, 2010 17:45
Simple duplication?
for (int i = 0; i < array.lenth; i++)
if (array[i] == 0)
bad_ones++;
@abyx
abyx / Game.java
Created November 2, 2010 05:11
Bowling Kata
public class Game {
private static final int MAX_ROLLS_IN_GAME = 21;
private static final int FRAMES_IN_GAME = 10;
private int rolls[] = new int [MAX_ROLLS_IN_GAME];
private int currRoll;
public void roll(int pins) {
rolls[currRoll] = pins;
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder name="enc" class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.crowdspot.logging.PatternLayoutWithUserContext">
<param name="Pattern" value="%d{HH:mm:ss.SSS} %-5level %logger{10} [%user %session] - %msg%n" />
</layout>
</encoder>
</appender>