Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare Dart -> Kotlin -> Java
/**
* Definiert einen Zeitrahmen
*
* User: mikemitterer, Date: 16.10.13, Time: 10:29
*/
@DartClass(library = "mobiad_rest_ui.model", classname = "FromToDate")
data class FromToDate(
val from: DateTime,
val to: DateTime)
{
companion object {
//private val logger = LoggerFactory.getLogger(FromToDate::class.java.simpleName)
// This is a named CTOR in Dart
fun now(plus : Duration = Duration.standardHours(24)) : FromToDate {
return FromToDate(DateTime.now(), DateTime.now().plusHours(plus.standardHours.toInt()))
}
}
/**
* Zeitraum zwischen From und To in Stunden
*/
val periodInHours: Long
get() = difference.standardHours
/**
* Zeitraum zwischen From und To in Minuten
*/
val periodInMinutes: Long
get() = difference.standardMinutes
/**
* Sind wir noch im Zeitfenster - Bezugspunkt ist JETZT
*/
val isTimeLeft: Boolean
get() = timeLeftInMinutes > 0
/**
* Wie viel Zeit steht noch zur Verfügung - Bezugspunkt ist JETZT
*
* Es kann keinen negativen Wert geben. Ist Keine Zeit mehr verfügbar
* kommt 0 zurück
*/
val timeLeftInMinutes : Long
get() {
val now = DateTime.now()
val diffNowToFrom = Duration(now,to)
return maxOf(diffNowToFrom.standardMinutes,0)
}
/**
* Zeitdifferenz zwischen [from] und [to]
*
* Sample: to = 2020, from = 2010 diff = +10
*/
val difference: Duration
get() = Duration(from, to)
}
@JavaClass("at.mikemitterer.mobiad.library.model.job.TimeFrame")
@beanreflector
class TimeFrame extends Object with JsonTO {
// final _logger = new Logger('mobiad_rest_ui.model.TimeFrame');
final List<FromToDate> _timeframe = new List<FromToDate>();
TimeFrame();
TimeFrame.fromJson(final data) {
final Serializer serializer = new TimeFrameSerializer(this);
serializer.fromJson(data);
}
Map<String, dynamic> toJson() => (new TimeFrameSerializer(this)).toJson();
/// setDate löscht eventuell schon vorhandene Zeiten
void setDate(final FromToDate fromToDate) {
removeAll();
addDate(fromToDate);
}
void addDate(final FromToDate fromToDate) {
Validate.notNull(fromToDate);
_timeframe.add(fromToDate);
}
/// Wenn die periodToDelete in einen Timeframe fällt wird es gelöscht
void removeDate(final FromToDate periodToDelete) {
Validate.notNull(periodToDelete);
if (_timeframe.length == 0) {
return;
}
_timeframe.removeWhere((final FromToDate period) {
return ((periodToDelete.from.isBefore(period.from) || periodToDelete.from.isAtSameMomentAs(period.from)) && (periodToDelete.to.isAfter(period.to) || periodToDelete.to.isAtSameMomentAs(period.to) ));
});
}
void removeAll() => _timeframe.clear();
bool get isTimeLeft => timeLeft.inMinutes > 0;
// TODO: Testen und nach Java portieren
bool get isTimeSet => _timeframe.length > 0;
bool get isNoTimeSet => !isTimeSet;
DateTime get from {
Validate.isTrue(isTimeSet, "No time set for TimeFrame");
return _timeframe.first.from;
}
DateTime get to {
Validate.isTrue(isTimeSet, "No time set for TimeFrame");
return _timeframe.first.to;
}
/// Gibt die Zeit in Minuten zurück die zwischen "Jetzt" und der "To"-Zeit zur Verfügung
/// stehen.
///
/// Gibt es mehrere Zeiten wird der längste Zeitraum zurückgegeben.
Duration get timeLeft {
if(_timeframe.length == 0) {
return new Duration();
}
int _timeLeft = 0;
_timeframe.forEach((final FromToDate ftDate)
=> _timeLeft = Math.max(_timeLeft, ftDate.timeLeftInMinutes));
return new Duration(minutes: _timeLeft);
}
// --------------------------------------------------------------------------------------------
// for testing
List<FromToDate> get timeframe => _timeframe;
}
@KotlinClass("at.mikemitterer.mobiad.library.model.job.FromToDate")
class FromToDate extends Object with JsonTO {
// private da sonst die verschiedenen Konstruktoren nicht funktionieren.
DateTime _from;
DateTime _to;
FromToDate.now({final Duration plus: const Duration(hours: 24)})
: _from = new DateTime.now(), _to = new DateTime.now().add(plus);
FromToDate(this._from, this._to) {
Validate.notNull(_from);
Validate.notNull(_to);
}
FromToDate.fromJson(final data) {
final Serializer serializer = new FromToDateSerializer(this);
serializer.fromJson(data);
}
Map<String, dynamic> toJson() => (new FromToDateSerializer(this)).toJson();
DateTime get from => _from;
DateTime get to => _to;
/// Zeitraum zwischen From und To in Stunden
int get periodInHours {
return from.difference(to).inHours;
}
/// Zeitraum zwischen From und To in Minuten
int get periodInMinutes {
return from.difference(to).inMinutes;
}
/// Sind wir noch im Zeitfenster - Bezugspunkt ist JETZT
bool get isTimeLeft => (timeLeftInMinutes > 0);
/// Wie viel Zeit steht noch zur Verfügung - Bezugspunkt ist JETZT
///
/// Es kann keinen negativen Wert geben. Ist Keine Zeit mehr verfügbar
/// kommt 0 zurück
int get timeLeftInMinutes {
final now = new DateTime.now();
final int diffNowToFrom = to.difference(now).inMinutes;
return Math.max(0, diffNowToFrom);
}
/// Zeitdifferenz zwischen [from] und [to]
///
/// Sample: to = 2020, from = 2010 diff = +10
Duration get difference => to.difference(from);
bool operator ==(o) => o is FromToDate && o.from == from && o.to == to;
int get hashCode => hash2(from.hashCode, to.hashCode);
}
/**
* Für einen Job kann es mehrere Zeitfenster geben - diese Zeitfenster werden eben
* im Timeframe definiert
*
* User: mikemitterer, Date: 16.10.13, Time: 10:27
*/
@DartClass(library = "mobiad_rest_ui.model",classname = "TimeFrame")
public class TimeFrame {
@SuppressWarnings("unused")
private static Logger logger = LoggerFactory.getLogger(TimeFrame.class.getSimpleName());
private final List<FromToDate> timeframe;
public TimeFrame() {
timeframe = new ArrayList<FromToDate>();
}
/** setDate löscht eventuell schon vorhandene Zeiten */
public void setDate(final FromToDate fromToDate) {
removeAll();
addDate(fromToDate);
}
public void addDate(final FromToDate fromToDate) {
Validate.notNull(fromToDate);
timeframe.add(fromToDate);
}
/** Wenn die periodToDelete in einen Timeframe fällt wird es gelöscht */
public void removeDate(final FromToDate periodToDelete) {
Validate.notNull(periodToDelete);
for (final FromToDate period : timeframe) {
if ((periodToDelete.getFrom().isBefore(period.getFrom()) || periodToDelete.getFrom().isEqual(period.getFrom())) &&
(periodToDelete.getTo().isAfter(period.getTo()) || periodToDelete.getTo().isEqual(period.getTo()))) {
timeframe.remove(period);
}
}
}
public void removeAll() {
timeframe.clear();
}
public boolean isTimeLeft() {
return getTimeLeft().getStandardMinutes() > 0;
}
public boolean isTimeSet() {
return timeframe.size() > 0;
}
public boolean isNoTimeSet() {
return !isTimeSet();
}
public DateTime getFrom() {
Validate.isTrue(isTimeSet());
return timeframe.get(0).getFrom();
}
public DateTime getTo() {
Validate.isTrue(isTimeSet());
return timeframe.get(0).getTo();
}
/**
* Gibt die Zeit in Minuten zurück die zwischen "Jetzt" und der "To"-Zeit zur Verfügung
* stehen.
*
* Gibt es mehrere Zeiten wird der längste Zeitraum zurückgegeben.
*/
Duration getTimeLeft() {
if(timeframe.size() == 0) {
return new Duration(0);
}
long _timeLeft = 0;
for(final FromToDate date : timeframe) {
_timeLeft = Math.max(0,date.getTimeLeftInMinutes());
}
return Duration.standardMinutes(_timeLeft);
}
// - default -----------------------------------------------------------------------------------
List<FromToDate> getTimeFrame() {
return timeframe;
}
// - private -----------------------------------------------------------------------------------
}
main() {
// final Logger _logger = new Logger("test.TimeFrame");
configLogging();
final String timeframeAsJsonString = "{\n" +
" \"timeframe\" :\n" +
" [\n" +
" {\n" +
" \"from\" : \"2013-01-01T00:00:00.000+01:00\",\n" +
" \"to\" : \"2013-01-31T00:00:00.000+01:00\",\n" +
" \"active\" : true\n" +
" },\n" +
" {\n" +
" \"from\" : \"2013-02-01T00:00:00.000+01:00\",\n" +
" \"to\" : \"2013-02-28T00:00:00.000+01:00\"\n" +
" }\n" +
" ]\n" +
"}";
group('TimeFrame', () {
setUp(() {
});
test('> setDate', () {
final tf = new TimeFrame();
expect(tf.isTimeSet, isFalse);
tf.setDate(new FromToDate.now(plus: new Duration(hours: 48)));
expect(tf.isTimeSet, isTrue);
// Zwischen dem setzen und dem check vergeht Zeit
expect(tf.timeLeft.inHours,greaterThanOrEqualTo(47));
expect(tf.timeLeft.inHours,lessThanOrEqualTo(48));
tf.addDate(new FromToDate.now(plus: new Duration(hours: 72)));
expect(tf.timeLeft.inHours,greaterThanOrEqualTo(71));
expect(tf.timeLeft.inHours,lessThanOrEqualTo(72));
}); // end of 'setDate' test
test('> difference', () {
final tf = new TimeFrame();
tf.setDate(new FromToDate.now(plus: new Duration(hours: 24)));
expect(tf.timeframe.first.difference.inHours,lessThanOrEqualTo(24));
expect(tf.timeframe.first.difference.inHours,greaterThanOrEqualTo(23));
}); // end of 'difference' test
test('> is Time left', () {
final tf = new TimeFrame();
tf.addDate(new FromToDate.now());
expect(tf.isTimeLeft, isTrue);
}); // end of 'is Time left' test
test('> is no Time left', () {
final tf = new TimeFrame();
final ftd = new FromToDate(
new DateTime.now(),
(new DateTime.now()).subtract(new Duration(hours: 2)));
tf.addDate(ftd);
// ca. -120 min (es kann aber in diesem Fall keinen negativen Wert geben)
expect(ftd.timeLeftInMinutes,0);
expect(tf.isTimeLeft, isFalse);
}); // end of 'is no Time left' test
test('> Delete frame', () {
final tf = new TimeFrame();
tf.addDate(new FromToDate(new DateTime(2013, 1, 1, 0, 0), new DateTime(2013, 1, 31, 0, 0)));
tf.addDate(new FromToDate(new DateTime(2013, 2, 1, 0, 0), new DateTime(2013, 2, 28, 0, 0)));
tf.addDate(new FromToDate(new DateTime(2013, 3, 1, 0, 0), new DateTime(2013, 3, 31, 0, 0)));
expect(tf.timeframe.length,3);
tf.removeDate(new FromToDate(new DateTime(2013, 1, 31, 0, 0), new DateTime(2013, 3, 1, 0, 0)));
expect(tf.timeframe.length,2);
tf.removeDate(new FromToDate(new DateTime(2013, 1, 1, 0, 0), new DateTime(2013, 1, 31, 0, 0)));
expect(tf.timeframe.length,1);
}); // end of 'Delete frame' test
test('> Remove all frames', () {
final tf = new TimeFrame();
tf.addDate(new FromToDate(new DateTime(2013, 1, 1, 0, 0), new DateTime(2013, 1, 31, 0, 0)));
tf.addDate(new FromToDate(new DateTime(2013, 2, 1, 0, 0), new DateTime(2013, 2, 28, 0, 0)));
tf.addDate(new FromToDate(new DateTime(2013, 3, 1, 0, 0), new DateTime(2013, 3, 31, 0, 0)));
expect(tf.timeframe.length,3);
tf.removeAll();
expect(tf.timeframe.length,0);
}); // end of 'Remove all frames' test
test('> toJson', () {
final TimeFrame tf = new TimeFrame();
final fromToDate1 = new FromToDate(new DateTime(2013, 1, 1, 0, 0), new DateTime(2013, 1, 31, 0, 0));
final fromToDate2 = new FromToDate(new DateTime(2013, 2, 1, 0, 0), new DateTime(2013, 2, 28, 0, 0));
tf.addDate(fromToDate1);
tf.addDate(fromToDate2);
expect(tf.timeframe.length,2);
final json = tf.toString();
final tfFromJson = new TimeFrame.fromJson(json);
expect(tfFromJson,isNotNull);
expect(tfFromJson.timeframe.length,2);
expect(tfFromJson.timeframe[0].from.toString(),tf.timeframe[0].from.toString());
expect(tfFromJson.timeframe[0].to.toString(),tf.timeframe[0].to.toString());
final fromToDate3 = new FromToDate(new DateTime(2013, 2, 1, 0, 0), new DateTime(2013, 2, 28, 0, 0));
// No data-Class for Dart
expect(tfFromJson.timeframe[1],fromToDate3);
//logger.info(tf);
}); // end of 'toJson' test
test('> Deserialize', () {
final TimeFrame tf = new TimeFrame.fromJson(timeframeAsJsonString);
expect(tf,isNotNull);
expect(tf.timeframe.length,2);
}); // end of 'from Json' test
test('> Deserialize from Json', () {
final TimeFrame temp = new TimeFrame.fromJson(timeframeAsJsonString);
final json = temp.toJson();
final TimeFrame tf = new TimeFrame.fromJson(json);
expect(tf,isNotNull);
expect(tf.timeframe.length,2);
}); // end of 'from Json' test
});
// end 'TimeFrame' group
}
/**
* User: mikemitterer, Date: 18.10.13, Time: 09:34
*
* Bei diesen Tests werden TimeFrame UND!!!! FromToDate getestet.
* Grund: Läuft bei Dart auch so...
*/
class TimeFrameTest : Assert() {
private val timeframeAsJsonString = "{\n" +
" \"timeframe\" :\n" +
" [\n" +
" {\n" +
" \"from\" : \"2013-01-01T00:00:00.000+01:00\",\n" +
" \"to\" : \"2013-01-31T00:00:00.000+01:00\",\n" +
" \"active\" : true\n" +
" },\n" +
" {\n" +
" \"from\" : \"2013-02-01T00:00:00.000+01:00\",\n" +
" \"to\" : \"2013-02-28T00:00:00.000+01:00\"\n" +
" }\n" +
" ]\n" +
"}"
@Test
fun testSetDate() {
val tf = TimeFrame()
assertTrue(tf.isNoTimeSet)
tf.setDate(FromToDate.now(plus = Duration.standardHours(48)))
assertTrue(tf.isTimeSet)
assertEquals(tf.timeLeft.standardMinutes.toFloat(),48 * 60f,1f)
tf.addDate(FromToDate.now(plus = Duration.standardHours(72)))
assertEquals(tf.timeLeft.standardMinutes.toFloat(),72 * 60f,1f)
}
@Test
fun testDifference() {
val tf = TimeFrame()
tf.setDate(FromToDate.now(plus = Duration.standardHours(24)))
assertEquals(tf.timeLeft.standardMinutes.toFloat(),24 * 60f,1f)
}
@Test
@Throws(Exception::class)
fun testIsTimeLeft() {
val tf = TimeFrame()
tf.addDate(FromToDate.now())
assertTrue(tf.isTimeLeft)
}
@Test
@Throws(Exception::class)
fun testNotTimeLeft() {
val tf = TimeFrame()
Assert.assertFalse(tf.isTimeLeft)
tf.addDate(FromToDate(DateTime.now(), DateTime.now().minusHours(2)))
assertEquals(0,tf.timeLeft.standardMinutes)
assertFalse(tf.isTimeLeft)
}
@Test
@Throws(Exception::class)
fun testDeleteFrame() {
val tf = TimeFrame()
tf.addDate(FromToDate(DateTime(2013, 1, 1, 0, 0), DateTime(2013, 1, 31, 0, 0)))
tf.addDate(FromToDate(DateTime(2013, 2, 1, 0, 0), DateTime(2013, 2, 28, 0, 0)))
tf.addDate(FromToDate(DateTime(2013, 3, 1, 0, 0), DateTime(2013, 3, 31, 0, 0)))
logDatesInTimeFrame(tf)
tf.removeDate(FromToDate(DateTime(2013, 1, 31, 0, 0), DateTime(2013, 3, 1, 0, 0)))
Assert.assertEquals(2, tf.timeFrame.size.toLong())
tf.removeDate(FromToDate(DateTime(2013, 1, 1, 0, 0), DateTime(2013, 1, 31, 0, 0)))
Assert.assertEquals(1, tf.timeFrame.size.toLong())
logDatesInTimeFrame(tf)
}
@Test
@Throws(Exception::class)
fun testRemoveAllFrames() {
val tf = TimeFrame()
tf.addDate(FromToDate(DateTime(2013, 1, 1, 0, 0), DateTime(2013, 1, 31, 0, 0)))
tf.addDate(FromToDate(DateTime(2013, 2, 1, 0, 0), DateTime(2013, 2, 28, 0, 0)))
tf.addDate(FromToDate(DateTime(2013, 3, 1, 0, 0), DateTime(2013, 3, 31, 0, 0)))
Assert.assertEquals(3, tf.timeFrame.size.toLong())
tf.removeAll()
Assert.assertEquals(0, tf.timeFrame.size.toLong())
Assert.assertFalse(tf.isTimeLeft)
}
@Test
@Throws(Exception::class)
fun testToJson() {
val tf = TimeFrame()
val fromToDate1 = FromToDate(DateTime(2013, 1, 1, 0, 0), DateTime(2013, 1, 31, 0, 0))
val fromToDate2 = FromToDate(DateTime(2013, 2, 1, 0, 0), DateTime(2013, 2, 28, 0, 0))
tf.addDate(fromToDate1)
tf.addDate(fromToDate2)
val gsonBuilder = GsonBuilder()
gsonBuilder.registerTypeAdapter(DateTime::class.java, AdapterForDateTime())
val gson = gsonBuilder.create()
val tfAsJsonString = gson.toJson(tf)
logger.debug(tfAsJsonString)
val tfFromJson = gson.fromJson(tfAsJsonString, TimeFrame::class.java)
assertNotNull(tfFromJson)
assertEquals(2, tfFromJson.timeFrame.size.toLong())
assertEquals(tf.timeFrame[0].from.toString(), tfFromJson.timeFrame[0].from.toString())
val fromToDate3 = FromToDate(DateTime(2013, 2, 1, 0, 0), DateTime(2013, 2, 28, 0, 0))
// Kotlin data-Class
assertEquals(fromToDate3,tf.timeFrame[1])
}
@Test
@Throws(Exception::class)
fun testFromJson() {
val gsonBuilder = GsonBuilder()
gsonBuilder.registerTypeAdapter(DateTime::class.java, AdapterForDateTime())
val gson = gsonBuilder.create()
val tf = gson.fromJson(timeframeAsJsonString, TimeFrame::class.java)
Assert.assertNotNull(tf)
Assert.assertEquals(2, tf.timeFrame.size.toLong())
}
// - private -----------------------------------------------------------------------------------
private fun logDatesInTimeFrame(timeFrame: TimeFrame) {
for (fromToDate in timeFrame.timeFrame) {
logger.debug("From: {} to: {}", fromToDate.from.toString(), fromToDate.to.toString())
}
}
companion object {
private val logger = LoggerFactory.getLogger(TimeFrameTest::class.java.simpleName)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment