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(); } } }
查找差异