Skip to content

Commit

Permalink
TimeUnits should have unique identifiers. Fixes ocpsoft#152
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Dec 19, 2020
1 parent cb596ba commit d7817fe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
18 changes: 9 additions & 9 deletions core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.ocpsoft.prettytime.impl.DurationImpl;
import org.ocpsoft.prettytime.impl.ResourcesTimeFormat;
Expand Down Expand Up @@ -71,9 +71,9 @@ public class PrettyTime
{
private volatile Instant reference;
private volatile Locale locale = Locale.getDefault();
private final Map<TimeUnit, TimeFormat> units = new LinkedHashMap<>();
private final Map<TimeUnit, TimeFormat> units = new ConcurrentHashMap<>();
private volatile List<TimeUnit> cachedUnits;
private final String overrideResourceBundle;
private String overrideResourceBundle;

/**
* Create a new {@link PrettyTime} instance that will always use the current value of
Expand All @@ -83,7 +83,6 @@ public class PrettyTime
public PrettyTime()
{
this((String) null);
initTimeUnits();
}

/**
Expand All @@ -95,7 +94,7 @@ public PrettyTime()
public PrettyTime(final String overrideResourceBundle)
{
this.overrideResourceBundle = overrideResourceBundle;
initTimeUnits();
this.initTimeUnits();
}

/**
Expand Down Expand Up @@ -268,9 +267,8 @@ public PrettyTime(final LocalDate reference, final ZoneId zoneId, final String o
*/
public PrettyTime(final Locale locale)
{
this.overrideResourceBundle = null;
this();
setLocale(locale);
initTimeUnits();
}

/**
Expand All @@ -282,9 +280,8 @@ public PrettyTime(final Locale locale)
*/
public PrettyTime(final Locale locale, String overrideResourceBundle)
{
this.overrideResourceBundle = overrideResourceBundle;
this(overrideResourceBundle);
setLocale(locale);
initTimeUnits();
}

/**
Expand Down Expand Up @@ -333,6 +330,9 @@ public Duration approximateDuration(Date then)

final Instant ref = reference != null ? reference : Instant.now();
long difference = then.getTime() - ref.toEpochMilli();
if (difference == 0) {
difference = 1;
}
return calculateDuration(difference);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
*/
public abstract class ResourcesTimeUnit implements TimeUnit
{
private static long ID = 0;
private long id = 0;
private long maxQuantity = 0;
private long millisPerUnit = 1;

public ResourcesTimeUnit()
{
this.id = ID++;
}

/**
* Return the time-unit prefix to specify which value to load from the bundle.
*/
Expand Down Expand Up @@ -77,8 +84,7 @@ public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + Long.hashCode(maxQuantity);
result = prime * result + Long.hashCode(millisPerUnit);
result = prime * result + Long.hashCode(id);
return result;
}

Expand Down
8 changes: 8 additions & 0 deletions core/src/test/java/org/ocpsoft/prettytime/PrettyTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public void testRightNow() throws Exception
Assert.assertEquals("moments from now", t.format(new Date()));
}

@Test
public void testRightNowWithReference() throws Exception
{
Date now = new Date();
PrettyTime t = new PrettyTime();
Assert.assertEquals("moments from now", t.format(now));
}

@Test
public void testCalculatePreciceDuration() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.junit.Assert.assertEquals;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Locale;

Expand Down Expand Up @@ -60,7 +61,10 @@ public void testRightNow() throws Exception
public void testMinutesFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
System.out.println(t.getUnits());
Assert.assertEquals(12, t.getUnits().size());
TimeFormat format = t.removeUnit(Minute.class);
Assert.assertEquals(11, t.getUnits().size());
Assert.assertNotNull(format);
assertEquals("720 seconds from now", t.format(new Date(1000 * 60 * 12)));
}
Expand All @@ -74,6 +78,14 @@ public void testHoursFromNow() throws Exception
assertEquals("180 minutes from now", t.format(new Date(1000 * 60 * 60 * 3)));
}

@Test
public void testModifyUnitInPlace()
{
PrettyTime prettyTime = new PrettyTime();
prettyTime.getUnit(JustNow.class).setMaxQuantity(1000L * 2L);
Assert.assertEquals("moments ago", prettyTime.format(LocalDateTime.now().minusSeconds(1)));
}

// Method tearDown() is called automatically after every test method
@After
public void tearDown() throws Exception
Expand Down

0 comments on commit d7817fe

Please sign in to comment.