Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Renamed MultiObject to Pair
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshiCodes committed Jun 11, 2023
1 parent 8a8b6e3 commit 0591556
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/joshicodes/rja/RJABuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import de.joshicodes.rja.requests.rest.RestRequest;
import de.joshicodes.rja.util.HttpUtil;
import de.joshicodes.rja.util.JsonUtil;
import de.joshicodes.rja.util.MultiObject;
import de.joshicodes.rja.util.Pair;

import javax.annotation.Nullable;
import java.io.IOException;
Expand Down Expand Up @@ -304,7 +304,7 @@ public void run() {

}

public <T> MultiObject<Integer, JsonElement> makeRequest(RestRequest<T> request) {
public <T> Pair<Integer, JsonElement> makeRequest(RestRequest<T> request) {
try {
return HttpUtil.sendRequest(
apiUrl + request.getEndpoint(),
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/de/joshicodes/rja/cache/CacheMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.joshicodes.rja.cache;

import de.joshicodes.rja.util.MultiObject;
import de.joshicodes.rja.util.Pair;
import de.joshicodes.rja.util.TrippleMap;

import java.util.HashMap;
Expand All @@ -23,13 +23,13 @@ public void put(K key, T t, long lifespan) {
}

public T get(K key) {
MultiObject<T, Long> multiObject = map.get(key);
if(multiObject == null) return null;
if(multiObject.getSecond() < System.currentTimeMillis()) {
Pair<T, Long> pair = map.get(key);
if(pair == null) return null;
if(pair.getSecond() < System.currentTimeMillis()) {
map.remove(key);
return null;
}
return multiObject.getFirst();
return pair.getFirst();
}

public void remove(K key) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/joshicodes/rja/requests/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import de.joshicodes.rja.requests.packet.PingRequest;
import de.joshicodes.rja.requests.rest.RestRequest;
import de.joshicodes.rja.rest.RestAction;
import de.joshicodes.rja.util.MultiObject;
import de.joshicodes.rja.util.Pair;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.framing.CloseFrame;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void run() {
*/
public <T> T sendRequest(final RJA rja, RestRequest<T> request) {
final RJABuilder builder = this.rja;
MultiObject<Integer, JsonElement> multi = builder.makeRequest(request);
Pair<Integer, JsonElement> multi = builder.makeRequest(request);
JsonElement e = multi.getSecond();
int code = multi.getFirst();
if(e == null && (code < 200 || code >= 300)) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/de/joshicodes/rja/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class HttpUtil {
public static final String AUTH_HEADER_BOT = "X-Bot-Token";
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 OPR/77.0.4054.203";

public static MultiObject<Integer, JsonElement> sendRequest(String url, String method, @Nullable String authHeader, @Nullable String auth, JsonElement body) throws IOException, InterruptedException {
public static Pair<Integer, JsonElement> sendRequest(String url, String method, @Nullable String authHeader, @Nullable String auth, JsonElement body) throws IOException, InterruptedException {
return sendRequest(url, method, authHeader, auth, body, null);
}

public static MultiObject<Integer, JsonElement> sendRequest(String url, String method, @Nullable String authHeader, @Nullable String auth, JsonElement body, @Nullable HashMap<String, String> headers) throws IOException, InterruptedException {
public static Pair<Integer, JsonElement> sendRequest(String url, String method, @Nullable String authHeader, @Nullable String auth, JsonElement body, @Nullable HashMap<String, String> headers) throws IOException, InterruptedException {

HttpClient client = HttpClient.newHttpClient();
HttpRequest.Builder request = HttpRequest.newBuilder();
Expand Down Expand Up @@ -53,18 +53,18 @@ public static MultiObject<Integer, JsonElement> sendRequest(String url, String m

String res = response.body();
if(res == null || res.isEmpty() && (code < 200 || code >= 300)) {
return new MultiObject<>(code, null);
return new Pair<>(code, null);
}
if(!res.startsWith("{") && !res.startsWith("[")) {
return new MultiObject<>(code, null);
return new Pair<>(code, null);
}
JsonElement e = JsonParser.parseString(res);

if(e == null) {
e = new JsonObject();
}

return new MultiObject<>(code, e);
return new Pair<>(code, e);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param <B> The first value
* @param <C> The second value
*/
public record MultiObject<B, C>(B first, C second) {
public record Pair<B, C>(B first, C second) {

public B getFirst() {
return first;
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/de/joshicodes/rja/util/TrippleMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
*/
public class TrippleMap<K, B, C> {

private final HashMap<K, MultiObject<B, C>> map;
private final HashMap<K, Pair<B, C>> map;

public TrippleMap() {
map = new HashMap<>();
}

public void put(K key, B b, C c) {
map.put(key, new MultiObject<>(b, c));
map.put(key, new Pair<>(b, c));
}

public void put(K key, MultiObject<B, C> multiObject) {
map.put(key, multiObject);
public void put(K key, Pair<B, C> pair) {
map.put(key, pair);
}

public void remove(K key) {
Expand All @@ -33,38 +33,38 @@ public boolean containsKey(K key) {
}

public boolean containsFirst(K key, B b) {
MultiObject<B, C> multiObject = map.get(key);
if(multiObject == null) return false;
return multiObject.getFirst().equals(b);
Pair<B, C> pair = map.get(key);
if(pair == null) return false;
return pair.getFirst().equals(b);
}

public boolean containsSecond(K key, C c) {
MultiObject<B, C> multiObject = map.get(key);
if(multiObject == null) return false;
return multiObject.getSecond().equals(c);
Pair<B, C> pair = map.get(key);
if(pair == null) return false;
return pair.getSecond().equals(c);
}

public B getFirst(K key) {
MultiObject<B, C> multiObject = map.get(key);
if(multiObject == null) return null;
return multiObject.getFirst();
Pair<B, C> pair = map.get(key);
if(pair == null) return null;
return pair.getFirst();
}

public C getSecond(K key) {
MultiObject<B, C> multiObject = map.get(key);
if(multiObject == null) return null;
return multiObject.getSecond();
Pair<B, C> pair = map.get(key);
if(pair == null) return null;
return pair.getSecond();
}

public void clear() {
map.clear();
}

public MultiObject<B, C> get(K key) {
public Pair<B, C> get(K key) {
return map.get(key);
}

public HashMap<K, MultiObject<B, C>> getMap() {
public HashMap<K, Pair<B, C>> getMap() {
return map;
}

Expand Down

0 comments on commit 0591556

Please sign in to comment.