Skip to content

Commit ee7c46e

Browse files
committed
Added API to easily remove registered units and retrieve their formats
1 parent ddfdafe commit ee7c46e

4 files changed

Lines changed: 142 additions & 7 deletions

File tree

‎core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,38 @@ public PrettyTime registerUnit(final TimeUnit unit, TimeFormat format)
403403
return this;
404404
}
405405

406+
/**
407+
* Removes the mapping for the given {@link TimeUnit} type. This effectively de-registers the unit so it will not be
408+
* used in formatting. Returns the {@link TimeFormat} that was registered for the given {@link TimeUnit} type, or
409+
* null if no unit of the given type was registered.
410+
*/
411+
public <UNIT extends TimeUnit> TimeFormat removeUnit(final Class<UNIT> unitType)
412+
{
413+
if (unitType == null)
414+
throw new IllegalArgumentException("Unit type to remove must not be null.");
415+
416+
for (TimeUnit unit : units.keySet()) {
417+
if (unitType.isAssignableFrom(unit.getClass()))
418+
{
419+
return units.remove(unit);
420+
}
421+
}
422+
return null;
423+
}
424+
425+
/**
426+
* Removes the mapping for the given {@link TimeUnit}. This effectively de-registers the unit so it will not be used
427+
* in formatting. Returns the {@link TimeFormat} that was registered for the given {@link TimeUnit}, or null if no
428+
* such unit was registered.
429+
*/
430+
public TimeFormat removeUnit(final TimeUnit unit)
431+
{
432+
if (unit == null)
433+
throw new IllegalArgumentException("Unit to remove must not be null.");
434+
435+
return units.remove(unit);
436+
}
437+
406438
/**
407439
* Get the currently configured {@link Locale} for this {@link PrettyTime} object.
408440
*/

‎core/src/test/java/org/ocpsoft/prettytime/PrettyTimeAPIManipulationTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,16 @@ public void testApiMisuse15() throws Exception
111111
t.toString();
112112
}
113113

114+
@Test(expected = IllegalArgumentException.class)
115+
public void testApiMisuse16() throws Exception
116+
{
117+
t.removeUnit((Class<TimeUnit>) null);
118+
}
119+
120+
@Test(expected = IllegalArgumentException.class)
121+
public void testApiMisuse17() throws Exception
122+
{
123+
t.removeUnit((TimeUnit) null);
124+
}
125+
114126
}

‎core/src/test/java/org/ocpsoft/prettytime/PrettyTimeI18n_Test.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ public void setUp() throws Exception
5353
public void testPrettyTimeDefault()
5454
{
5555
// The default resource bundle should be used
56-
PrettyTime p = new PrettyTime(Locale.ROOT);
57-
assertEquals("moments from now", p.format(new Date()));
56+
PrettyTime p = new PrettyTime(new Date(0), Locale.ROOT);
57+
assertEquals("moments from now", p.format(new Date(1)));
5858
}
5959

6060
@Test
6161
public void testPrettyTimeGerman()
6262
{
6363
// The German resource bundle should be used
6464
PrettyTime p = new PrettyTime(Locale.GERMAN);
65-
assertEquals("Jetzt", p.format(new Date()));
65+
p.setReference(new Date(0));
66+
assertEquals("Jetzt", p.format(new Date(1)));
6667
}
6768

6869
@Test
@@ -94,17 +95,17 @@ public void testPrettyTimeViaDefaultLocaleDefault()
9495
{
9596
// The default resource bundle should be used
9697
Locale.setDefault(Locale.ROOT);
97-
PrettyTime p = new PrettyTime();
98-
assertEquals(p.format(new Date()), "moments from now");
98+
PrettyTime p = new PrettyTime(new Date(0));
99+
assertEquals(p.format(new Date(1)), "moments from now");
99100
}
100101

101102
@Test
102103
public void testPrettyTimeViaDefaultLocaleGerman()
103104
{
104105
// The German resource bundle should be used
105106
Locale.setDefault(Locale.GERMAN);
106-
PrettyTime p = new PrettyTime();
107-
assertEquals(p.format(new Date()), "Jetzt");
107+
PrettyTime p = new PrettyTime(new Date(0));
108+
assertEquals(p.format(new Date(1)), "Jetzt");
108109
}
109110

110111
@Test
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.prettytime;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
20+
21+
import java.text.SimpleDateFormat;
22+
import java.util.Date;
23+
import java.util.List;
24+
import java.util.Locale;
25+
26+
import org.junit.After;
27+
import org.junit.Assert;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.ocpsoft.prettytime.Duration;
31+
import org.ocpsoft.prettytime.PrettyTime;
32+
import org.ocpsoft.prettytime.TimeUnit;
33+
import org.ocpsoft.prettytime.format.SimpleTimeFormat;
34+
import org.ocpsoft.prettytime.units.Hour;
35+
import org.ocpsoft.prettytime.units.JustNow;
36+
import org.ocpsoft.prettytime.units.Minute;
37+
38+
public class PrettyTimeUnitConfigurationTest
39+
{
40+
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
41+
42+
// Stores current locale so that it can be restored
43+
private Locale locale;
44+
45+
// Method setUp() is called automatically before every test method
46+
@Before
47+
public void setUp() throws Exception
48+
{
49+
locale = Locale.getDefault();
50+
Locale.setDefault(Locale.ROOT);
51+
}
52+
53+
@Test
54+
public void testRightNow() throws Exception
55+
{
56+
Date ref = new Date(0);
57+
Date then = new Date(2);
58+
59+
PrettyTime t = new PrettyTime(ref);
60+
TimeFormat format = t.removeUnit(JustNow.class);
61+
Assert.assertNotNull(format);
62+
assertEquals("2 milliseconds from now", t.format(then));
63+
}
64+
65+
@Test
66+
public void testMinutesFromNow() throws Exception
67+
{
68+
PrettyTime t = new PrettyTime(new Date(0));
69+
TimeFormat format = t.removeUnit(Minute.class);
70+
Assert.assertNotNull(format);
71+
assertEquals("720 seconds from now", t.format(new Date(1000 * 60 * 12)));
72+
}
73+
74+
@Test
75+
public void testHoursFromNow() throws Exception
76+
{
77+
PrettyTime t = new PrettyTime(new Date(0));
78+
TimeFormat format = t.removeUnit(Hour.class);
79+
Assert.assertNotNull(format);
80+
assertEquals("180 minutes from now", t.format(new Date(1000 * 60 * 60 * 3)));
81+
}
82+
83+
// Method tearDown() is called automatically after every test method
84+
@After
85+
public void tearDown() throws Exception
86+
{
87+
Locale.setDefault(locale);
88+
}
89+
90+
}

0 commit comments

Comments
 (0)