Skip to content

Commit

Permalink
chore: formatting and text stabilization fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Mar 1, 2023
1 parent 3fdf436 commit 28787d0
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 210 deletions.
24 changes: 11 additions & 13 deletions core/src/main/java/org/ocpsoft/prettytime/PrettyTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public String formatDurationUnrounded(Date then)
* rules are ignored. If the given {@link Calendar} is <code>null</code>, the current value of
* {@link System#currentTimeMillis()} will be used instead.
* <p>
*
*
* @param then the date to be formatted
* @return A formatted string of the given {@link Date}
*/
Expand Down Expand Up @@ -1306,10 +1306,12 @@ public String formatDurationUnrounded(final LocalDate then)
*/
public TimeFormat getFormat(TimeUnit unit)
{
if (unit == null) return null;
if (unit == null)
return null;
if (units.get(unit) != null) {
return units.get(unit);
} else {
}
else {
// Trying to transform the TimeUnit to String does the trick
Map<String, TimeFormat> map = new ConcurrentHashMap<>();
units.keySet().forEach(key -> map.put(key.toString(), units.get(key)));
Expand Down Expand Up @@ -1466,13 +1468,11 @@ public <UNIT extends TimeUnit> UNIT getUnit(final Class<UNIT> unitType)
}

/**
* Get the registered {@link TimeUnit} for the given {@link ChronoUnit} type or {@code null}
* if none exists.
* Get the registered {@link TimeUnit} for the given {@link ChronoUnit} type or {@code null} if none exists.
*
* @param unit The {@code ChronoUnit}
* @return the time unit
* @throws IllegalArgumentException if no corresponding {@code TimeUnit} was found for the given
* {@code ChronoUnit}
* @throws IllegalArgumentException if no corresponding {@code TimeUnit} was found for the given {@code ChronoUnit}
*/
public TimeUnit getUnit(final ChronoUnit unit)
{
Expand All @@ -1499,11 +1499,11 @@ public PrettyTime registerUnit(final TimeUnit unit, TimeFormat format)

/**
* Register the given {@link ChronoUnit} and corresponding {@link TimeFormat}.
*
* @param unit The {@code ChronoUnit} to be registered
* @param format The {@code TimeFormat} to be registered
* @return the current {@code PrettyTime} object
* @throws IllegalArgumentException if no corresponding {@code TimeUnit} was found for the given
* {@code ChronoUnit}
* @throws IllegalArgumentException if no corresponding {@code TimeUnit} was found for the given {@code ChronoUnit}
*/
public PrettyTime registerUnit(final ChronoUnit unit, final TimeFormat format)
{
Expand Down Expand Up @@ -1574,13 +1574,11 @@ public TimeFormat removeUnit(final TimeUnit unit)
}

/**
* Removes the mapping corresponding to the given {@link ChronoUnit}, returning the
* {@link TimeFormat} if available.
* Removes the mapping corresponding to the given {@link ChronoUnit}, returning the {@link TimeFormat} if available.
*
* @param unit The {@code ChronoUnit} to be removed
* @return the corresponding {@code TimeFormat}
* @throws IllegalArgumentException if no {@link TimeUnit} corresponding to the given
* {@code ChronoUnit} was found.
* @throws IllegalArgumentException if no {@link TimeUnit} corresponding to the given {@code ChronoUnit} was found.
*/
public TimeFormat removeUnit(final ChronoUnit unit)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testApiMisuseSetUnits1() throws Exception
@Test
public void testApiMisuse1() throws Exception
{
Assert.assertEquals(t.approximateDuration(LocalDateTime.now()),
Assert.assertEquals(t.approximateDuration((LocalDateTime) null),
t.approximateDuration((LocalDateTime) null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ public void testCeilingInterval() throws Exception
@Test
public void testNullDate() throws Exception
{
PrettyTime t = new PrettyTime(locale);
Date now = new Date();
PrettyTime t = new PrettyTime(now, locale);
assertEquals("straks", t.format((Date) null));
}

@Test
public void testRightNow() throws Exception
{
PrettyTime t = new PrettyTime(locale);
Date now = new Date();
PrettyTime t = new PrettyTime(now, locale);
assertEquals("straks", t.format(new Date()));
}

Expand Down
108 changes: 69 additions & 39 deletions core/src/test/java/org/ocpsoft/prettytime/PrettyTimeI18n_EL_Test.java
Original file line number Diff line number Diff line change
@@ -1,119 +1,136 @@
package org.ocpsoft.prettytime;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.ocpsoft.prettytime.impl.ResourcesTimeFormat;
import org.ocpsoft.prettytime.units.Minute;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class PrettyTimeI18n_EL_Test {
public class PrettyTimeI18n_EL_Test
{
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

private Locale locale;

@Before
public void setUp() throws Exception {
public void setUp() throws Exception
{
locale = Locale.getDefault();
Locale.setDefault(Locale.forLanguageTag("el"));
}

@Test
public void testCeilingInterval() throws Exception {
public void testCeilingInterval() throws Exception
{
Date then = format.parse("5/20/2009");
Date ref = format.parse("6/17/2009");
PrettyTime t = new PrettyTime(ref);
Assert.assertEquals("1 μήνας Πριν από", t.format(then));
}

@Test
public void testNullDate() throws Exception {
public void testNullDate() throws Exception
{
PrettyTime t = new PrettyTime();
final String format = t.format((Date) null);
Assert.assertTrue("πριν από στιγμές".equals(format) || "στιγμές από τώρα".equals(format));
}

@Test
public void testRightNow() throws Exception {
public void testRightNow() throws Exception
{
PrettyTime t = new PrettyTime();
Assert.assertEquals("στιγμές από τώρα", t.format(new Date()));
}

@Test
public void testCalculatePreciceDuration() throws Exception {
PrettyTime t = new PrettyTime();
public void testCalculatePreciceDuration() throws Exception
{
Date now = new Date();
PrettyTime t = new PrettyTime(now);
List<Duration> preciseDuration = t.calculatePreciseDuration(
new Date(System.currentTimeMillis() - (2 * 60 * 60 * 1000) - (2 * 60 * 1000)));
new Date(System.currentTimeMillis() - (2 * 60 * 60 * 1000) - (2 * 60 * 1000)));
Assert.assertEquals("2 ώρες 2 λεπτά Πριν από", t.format(preciseDuration));
Assert.assertEquals("2 ώρες 2 λεπτά", t.formatDuration(preciseDuration));
Assert.assertEquals("στιγμές από τώρα", t.format(t.calculatePreciseDuration(new Date())));
Assert.assertEquals("στιγμές από τώρα", t.format(t.calculatePreciseDuration(now)));
}

@Test
public void testCalculatePreciseDuration2() {
public void testCalculatePreciseDuration2()
{
PrettyTime prettyTime = new PrettyTime();
prettyTime.clearUnits();
Minute minutes = new Minute();
prettyTime.registerUnit(minutes, new ResourcesTimeFormat(minutes));
Assert.assertEquals("40 λεπτά Πριν από", prettyTime.formatUnrounded(prettyTime.calculatePreciseDuration(
new Date(new Date().getTime() - 40 * 60 * 1000 - 40 * 1000))));
new Date(new Date().getTime() - 40 * 60 * 1000 - 40 * 1000))));
}

@Test
public void testRightNowVariance() throws Exception {
public void testRightNowVariance() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("στιγμές από τώρα", t.format(new Date(600)));
}

@Test
public void testMinutesFromNow() throws Exception {
public void testMinutesFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("12 λεπτά από τώρα", t.format(new Date(1000 * 60 * 12)));
}

@Test
public void testHoursFromNow() throws Exception {
public void testHoursFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 ώρες από τώρα", t.format(new Date(1000 * 60 * 60 * 3)));
}

@Test
public void testDaysFromNow() throws Exception {
public void testDaysFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 ημέρες από τώρα", t.format(new Date(1000 * 60 * 60 * 24 * 3)));
}

@Test
public void testWeeksFromNow() throws Exception {
public void testWeeksFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 εβδομάδες από τώρα", t.format(new Date(1000 * 60 * 60 * 24 * 7 * 3)));
}

@Test
public void testMonthsFromNow() throws Exception {
public void testMonthsFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 μήνες από τώρα", t.format(new Date(2629743830L * 3L)));
}

@Test
public void testYearsFromNow() throws Exception {
public void testYearsFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 έτη από τώρα", t.format(new Date(2629743830L * 12L * 3L)));
}

@Test
public void testDecadesFromNow() throws Exception {
public void testDecadesFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 δεκαετίες από τώρα", t.format(new Date(315569259747L * 3L)));
}

@Test
public void testCenturiesFromNow() throws Exception {
public void testCenturiesFromNow() throws Exception
{
PrettyTime t = new PrettyTime(new Date(0));
Assert.assertEquals("3 αιώνες από τώρα", t.format(new Date(3155692597470L * 3L)));
}
Expand All @@ -122,79 +139,92 @@ public void testCenturiesFromNow() throws Exception {
* Past
*/
@Test
public void testMomentsAgo() throws Exception {
public void testMomentsAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(6000));
Assert.assertEquals("πριν από στιγμές", t.format(new Date(0)));
}

@Test
public void testMinutesAgo() throws Exception {
public void testMinutesAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(1000 * 60 * 12));
Assert.assertEquals("12 λεπτά Πριν από", t.format(new Date(0)));
}

@Test
public void testMinutesFromNowDefaultReference() throws Exception {
public void testMinutesFromNowDefaultReference() throws Exception
{
PrettyTime t = new PrettyTime();
Assert.assertEquals("12 λεπτά από τώρα", t.format(new Date(System.currentTimeMillis() + 1000 * 60 * 12)));
}

@Test
public void testHoursAgo() throws Exception {
public void testHoursAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 3));
Assert.assertEquals("3 ώρες Πριν από", t.format(new Date(0)));
}

@Test
public void testHoursAgoDefaultReference() throws Exception {
public void testHoursAgoDefaultReference() throws Exception
{
PrettyTime t = new PrettyTime();
Assert.assertEquals("3 ώρες Πριν από", t.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 3)));
}

@Test
public void testDaysAgo() throws Exception {
public void testDaysAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 3));
Assert.assertEquals("3 ημέρες Πριν από", t.format(new Date(0)));
}

@Test
public void testWeeksAgo() throws Exception {
public void testWeeksAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 7 * 3));
Assert.assertEquals("3 εβδομάδες Πριν από", t.format(new Date(0)));
}

@Test
public void testMonthsAgo() throws Exception {
public void testMonthsAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(2629743830L * 3L));
Assert.assertEquals("3 μήνες Πριν από", t.format(new Date(0)));
}

@Test
public void testYearsAgo() throws Exception {
public void testYearsAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(2629743830L * 12L * 3L));
Assert.assertEquals("3 έτη Πριν από", t.format(new Date(0)));
}

@Test
public void testDecadesAgo() throws Exception {
public void testDecadesAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(315569259747L * 3L));
Assert.assertEquals("3 δεκαετίες Πριν από", t.format(new Date(0)));
}

@Test
public void testCenturiesAgo() throws Exception {
public void testCenturiesAgo() throws Exception
{
PrettyTime t = new PrettyTime(new Date(3155692597470L * 3L));
Assert.assertEquals("3 αιώνες Πριν από", t.format(new Date(0)));
}

@Test
public void testWithinTwoHoursRounding() throws Exception {
public void testWithinTwoHoursRounding() throws Exception
{
PrettyTime t = new PrettyTime();
Assert.assertEquals("2 ώρες Πριν από", t.format(new Date(new Date().getTime() - 6543990)));
}

@After
public void tearDown() throws Exception {
public void tearDown() throws Exception
{
Locale.setDefault(locale);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void testCalculatePreciceDurationMillenia() throws Exception
{
PrettyTime t = new PrettyTime(LocalDate.of(2000, 2, 2));
List<Duration> durations = t.calculatePreciseDuration(LocalDate.of(100, 1, 1));
Assert.assertEquals("antaŭ 1 jarmilo 9 jarcentoj 1 monato 1 tago 8 horoj 34 minutoj", t.format(durations));
Assert.assertEquals("1 jarmilo 9 jarcentoj 1 monato 1 tago 8 horoj 34 minutoj", t.formatDuration(durations));
Assert.assertEquals("antaŭ 1 jarmilo 9 jarcentoj 1 monato 1 tago 9 horoj 19 minutoj", t.format(durations));
Assert.assertEquals("1 jarmilo 9 jarcentoj 1 monato 1 tago 9 horoj 19 minutoj", t.formatDuration(durations));
}

@Test
Expand Down
Loading

0 comments on commit 28787d0

Please sign in to comment.