Skip to content

Commit

Permalink
Some lazy loading with static initializers;
Browse files Browse the repository at this point in the history
RegionTag.fromstring now ignores colons;
Auto-formatting.
  • Loading branch information
Agadar committed Feb 4, 2017
1 parent f5d7e1d commit 687c756
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class Resolution {
public String category;

/**
* Which council this resolution was send to. Has no value if this resolution
* is currently at vote.
* Which council this resolution was send to. Has no value if this
* resolution is currently at vote.
*/
@XmlElement(name = "COUNCIL")
@XmlJavaTypeAdapter(Council.Adapter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public enum Authority {
/**
* Reverse mapping.
*/
private final static Map<Character, Authority> CHARS_TO_ENUMS = new HashMap<>();
private final static Map<Character, Authority> CHARS_TO_ENUMS;

/**
* Static init for filling the reverse mapping.
*/
static {
CHARS_TO_ENUMS = new HashMap<>();
for (Authority auth : values()) {
CHARS_TO_ENUMS.put(auth.toChar(), auth);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ public enum CensusId {
/**
* Map for reverse look-up via id.
*/
private final static Map<Integer, CensusId> INTS_TO_ENUMS = new HashMap<>();
private final static Map<Integer, CensusId> INTS_TO_ENUMS;

/**
* Static 'constructor' for filling the reverse map.
*/
static {
INTS_TO_ENUMS = new HashMap<>();
for (CensusId censusId : values()) {
INTS_TO_ENUMS.put(censusId.id, censusId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public enum DelegateAction {
/**
* Map for reverse look-up.
*/
private final static Map<String, DelegateAction> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, DelegateAction> STRINGS_TO_ENUMS;

/**
* Static 'constructor' for filling the reverse map.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (DelegateAction delegateAction : values()) {
STRINGS_TO_ENUMS.put(delegateAction.stringValue, delegateAction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public enum DispatchCategory {
/**
* Map for reverse look-up.
*/
private final static Map<String, DispatchCategory> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, DispatchCategory> STRINGS_TO_ENUMS;

/**
* Static 'constructor' for filling the reverse map.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (DispatchCategory dispatchCategory : values()) {
STRINGS_TO_ENUMS.put(dispatchCategory.stringValue, dispatchCategory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public enum DispatchSubCategory {
/**
* Map for reverse look-up.
*/
private final static Map<String, DispatchSubCategory> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, DispatchSubCategory> STRINGS_TO_ENUMS;

/**
* Static 'constructor' for filling the reverse map.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (DispatchSubCategory dispatchSubCategory : values()) {
STRINGS_TO_ENUMS.put(dispatchSubCategory.stringValue, dispatchSubCategory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public enum EmbassiesRmbPermissions {
/**
* Reverse mapping.
*/
private final static Map<String, EmbassiesRmbPermissions> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, EmbassiesRmbPermissions> STRINGS_TO_ENUMS;

/**
* Static init for filling the reverse mapping.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (EmbassiesRmbPermissions status : values()) {
STRINGS_TO_ENUMS.put(status.toString(), status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public enum EmbassyStatus {
/**
* Reverse mapping.
*/
private final static Map<String, EmbassyStatus> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, EmbassyStatus> STRINGS_TO_ENUMS;

/**
* Static init for filling the reverse mapping.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (EmbassyStatus status : values()) {
STRINGS_TO_ENUMS.put(status.toString(), status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ public enum RegionTag {
/**
* Map for reverse look-up.
*/
private final static Map<String, RegionTag> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, RegionTag> STRINGS_TO_ENUMS;

/**
* Static 'constructor' for filling the reverse map.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (RegionTag regionTag : values()) {
STRINGS_TO_ENUMS.put(regionTag.stringValue, regionTag);
}
Expand All @@ -130,13 +131,14 @@ public String toString() {

/**
* Returns the RegionTag represented by the supplied string.
* Case-insensitive, and whitespaces are interpreted as underscores.
* Case-insensitive, and whitespaces are interpreted as underscores. Colons
* (':') are ignored.
*
* @param stringValue the supplied string.
* @return the RegionTag represented by the supplied string.
*/
public static RegionTag fromString(String stringValue) {
stringValue = stringValue.toLowerCase().replace(' ', '_');
stringValue = stringValue.toLowerCase().replace(' ', '_').replace(":", "");

if (!STRINGS_TO_ENUMS.containsKey(stringValue)) {
throw new IllegalArgumentException("'" + stringValue + "' cannot be parsed to this enum");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public enum RegionalMessageStatus {
/**
* Map for reverse look-up.
*/
private final static Map<Integer, RegionalMessageStatus> INTS_TO_ENUMS = new HashMap<>();
private final static Map<Integer, RegionalMessageStatus> INTS_TO_ENUMS;

/**
* Static 'constructor' for filling the reverse map.
*/
static {
INTS_TO_ENUMS = new HashMap<>();
for (RegionalMessageStatus regionalMessageStatus : values()) {
INTS_TO_ENUMS.put(regionalMessageStatus.intValue, regionalMessageStatus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public enum WorldAssemblyStatus {
/**
* Reverse mapping.
*/
private final static Map<String, WorldAssemblyStatus> STRINGS_TO_ENUMS = new HashMap<>();
private final static Map<String, WorldAssemblyStatus> STRINGS_TO_ENUMS;

/**
* Static init for filling the reverse mapping.
*/
static {
STRINGS_TO_ENUMS = new HashMap<>();
for (WorldAssemblyStatus status : values()) {
STRINGS_TO_ENUMS.put(status.toString(), status);
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/github/agadar/nationstates/query/APIQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ public abstract class APIQuery<Q extends APIQuery, R> extends AbstractQuery<Q, R
* instead of a burst-like pattern, we make this into 10 requests per 6.01
* seconds.
*/
protected static final RateLimiter RATE_LIMITER = new RateLimiter(10, 6010);
protected static final RateLimiter RATE_LIMITER;

/**
* Rate limiter for API calls when scraping. Reduces the rate limit further
* to just 1 request per second, as suggested by the official documentation.
*/
private static final DependantRateLimiter SCRAPING_RATE_LIMITER
= new DependantRateLimiter(1, 1000, RATE_LIMITER);
private static final DependantRateLimiter SCRAPING_RATE_LIMITER;

// Lazily initialize the rate limiters.
static {
RATE_LIMITER = new RateLimiter(10, 6010);
SCRAPING_RATE_LIMITER = new DependantRateLimiter(1, 1000, RATE_LIMITER);
}

/**
* The resource value, e.g. the nation's or region's name. Set by the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public abstract class AbstractQuery<Q extends AbstractQuery, R> {
/**
* The logger for this object.
*/
protected static final Logger LOGGER = Logger.getLogger(AbstractQuery.class.getName());
protected static final Logger LOGGER;

/**
* Base URL to NationStates.
*/
private static final String BASE_URL = "https://www.nationstates.net/";
private static final String BASE_URL;

// Lazily initialize fields.
static {
LOGGER = Logger.getLogger(AbstractQuery.class.getName());
BASE_URL = "https://www.nationstates.net/";
}

/**
* The return type of this Query's execute()-method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ public final class TelegramQuery extends APIQuery<TelegramQuery, Void> {
* telegram per 30 seconds. To make sure we're on the safe side, we reduce
* this to 1 telegram per 30.05 seconds.
*/
private static final DependantRateLimiter TELEGRAM_RATE_LIMITER
= new DependantRateLimiter(1, TIME_BETWEEN_TELEGRAMS, RATE_LIMITER);
private static final DependantRateLimiter TELEGRAM_RATE_LIMITER;

/**
* The rate limiter for recruitment telegrams. The mandated rate limit is 1
* telegram per 180 seconds. To make sure we're on the safe side, we reduce
* this to 1 telegram per 180.05 seconds.
*/
private static final DependantRateLimiter RECRUITMENT_TELEGRAM_RATE_LIMITER
= new DependantRateLimiter(1, TIME_BETWEEN_RECRUITMENT_TELEGRAMS, TELEGRAM_RATE_LIMITER);
private static final DependantRateLimiter RECRUITMENT_TELEGRAM_RATE_LIMITER;

// Lazily initialize the rate limiters.
static {
TELEGRAM_RATE_LIMITER = new DependantRateLimiter(1, TIME_BETWEEN_TELEGRAMS, RATE_LIMITER);
RECRUITMENT_TELEGRAM_RATE_LIMITER = new DependantRateLimiter(1, TIME_BETWEEN_RECRUITMENT_TELEGRAMS, TELEGRAM_RATE_LIMITER);
}

/**
* List of listeners.
Expand Down

0 comments on commit 687c756

Please sign in to comment.