feat: cache draft
This commit is contained in:
parent
82af876560
commit
6d0e8f9786
53
src/main/java/eirb/pg203/Cache.java
Normal file
53
src/main/java/eirb/pg203/Cache.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,13 @@ package eirb.pg203;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.concurrent.locks.Condition;
|
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 {
|
enum Condition {
|
||||||
SUNNY("☀️"),
|
SUNNY("☀️"),
|
||||||
PARTIAL("🌤"),
|
PARTIAL("🌤"),
|
||||||
@ -14,6 +20,25 @@ class WeatherData {
|
|||||||
private String desc;
|
private String desc;
|
||||||
Condition(String desc) { this.desc = 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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.desc;
|
return this.desc;
|
||||||
@ -31,6 +56,29 @@ class WeatherData {
|
|||||||
NW("🡬"),
|
NW("🡬"),
|
||||||
ERROR("E");
|
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;
|
private String desc;
|
||||||
WindDirection(String desc) {this.desc = desc;}
|
WindDirection(String desc) {this.desc = desc;}
|
||||||
|
|
||||||
@ -139,4 +187,25 @@ class WeatherData {
|
|||||||
this.getWindDirection().toString()
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
9
src/main/java/eirb/pg203/utils/JSONCachable.java
Normal file
9
src/main/java/eirb/pg203/utils/JSONCachable.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package eirb.pg203.utils;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public interface JSONCachable {
|
||||||
|
JSONObject toJSON();
|
||||||
|
|
||||||
|
JSONCachable fromJSON(JSONObject data);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user