Diff
checker
文本
文本
圖像
文檔
Excel
文件夾
Legal
Enterprise
桌面版
定價
登入
下載 Diffchecker 桌面版
比較文本
尋找兩個文字檔案之間的差異
工具
歷史
即時編輯器
摺疊未變更行
關閉換行
檢視
拆分
統一
比對精度
智能
單詞
字符
語法突出顯示
選擇語法
忽略
文字轉換
前往第一個差異
編輯輸入
Diffchecker Desktop
執行Diffchecker最安全的方式。取得Diffchecker桌面應用程式:您的差異永遠不會離開您的電腦!
取得桌面版
Untitled diff
建立於
10 年前
差異永不過期
清除
匯出
分享
解釋
5 刪除
行
總計
刪除
字符
總計
刪除
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
145 行
全部複製
9 新增
行
總計
新增
字符
總計
新增
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
149 行
全部複製
package BotFarmServer;
package BotFarmServer;
import java.io.IOException;
import java.io.IOException;
import java.net.URL;
import java.net.URL;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map;
import java.util.Scanner;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.Pattern;
/**
/**
* Utility class for access to the Grand Exchange API.
* Utility class for access to the Grand Exchange API.
*/
*/
public class GrandExchangeApi {
public class GrandExchangeApi {
private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" +
private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" +
"/api/catalogue/detail.json?item=%d";
"/api/catalogue/detail.json?item=%d";
private static final long TEN_MINUTES = 600000;
private static final long TEN_MINUTES = 600000;
private final Map<Integer, GELookupResult> cache;
private final Map<Integer, GELookupResult> cache;
private long startTime;
private long startTime;
/**
/**
* Caching-enabled default constructor
* Caching-enabled default constructor
*/
*/
public GrandExchangeApi() {
public GrandExchangeApi() {
this(true);
this(true);
}
}
/**
/**
* Creates a new Grand Exchange API instance. Starts cache-refresh timer.
* Creates a new Grand Exchange API instance. Starts cache-refresh timer.
* @[member='param'] cache Whether to enable caching of results.
* @[member='param'] cache Whether to enable caching of results.
*/
*/
public GrandExchangeApi(boolean cache) {
public GrandExchangeApi(boolean cache) {
startTime = System.currentTimeMillis();
startTime = System.currentTimeMillis();
this.cache = cache ? new HashMap<>() : null;
this.cache = cache ? new HashMap<>() : null;
}
}
/**
/**
* If caching is enabled, clears the cache so that new results are fetched on lookup.
* If caching is enabled, clears the cache so that new results are fetched on lookup.
*/
*/
public void flushCache() {
public void flushCache() {
複製
已複製
複製
已複製
long currentTime = System.currentTimeMillis();
if(cache != null) {
if(cache != null) {
複製
已複製
複製
已複製
cache.clear();
for (Map.Entry<int,GELookupResult>> entry : cache) {
if (currentTime - TEN_MINUTES) > entry.getValue().lastUpdatedAt) {
cache.remove(entry.getKey()); // item is old. it needs to be refreshed
}
}
}
}
}
}
/**
/**
* Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result.
* Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result.
* @[member='param'] itemId the id to look up.
* @[member='param'] itemId the id to look up.
* @return the result returned by the api. May be null if an error has occurred.
* @return the result returned by the api. May be null if an error has occurred.
*/
*/
public GELookupResult lookup(int itemId) {
public GELookupResult lookup(int itemId) {
複製
已複製
複製
已複製
if((System.currentTimeMillis() - TEN_MINUTES) > startTime){ //Flush cache after 10 minutes. Auto-update prices.
flushCache();
flushCache();
startTime = System.currentTimeMillis();
}
if(cache != null && !cache.isEmpty()) {
if(cache != null && !cache.isEmpty()) {
GELookupResult result = cache.get(itemId);
GELookupResult result = cache.get(itemId);
if(result != null) {
if(result != null) {
return result;
return result;
}
}
}
}
String json;
String json;
try {
try {
URL url = new URL(String.format(API_LOCATION, itemId));
URL url = new URL(String.format(API_LOCATION, itemId));
Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A");
Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A");
json = scan.next();
json = scan.next();
scan.close();
scan.close();
} catch(IOException e) {
} catch(IOException e) {
return null;
return null;
}
}
GELookupResult result = parse(itemId, json);
GELookupResult result = parse(itemId, json);
if(cache != null) {
if(cache != null) {
cache.put(itemId, result);
cache.put(itemId, result);
}
}
return result;
return result;
}
}
/**
/**
* Parses a GELookupResult from the JSON returned by the API.
* Parses a GELookupResult from the JSON returned by the API.
* @[member='param'] itemId The item ID.
* @[member='param'] itemId The item ID.
* @[member='param'] json The JSON returned by the RuneScape's API.
* @[member='param'] json The JSON returned by the RuneScape's API.
* @return The serialized result.
* @return The serialized result.
*/
*/
private static GELookupResult parse(int itemId, String json) {
private static GELookupResult parse(int itemId, String json) {
Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
Matcher m = pattern.matcher(json);
Matcher m = pattern.matcher(json);
Map<String, String> results = new HashMap<>();
Map<String, String> results = new HashMap<>();
while(m.find()) {
while(m.find()) {
results.put(m.group("key"), m.group("value"));
results.put(m.group("key"), m.group("value"));
}
}
int price = 0;
int price = 0;
Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json);
Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json);
if (priceMatcher.find()) {
if (priceMatcher.find()) {
price = Integer.parseInt(priceMatcher.group("price"));
price = Integer.parseInt(priceMatcher.group("price"));
}
}
return new GELookupResult(
return new GELookupResult(
results.get("icon"),
results.get("icon"),
results.get("icon_large"),
results.get("icon_large"),
results.get("type"),
results.get("type"),
results.get("typeIcon"),
results.get("typeIcon"),
results.get("name"),
results.get("name"),
results.get("description"),
results.get("description"),
Boolean.parseBoolean(results.get("members")),
Boolean.parseBoolean(results.get("members")),
itemId,
itemId,
price
price
);
);
}
}
/**
/**
* A class representing a result from an API lookup.
* A class representing a result from an API lookup.
*/
*/
public static final class GELookupResult {
public static final class GELookupResult {
public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription;
public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription;
public final boolean isMembers;
public final boolean isMembers;
public final int id, price;
public final int id, price;
複製
已複製
複製
已複製
public final long lastUpdatedAt;
private GELookupResult(String smallIconUrl,
private GELookupResult(String smallIconUrl,
String largeIconUrl,
String largeIconUrl,
String type,
String type,
String typeIcon,
String typeIcon,
String name,
String name,
String itemDescription,
String itemDescription,
boolean isMembers,
boolean isMembers,
int id,
int id,
int price) {
int price) {
this.smallIconUrl = smallIconUrl;
this.smallIconUrl = smallIconUrl;
this.largeIconUrl = largeIconUrl;
this.largeIconUrl = largeIconUrl;
this.type = type;
this.type = type;
this.typeIcon = typeIcon;
this.typeIcon = typeIcon;
this.name = name;
this.name = name;
this.itemDescription = itemDescription;
this.itemDescription = itemDescription;
this.isMembers = isMembers;
this.isMembers = isMembers;
this.id = id;
this.id = id;
this.price = price;
this.price = price;
複製
已複製
複製
已複製
this.lastUpdatedAt = System.currentTimeMillis();
}
}
}
}
}
}
已保存差異
原始文本
開啟檔案
package BotFarmServer; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Utility class for access to the Grand Exchange API. */ public class GrandExchangeApi { private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" + "/api/catalogue/detail.json?item=%d"; private static final long TEN_MINUTES = 600000; private final Map<Integer, GELookupResult> cache; private long startTime; /** * Caching-enabled default constructor */ public GrandExchangeApi() { this(true); } /** * Creates a new Grand Exchange API instance. Starts cache-refresh timer. * @[member='param'] cache Whether to enable caching of results. */ public GrandExchangeApi(boolean cache) { startTime = System.currentTimeMillis(); this.cache = cache ? new HashMap<>() : null; } /** * If caching is enabled, clears the cache so that new results are fetched on lookup. */ public void flushCache() { if(cache != null) { cache.clear(); } } /** * Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result. * @[member='param'] itemId the id to look up. * @return the result returned by the api. May be null if an error has occurred. */ public GELookupResult lookup(int itemId) { if((System.currentTimeMillis() - TEN_MINUTES) > startTime){ //Flush cache after 10 minutes. Auto-update prices. flushCache(); startTime = System.currentTimeMillis(); } if(cache != null && !cache.isEmpty()) { GELookupResult result = cache.get(itemId); if(result != null) { return result; } } String json; try { URL url = new URL(String.format(API_LOCATION, itemId)); Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A"); json = scan.next(); scan.close(); } catch(IOException e) { return null; } GELookupResult result = parse(itemId, json); if(cache != null) { cache.put(itemId, result); } return result; } /** * Parses a GELookupResult from the JSON returned by the API. * @[member='param'] itemId The item ID. * @[member='param'] json The JSON returned by the RuneScape's API. * @return The serialized result. */ private static GELookupResult parse(int itemId, String json) { Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\""); Matcher m = pattern.matcher(json); Map<String, String> results = new HashMap<>(); while(m.find()) { results.put(m.group("key"), m.group("value")); } int price = 0; Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json); if (priceMatcher.find()) { price = Integer.parseInt(priceMatcher.group("price")); } return new GELookupResult( results.get("icon"), results.get("icon_large"), results.get("type"), results.get("typeIcon"), results.get("name"), results.get("description"), Boolean.parseBoolean(results.get("members")), itemId, price ); } /** * A class representing a result from an API lookup. */ public static final class GELookupResult { public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription; public final boolean isMembers; public final int id, price; private GELookupResult(String smallIconUrl, String largeIconUrl, String type, String typeIcon, String name, String itemDescription, boolean isMembers, int id, int price) { this.smallIconUrl = smallIconUrl; this.largeIconUrl = largeIconUrl; this.type = type; this.typeIcon = typeIcon; this.name = name; this.itemDescription = itemDescription; this.isMembers = isMembers; this.id = id; this.price = price; } } }
更改後文本
開啟檔案
package BotFarmServer; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Utility class for access to the Grand Exchange API. */ public class GrandExchangeApi { private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" + "/api/catalogue/detail.json?item=%d"; private static final long TEN_MINUTES = 600000; private final Map<Integer, GELookupResult> cache; private long startTime; /** * Caching-enabled default constructor */ public GrandExchangeApi() { this(true); } /** * Creates a new Grand Exchange API instance. Starts cache-refresh timer. * @[member='param'] cache Whether to enable caching of results. */ public GrandExchangeApi(boolean cache) { startTime = System.currentTimeMillis(); this.cache = cache ? new HashMap<>() : null; } /** * If caching is enabled, clears the cache so that new results are fetched on lookup. */ public void flushCache() { long currentTime = System.currentTimeMillis(); if(cache != null) { for (Map.Entry<int,GELookupResult>> entry : cache) { if (currentTime - TEN_MINUTES) > entry.getValue().lastUpdatedAt) { cache.remove(entry.getKey()); // item is old. it needs to be refreshed } } } } /** * Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result. * @[member='param'] itemId the id to look up. * @return the result returned by the api. May be null if an error has occurred. */ public GELookupResult lookup(int itemId) { flushCache(); if(cache != null && !cache.isEmpty()) { GELookupResult result = cache.get(itemId); if(result != null) { return result; } } String json; try { URL url = new URL(String.format(API_LOCATION, itemId)); Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A"); json = scan.next(); scan.close(); } catch(IOException e) { return null; } GELookupResult result = parse(itemId, json); if(cache != null) { cache.put(itemId, result); } return result; } /** * Parses a GELookupResult from the JSON returned by the API. * @[member='param'] itemId The item ID. * @[member='param'] json The JSON returned by the RuneScape's API. * @return The serialized result. */ private static GELookupResult parse(int itemId, String json) { Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\""); Matcher m = pattern.matcher(json); Map<String, String> results = new HashMap<>(); while(m.find()) { results.put(m.group("key"), m.group("value")); } int price = 0; Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json); if (priceMatcher.find()) { price = Integer.parseInt(priceMatcher.group("price")); } return new GELookupResult( results.get("icon"), results.get("icon_large"), results.get("type"), results.get("typeIcon"), results.get("name"), results.get("description"), Boolean.parseBoolean(results.get("members")), itemId, price ); } /** * A class representing a result from an API lookup. */ public static final class GELookupResult { public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription; public final boolean isMembers; public final int id, price; public final long lastUpdatedAt; private GELookupResult(String smallIconUrl, String largeIconUrl, String type, String typeIcon, String name, String itemDescription, boolean isMembers, int id, int price) { this.smallIconUrl = smallIconUrl; this.largeIconUrl = largeIconUrl; this.type = type; this.typeIcon = typeIcon; this.name = name; this.itemDescription = itemDescription; this.isMembers = isMembers; this.id = id; this.price = price; this.lastUpdatedAt = System.currentTimeMillis(); } } }
尋找差異