Skip to content

Instantly share code, notes, and snippets.

View krisskross's full-sized avatar

Christofer Sjögren krisskross

View GitHub Profile
public class ProductCatalogue {
private Multimap<Class, ? extends Item> catalogue = ArrayListMultimap.create();
public void add(Item item) {
catalogue.put(item.getClass(), item);
}
public <T extends Item> Collection<Item> list(Class<T> clazz) {
return catalogue.get(clazz);
}
BiMap<String, String> languageCodes = HashBiMap.create();
languageCodes.put("en", "English");
languageCodes.put("fr", "French");
languageCodes.put("zh", "Chinese");
assert "English" == languageCodes.get("en");
assert "en&amp" == languageCodes.inverse().get("English");
public Item(String id, String name) {
this.id = Preconditions.checkNotNull(id, "id must not be null");
this.name = Preconditions.checkNotNull(name, "name must not be null");
Preconditions.checkArgument(name.length() < 6, "name must be longer than 6 chars");
}
public class Voyage {
private Country targetcountry;
private int capacity;
private List<Cargo> items = Constraints.constrainedList(new ArrayList<Cargo>(), new Constraint<Cargo>() {
@Override
public Cargo checkElement(Cargo cargo) {
Preconditions.checkNotNull(cargo);
Preconditions.checkArgument(targetcountry.allows(cargo));
Preconditions.checkArgument(cargo.getUnits() > 0);
return cargo;
Predicate<Item> heavyItemPolicy = new Predicate<Item>() {
@Override
public boolean apply(Item item) {
if(item.getWeight() > 1000){
return true;
}
return false;
}
};
Collection<Item> heavyItems = Collections2.filter(order, heavyItemPolicy);
Function currencyConverter = new Function<Double, Item>() {
@Override
public Double apply(Item item) {
return item.getPrice() * ANOTHER_CURRENCY;
}
}
Collection<Double> prices = Collections2.transform(order, currencyConverter);
InMemoryStorage storage = new InMemoryStorage();
// add a few Item.class objects to storage
Criteria middleprice = field("price").is(largerThan(100)).and(lessThan(200));
Criteria expired = field("expires").is(after(currentDate));
Collection<Item> result = storage.select(middleprice.and(not(expired))).from(Item.class);
@RunWith(JUnitRunner.class)
public class JUnitRunnerDemoTest {
@Test
public void demoFixtureArguments(FixtureExample one, FixtureExample two, FixtureExample three) {
System.out.println("demonstrateFixtureArguments(" + one + ", " + two + ", " + three + ")");
}
}
demonstrateFixtureArguments(0, 0, 0)
demonstrateFixtureArguments(1, 0, 0)
demonstrateFixtureArguments(0, 1, 0)
demonstrateFixtureArguments(1, 1, 0)
demonstrateFixtureArguments(0, 0, 1)
demonstrateFixtureArguments(1, 0, 1)
demonstrateFixtureArguments(0, 1, 1)
demonstrateFixtureArguments(1, 1, 1)
public class DummyTest {
@Rule
public IgnoreLeadingFailure ilf = new IgnoreLeadingFailure();
@Test
public void testTest() {
assertTrue(false);
}
}