feat: cache draft

This commit is contained in:
Nemo D'ACREMONT 2024-11-23 13:19:33 +01:00
parent 82af876560
commit 6d0e8f9786
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
3 changed files with 132 additions and 1 deletions

View File

@ -0,0 +1,53 @@
package eirb.pg203;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
class Cache {
HashMap<String, String> hashMap;
public Cache() {
this.hashMap = new HashMap<>();
}
public boolean has(String key) {
return this.hashMap.get(key) == null;
}
public String get(String key) {
return this.hashMap.get(key);
}
public void set(String key, String value) {
this.hashMap.put(key, value);
}
public void flush() {
this.hashMap = new HashMap<>();
}
public void fromString(String input) {
JSONArray data = new JSONArray(input);
for (int i = 0 ; i < data.length() ; ++i)
{
JSONObject entry = data.getJSONObject(i);
String key = entry.getString("key");
String value = entry.getString("value");
this.hashMap.put(key, value);
}
}
public String exportString() {
JSONArray data = new JSONArray();
for (String key: this.hashMap.keySet())
data.put(this.hashMap.get(key));
return data.toString();
}
}

View File

@ -3,7 +3,13 @@ package eirb.pg203;
import java.time.Instant;
import java.util.concurrent.locks.Condition;
class WeatherData {
import org.json.JSONObject;
import com.sun.net.httpserver.Authenticator.Retry;
import eirb.pg203.utils.JSONCachable;
class WeatherData implements JSONCachable {
enum Condition {
SUNNY("☀️"),
PARTIAL("🌤"),
@ -14,6 +20,25 @@ class WeatherData {
private String desc;
Condition(String desc) { this.desc = desc; }
public static Condition fromString(String desc) {
switch (desc) {
case "☀️":
return SUNNY;
case "🌤":
return PARTIAL;
case "☁️":
return CLOUDY;
case "🌧":
return RAINY;
default:
return ERROR;
}
}
@Override
public String toString() {
return this.desc;
@ -31,6 +56,29 @@ class WeatherData {
NW("🡬"),
ERROR("E");
public static WindDirection fromString(String desc) {
switch (desc) {
case "🡩":
return N;
case "🡭":
return NE;
case "🡪":
return E;
case "🡮":
return SE;
case "🡫":
return S;
case "🡯":
return SW;
case "🡨":
return W;
case "🡬":
return NW;
default:
return ERROR;
}
}
private String desc;
WindDirection(String desc) {this.desc = desc;}
@ -139,4 +187,25 @@ class WeatherData {
this.getWindDirection().toString()
);
}
@Override
public JSONObject toJSON() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("city", city.getCityName());
jsonObject.put("date", date.toString());
jsonObject.put("temp", temp);
jsonObject.put("condition", condition.toString());
jsonObject.put("windSpeed", windSpeed);
jsonObject.put("windDirectionAngle", windDirectionAngle);
jsonObject.put("windDirection", windDirection.toString());
return null;
}
@Override
public JSONCachable fromJSON(JSONObject data) {
return null;
}
}

View File

@ -0,0 +1,9 @@
package eirb.pg203.utils;
import org.json.JSONObject;
public interface JSONCachable {
JSONObject toJSON();
JSONCachable fromJSON(JSONObject data);
}