Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
87c21272de | |||
3ffc40596a |
4 changed files with 128 additions and 3 deletions
|
@ -1,6 +1,5 @@
|
||||||
# PPJ
|
# PPJ
|
||||||
|
|
||||||
This repository exists to archive PJATK tasks for the PPJ curriculum.
|
This branch contains the solution for the **5th task**.
|
||||||
|
|
||||||
Each task can be found under its **corresponding branch**: `task/n`.
|
[Go back to main](/femsci/ppj/src/branch/nya)
|
||||||
Example: `task/2`.
|
|
31
dev/meowmeow/Kitku.java
Normal file
31
dev/meowmeow/Kitku.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
/*
|
||||||
|
* TASK 5
|
||||||
|
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/5
|
||||||
|
* by femsci
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.meowmeow;
|
||||||
|
|
||||||
|
public class Kitku {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// let's play with silly datestamps and serialization
|
||||||
|
// 2127 + 10
|
||||||
|
|
||||||
|
LimitedDate from = new LimitedDate(3, 2, 2000),
|
||||||
|
to = new LimitedDate(29, 11, 2127);
|
||||||
|
|
||||||
|
System.out.printf("**Original**\n%s -> %s\n\n", from, to);
|
||||||
|
|
||||||
|
// for more explicitness let's stress test the system by redundant serde ops
|
||||||
|
int spanRaw = new LimitedDateSpan(from, to).rawSpan();
|
||||||
|
|
||||||
|
LimitedDateSpan theActualSpanAbstraction = new LimitedDateSpan(spanRaw);
|
||||||
|
|
||||||
|
// here we perform the another batch of deserialization operations :3
|
||||||
|
LimitedDate fromde = theActualSpanAbstraction.dateFrom(),
|
||||||
|
tode = theActualSpanAbstraction.dateTo();
|
||||||
|
|
||||||
|
System.out.printf("**Reconstructed**\n%s -> %s\n", fromde, tode);
|
||||||
|
}
|
||||||
|
}
|
51
dev/meowmeow/LimitedDate.java
Normal file
51
dev/meowmeow/LimitedDate.java
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* TASK 5
|
||||||
|
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/5
|
||||||
|
* by femsci
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.meowmeow;
|
||||||
|
|
||||||
|
public class LimitedDate {
|
||||||
|
public LimitedDate(int day, int month, int year) {
|
||||||
|
if (year < 2000 || year > 2127) {
|
||||||
|
throw new IllegalArgumentException("year must be within [2000..2127]");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (month < 1 || month > 12) {
|
||||||
|
throw new IllegalArgumentException("month must be within [1..12]");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (day < 1 || day > 31) {
|
||||||
|
throw new IllegalArgumentException("day must be within [1..31]");
|
||||||
|
}
|
||||||
|
this.day = day;
|
||||||
|
this.month = month;
|
||||||
|
this.year = year;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int day, month, year;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%02d.%02d.%04d", day, month, year);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* the structure (sadly java does not support unsigned integers):
|
||||||
|
* | octet 1 .| octet 2. |
|
||||||
|
* | 01234567 | 89abcdef |
|
||||||
|
* | Yyyyyyym | mmmddddd |
|
||||||
|
*/
|
||||||
|
public short toSillyDatestamp() {
|
||||||
|
int yr = year - 2000;
|
||||||
|
return (short) ((yr << 9) | (month << 5) | (day));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LimitedDate fromSillyDatestamp(short datestamp) {
|
||||||
|
int day = datestamp & 0x1f;
|
||||||
|
int month = (datestamp >> 5) & 0xf;
|
||||||
|
int year = (datestamp >> 9) & 0x7f;
|
||||||
|
return new LimitedDate(day, month, year + 2000);
|
||||||
|
}
|
||||||
|
}
|
44
dev/meowmeow/LimitedDateSpan.java
Normal file
44
dev/meowmeow/LimitedDateSpan.java
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* TASK 5
|
||||||
|
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/5
|
||||||
|
* by femsci
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.meowmeow;
|
||||||
|
|
||||||
|
public class LimitedDateSpan {
|
||||||
|
|
||||||
|
// we still don't account for the transitions and abominable human-made
|
||||||
|
// exceptions
|
||||||
|
public LimitedDateSpan(LimitedDate from, LimitedDate to) {
|
||||||
|
this(from.toSillyDatestamp(), to.toSillyDatestamp());
|
||||||
|
}
|
||||||
|
|
||||||
|
LimitedDateSpan(short rawFrom, short rawTo) {
|
||||||
|
this((rawFrom << 16) | Short.toUnsignedInt(rawTo));
|
||||||
|
}
|
||||||
|
|
||||||
|
LimitedDateSpan(int raw) {
|
||||||
|
this.serializedData = raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
// let's pretend that we need some weird optimization scheme, despite GC being
|
||||||
|
// finally smart enough
|
||||||
|
|
||||||
|
private final int serializedData;
|
||||||
|
|
||||||
|
public LimitedDate dateFrom() {
|
||||||
|
return LimitedDate.fromSillyDatestamp((short) ((serializedData >> 16) & 0xffff));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LimitedDate dateTo() {
|
||||||
|
return LimitedDate.fromSillyDatestamp((short) (serializedData & 0xffff));
|
||||||
|
}
|
||||||
|
|
||||||
|
int rawSpan() {
|
||||||
|
return serializedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// span calc methods etc...
|
||||||
|
// years(), days()...
|
||||||
|
}
|
Loading…
Reference in a new issue