AgeMerger implements SinglePropertyValueMerger<Double, Object> { @Override public Double mergeObjects(Object value, String[] extraParam) { if (value == null) { return 0.0; } if (!(value instanceof Date) && !(value instanceof Calendar)) { throw new IllegalArgumentException("Value is neither a Date or a Calendar instance."); } double todayStamp = Calendar.getInstance().getTimeInMillis(); double otherStamp = getOtherStamp(value); double constant = resolveConstant(extraParam); return (double) Math.round((todayStamp - otherStamp) / constant); }  @Override Double mergeObjects(Object value, String[] extraParam); @Override boolean isRestoreSupported(String[] params); @Override Object restoreObject(Double object, String[] params); static final String YEARS; static final String WEEKS; static final String DAYS; }
@Test public void testAgeMergerNull() { AgeMerger merger = new AgeMerger(); Number expected = merger.mergeObjects(null, null); assertEquals("should be 0", 0, expected.intValue()); }
@Test public void testAgeMergerDate() { AgeMerger merger = new AgeMerger(); Number expected = 20.0; Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -1* expected.intValue()); Number result = merger.mergeObjects(cal.getTime(), null); assertEquals("should be the same age", expected.intValue(), result.intValue()); }
@Test public void testAgeMergerCalendar() { AgeMerger merger = new AgeMerger(); Number expected = 20.0; Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -1* expected.intValue()); Number result = merger.mergeObjects(cal, new String[] { AgeMerger.YEARS }); assertEquals("should be the same age", expected.intValue(), result.intValue()); }
@Test public void testAgeMergerWeeks() { AgeMerger merger = new AgeMerger(); Number expected = 20.0; Calendar cal = Calendar.getInstance(); cal.add(Calendar.WEEK_OF_YEAR, -1* expected.intValue()); Number result = merger.mergeObjects(cal, new String[] { AgeMerger.WEEKS }); assertEquals("should be the same age in weeks", expected.intValue(), result.intValue()); }
@Test public void testAgeMergerDays() { AgeMerger merger = new AgeMerger(); Number expected = 20.0; Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -1* expected.intValue()); Number result = merger.mergeObjects(cal, new String[] { AgeMerger.DAYS }); assertEquals("should be the same age in weeks", expected.intValue(), result.intValue()); }