Skip to content
This repository was archived by the owner on Jan 22, 2023. It is now read-only.

Commit f67956d

Browse files
author
Jonas Schubert
committed
shortens class names, implements new format*-methods, refactors, units got shortened
1 parent 071174f commit f67956d

File tree

20 files changed

+552
-384
lines changed

20 files changed

+552
-384
lines changed

README.md

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
[![Platform](https://img.shields.io/badge/platform-Android-blue.svg)](https://www.android.com)
44
[![API](https://img.shields.io/badge/API-26+-blue.svg)](https://android-arsenal.com/api?level=26)
55

6-
[![Version](https://img.shields.io/badge/version-0.3.0-blue.svg)](https://github.com/TimeXt/TimeXt-Java/tree/0.3.0)
7-
[![Build](https://img.shields.io/badge/buildJava-success-green.svg)](releases/lib-2019-05-02-1.jar)
6+
[![Version](https://img.shields.io/badge/version-0.4.0-blue.svg)](https://github.com/TimeXt/TimeXt-Java/tree/0.4.0)
7+
[![Build](https://img.shields.io/badge/buildJava-success-green.svg)](releases/lib-2019-07-19-1.jar)
8+
[![Build](https://img.shields.io/badge/buildAndroid-success-green.svg)](releases/lib-android-2019-07-19-2.aar)
89

910
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
1011
[![Paypal](https://img.shields.io/badge/paypal-donate-blue.svg)](https://www.paypal.me/GuepardoApps)
@@ -15,7 +16,7 @@ First of all many thanks to [Kizitonwose](https://github.com/kizitonwose/Time) f
1516
This library shall help to reduce code like
1617

1718
```java
18-
int dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in milliSeconds
19+
int dayInMillis = 24 * 60 * 60 * 1000; // Represent a day in millis
1920
```
2021

2122
## Important
@@ -27,65 +28,71 @@ Better use the [Kotlin version](https://github.com/TimeXt/TimeXt-Kotlin/) of thi
2728
### Basics
2829

2930
```java
30-
Interval threeYears = NumberExtensions.toYears(3);
31-
Interval oneWeek = NumberExtensions.toWeeks(1);
32-
Interval threeDays = NumberExtensions.toDays(3);
33-
Interval elevenHours = NumberExtensions.toHours(11);
34-
Interval sixMinutes = NumberExtensions.toMinutes(6);
35-
Interval fiftySeconds = NumberExtensions.toSeconds(50);
36-
Interval hundredMilliSeconds = NumberExtensions.toMilliSeconds(100);
37-
Interval fiveMicroSeconds = NumberExtensions.toMicroSeconds(5);
38-
Interval oneNanoSecond = NumberExtensions.toNanoSeconds(1);
39-
Interval onePicoSecond = NumberExtensions.toPicoSeconds(1);
40-
41-
double oneDayInMillis = NumberExtensions.toDays(1).InMilliSeconds(); // Converts one day into milliseconds
42-
double twoWeeksInHours = NumberExtensions.toWeeks(2).InHours(); // Converts two weeks into hours
43-
44-
Interval duration = NumberExtensions.toDays(1).Plus(NumberExtensions.toHours(6));
45-
Interval difference = NumberExtensions.toMinutes(34).Minus(NumberExtensions.toSeconds(420));
46-
Interval multipliedDuration = new Interval(4, Unit.Week).Times(1.5);
47-
Interval dividedDuration = new Interval(750, Unit.PicoSecond).Div(2.0);
31+
TimeXt threeYears = DoubleXt.toYears(3.0);
32+
TimeXt oneWeek = IntXt.toWeeks(1);
33+
TimeXt threeDays = LongXt.toDays(3L);
34+
TimeXt elevenHours = DoubleXt.toHours(11.0);
35+
TimeXt sixMinutes = IntXt.toMinutes(6);
36+
TimeXt fiftySeconds = LongXt.toSeconds(50L);
37+
TimeXt hundredMillis = DoubleXt.toMillis(100.0);
38+
TimeXt fiveMicros = IntXt.toMicros(5);
39+
TimeXt oneNano = LongXt.toNanos(1L);
40+
TimeXt onePico = DoubleXt.toPicos(1.0);
41+
42+
double oneDayInMillis = IntXt.toDays(1).InMillis(); // Converts one day into millis
43+
double twoWeeksInHours = LongXt.toWeeks(2L).InHours(); // Converts two weeks into hours
44+
45+
TimeXt duration = DoubleXt.toDays(1.0).Plus(IntXt.toHours(6));
46+
TimeXt difference = LongXt.toMinutes(34L).Minus(DoubleXt.toSeconds(420.0));
47+
TimeXt multipliedDuration = new TimeXt(4, TimeXtUnit.Week).Times(1.5);
48+
TimeXt dividedDuration = new TimeXt(750, TimeXtUnit.Picos).Div(2.0);
4849

4950
```
5051

5152
### Extensions
5253

5354
```java
5455
// Calendar
55-
Calendar inOneHour = CalendarExtensions.Add(Calendar.getInstance(), new Interval(1, Unit.Hour));
56-
Calendar threeDaysAgo = CalendarExtensions.Minus(Calendar.getInstance(), new Interval(3, Unit.Day));
56+
Calendar inOneHour = CalendarXt.Add(Calendar.getInstance(), new TimeXt(1, TimeXtUnit.Hour));
57+
Calendar threeDaysAgo = CalendarXt.Minus(Calendar.getInstance(), new TimeXt(3, TimeXtUnit.Day));
58+
TimeXt calendarDifference = CalendarXt.Minus(Calendar calendar1, Calendar calendar2);
5759

5860
// Sql Date
5961
java.sql.Date sqlDate = new java.sql.Date();
60-
sqlDate = DateExtensions.Add(sqlDate, new Interval(30, Unit.Second));
61-
sqlDate = DateExtensions.Minus(sqlDate, new Interval(500, Unit.MilliSecond));
62+
sqlDate = DateXt.Add(sqlDate, new TimeXt(30, TimeXtUnit.Second));
63+
sqlDate = DateXt.Minus(sqlDate, new TimeXt(500, TimeXtUnit.Millis));
64+
TimeXt dateDifference = DateXt.Minus(java.sql.Date date1, java.sql.Date date2);
6265

6366
// Util Date
6467
java.util.Date utilDate = new java.util.Date();
65-
utilDate = DateExtensions.Add(utilDate, new Interval(30, Unit.Second));
66-
utilDate = DateExtensions.Minus(utilDate, new Interval(500, Unit.MilliSecond));
68+
utilDate = DateXt.Add(utilDate, new TimeXt(30, TimeXtUnit.Second));
69+
utilDate = DateXt.Minus(utilDate, new TimeXt(500, TimeXtUnit.Millis));
70+
TimeXt dateDifference = DateXt.Minus(java.util.Date date1, java.util.Date date2);
6771

6872
// Timer
6973
Timer timer = new Timer();
7074
TimerTask timerTask = ...;
71-
TimerExtensions.Schedule(timer, timerTask, new Interval(10, Unit.Second));
75+
TimerXt.Schedule(timer, timerTask, new TimeXt(10, TimeXtUnit.Second));
7276
```
7377

7478
The library also includes extensions for Android's Handler class, this is only available if you compile the "lib-android" module.
7579

7680
```java
7781
Handler handler = new Handler()
7882
Runnable runnable = ...;
79-
HandlerExtensions.PostDelayed(handler, runnable, new Interval(2, Unit.Minute));
83+
HandlerXt.PostDelayed(handler, runnable, new TimeXt(2, TimeXtUnit.Minute));
8084
```
8185

8286
Since version 0.3.0 TimeXt has new extensions for the long type to display this number value in a human readable string format.
87+
In version 0.4.0 support for Integer and Double was added as well as the new method formatWeeks.
8388

8489
```java
85-
val readableStringFromMilliseconds = LongExtensions.formatMilliseconds(34325055574) // 56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds
86-
val readableStringFromSeconds = LongExtensions.formatSeconds(4350554) // 7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds
87-
val readableStringFromMinutes = LongExtensions.formatMinutes(432555) // 42 weeks, 6 days, 9 hours, 15 minutes
88-
val readableStringFromHours = LongExtensions.formatHours(4574) // 27 weeks, 1 day, 14 hours
90+
val readableStringFromMillis = LongXt.formatMillis(34325055574L) // 56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds
91+
val readableStringFromSeconds = LongXt.formatSeconds(4350554L) // 7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds
92+
val readableStringFromMinutes = LongXt.formatMinutes(432555L) // 42 weeks, 6 days, 9 hours, 15 minutes
93+
val readableStringFromHours = LongXt.formatHours(4574L) // 27 weeks, 1 day, 14 hours
94+
val readableStringFromDays = DoubleXt.formatDays(0.75) // 18 hours
95+
val readableStringFromWeeks = IntXt.formatWeeks(3) // 3 weeks
8996
```
9097

9198
## Requirements
@@ -119,3 +126,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
119126
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
120127
SOFTWARE.
121128
```
129+
130+
## Attention
131+
132+
Migrating from version 0.3.0 or lower to 0.4.0 or upper includes breaking changes.
133+
Class names where shortened from *Extensions to *Xt and unit enum was moved to another package (same as TimeXt) and renamed to TimeXtUnit.
134+
Furthermore all units smaller the unit second got shortened to *s (millis, micros, picos, nanos).

lib-android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 26
1010
targetSdkVersion 28
11-
versionCode 300
12-
versionName "0.3.0"
11+
versionCode 400
12+
versionName "0.4.0"
1313
}
1414

1515
buildTypes {

lib-android/src/main/java/guepardoapps/timext/java/extensions/HandlerExtensions.java

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package guepardoapps.timext.java.extensions;
2+
3+
import android.os.Handler;
4+
5+
import guepardoapps.timext.java.TimeXt;
6+
7+
public class HandlerXt {
8+
public static void PostDelayed(Handler handler, Runnable runnable, TimeXt delay) {
9+
handler.postDelayed(runnable, Math.round(delay.InMillis()));
10+
}
11+
}

lib/src/main/java/guepardoapps/timext/java/Interval.java

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package guepardoapps.timext.java;
2+
3+
public class TimeXt {
4+
5+
private double value;
6+
7+
public double GetValue() {
8+
return this.value;
9+
}
10+
11+
private TimeXtUnit unit;
12+
13+
public TimeXtUnit GetUnit() {
14+
return this.unit;
15+
}
16+
17+
public TimeXt(double value, TimeXtUnit unit) {
18+
this.value = value;
19+
this.unit = unit;
20+
}
21+
22+
public double InYears() { return this.value * this.unit.conversion / TimeXtUnit.Year.conversion; }
23+
24+
public double InWeeks() { return this.value * this.unit.conversion / TimeXtUnit.Week.conversion; }
25+
26+
public double InDays() {
27+
return this.value * this.unit.conversion / TimeXtUnit.Day.conversion;
28+
}
29+
30+
public double InHours() { return this.value * this.unit.conversion / TimeXtUnit.Hour.conversion; }
31+
32+
public double InMinutes() { return this.value * this.unit.conversion / TimeXtUnit.Minute.conversion; }
33+
34+
public double InSeconds() { return this.value * this.unit.conversion / TimeXtUnit.Second.conversion; }
35+
36+
public double InMillis() { return this.value * this.unit.conversion / TimeXtUnit.Millis.conversion; }
37+
38+
public double InMicros() { return this.value * this.unit.conversion / TimeXtUnit.Micros.conversion; }
39+
40+
public double InNanos() { return this.value * this.unit.conversion / TimeXtUnit.Nanos.conversion; }
41+
42+
public double InPicos() { return this.value * this.unit.conversion / TimeXtUnit.Picos.conversion; }
43+
44+
public TimeXt Plus(TimeXt timeXt) {
45+
this.value = ((this.InPicos() + timeXt.InPicos()) / this.unit.conversion) * TimeXtUnit.Picos.conversion;
46+
return this;
47+
}
48+
49+
public TimeXt Minus(TimeXt timeXt) {
50+
this.value = ((this.InPicos() - timeXt.InPicos()) / this.unit.conversion) * TimeXtUnit.Picos.conversion;
51+
return this;
52+
}
53+
54+
public TimeXt Times(double value) {
55+
this.value *= value;
56+
return this;
57+
}
58+
59+
public TimeXt Div(double value) {
60+
if (value == 0) {
61+
throw new ArithmeticException("Diversion value may not be 0!");
62+
}
63+
this.value /= value;
64+
return this;
65+
}
66+
67+
public TimeXt Inc() {
68+
this.value++;
69+
return this;
70+
}
71+
72+
public TimeXt Dec() {
73+
this.value--;
74+
return this;
75+
}
76+
77+
public int CompareTo(TimeXt interval) { return (int) (this.InPicos() - interval.InPicos()); }
78+
79+
public boolean Contains(TimeXt interval) { return this.InPicos() >= interval.InPicos(); }
80+
81+
public boolean Equals(TimeXt interval) { return interval != null && this.CompareTo(interval) == 0; }
82+
83+
public int HashCode() {
84+
return (int) this.InPicos();
85+
}
86+
}

lib/src/main/java/guepardoapps/timext/java/enums/Unit.java renamed to lib/src/main/java/guepardoapps/timext/java/TimeXtUnit.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package guepardoapps.timext.java.enums;
1+
package guepardoapps.timext.java;
22

3-
public enum Unit {
3+
public enum TimeXtUnit {
44
Year(365 * 24.0 * 60.0 * 60.0),
55
Week(7 * 24.0 * 60.0 * 60.0),
66
Day(24.0 * 60.0 * 60.0),
77
Hour(60.0 * 60.0),
88
Minute(60.0),
99
Second(1),
10-
MilliSecond(1e-3),
11-
MicroSecond(1e-6),
12-
NanoSecond(1e-9),
13-
PicoSecond(1e-12);
10+
Millis(1e-3),
11+
Micros(1e-6),
12+
Nanos(1e-9),
13+
Picos(1e-12);
1414

15-
Unit(double conversion) {
15+
TimeXtUnit(double conversion) {
1616
this.conversion = conversion;
1717
}
1818

0 commit comments

Comments
 (0)