Skip to content

Commit

Permalink
New API version + codestyle of Utilities class (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfsblvt committed Nov 23, 2016
1 parent 35e140e commit 128bdcb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 25 deletions.
2 changes: 1 addition & 1 deletion BlossomsPokemonGoManager.iml
Expand Up @@ -12,7 +12,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.github.Wolfsblvt:PokeGOAPI-Java:251ab09691fbf754e887fcac186b9f38b59c95b8" level="project" />
<orderEntry type="library" name="Maven: com.github.Wolfsblvt:PokeGOAPI-Java:51d555860d68fa3507d39080a0b39c2bccd39917" level="project" />
<orderEntry type="library" name="Maven: svarzee.gps:gpsoauth:0.3" level="project" />
<orderEntry type="library" name="Maven: net.iharder:base64:2.3.9" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.9.0" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -107,7 +107,7 @@
<dependency>
<groupId>com.github.Wolfsblvt</groupId>
<artifactId>PokeGOAPI-Java</artifactId>
<version>251ab09691fbf754e887fcac186b9f38b59c95b8</version>
<version>51d555860d68fa3507d39080a0b39c2bccd39917</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
Expand Down
115 changes: 93 additions & 22 deletions src/me/corriekay/pokegoutil/utils/Utilities.java
Expand Up @@ -6,21 +6,39 @@

import org.apache.commons.lang3.StringUtils;

import me.corriekay.pokegoutil.utils.ui.ColorTransitioner;

import com.google.protobuf.InvalidProtocolBufferException;

/**
* The main Utilities class.
* Inside are utility global constants and basic utility functions that aren't enough to get extracted to its own file.
* <p>
* Dear DEVELOPER, if you decide to add another utility function here, please take a look if there are enough similar
* ones so that they can be extracted in their own class. Also do take a look if there isn't already a specialized
* utility class for what you want to add.
*/
public final class Utilities {


public static final int PERCENTAGE_FACTOR = 100;
public static final int CALCULATION_FACTOR_1000 = 1000;

private static final Random RANDOM = new Random(System.currentTimeMillis());

/** Prevent initializing this class. */
private Utilities() {
}

private static final Random random = new Random(System.currentTimeMillis());

public static boolean isEven(long i) {
return i % 2 == 0;
/**
* Checks is a number is even.
*
* @param number The number.
* @return True if Even, else false.
*/
public static boolean isEven(final long number) {
return number % 2 == 0;
}

/**
Expand All @@ -44,12 +62,12 @@ public static <T extends Comparable> T limit(final T min, final T value, final T
}

/**
* Takes to numbers and creates a decimal percentage of it (like 0.7542).
* Takes two numbers and creates a decimal percentage of it (like 0.7542).
* If the maximum is zero, then the percentage returned is 1.0, so highest possible.
*
* @param number The real part.
* @param maximum The maximum of the number.
* @return The percentage value
* @return The percentage value.
*/
public static double percentage(final double number, final double maximum) {
return (maximum != 0.0) ? number / maximum : 1.0;
Expand All @@ -68,54 +86,107 @@ public static String percentageWithTwoCharacters(final double decimalNumber) {
return (percentage < PERCENTAGE_FACTOR) ? StringUtils.leftPad(String.valueOf(percentage), 2, '0') : "XX";
}

/**
* Gets a random color.
* Notes to Developers: If there will be more color functions in the future, please extract them to a ColorHelper.
*
* @return A random color.
*/
public static Color randomColor() {
Color c = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255), 255);
return c;
final int maxColor = ColorTransitioner.MAX_COLOR;
final Color color = new Color(RANDOM.nextInt(maxColor), RANDOM.nextInt(maxColor), RANDOM.nextInt(maxColor), maxColor);
return color;
}

public static void sleep(int milliseconds) {
/**
* Let the thread sleep for a given amount of milliseconds.
* IMPORTANT: Don't call from main thread of the UI, otherwise the UI will freeze.
*
* @param milliseconds The milliseconds to sleep.
*/
public static void sleep(final int milliseconds) {
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void sleepRandom(int minMilliseconds, int maxMilliseconds) {
/**
* Let the thread sleep for a random amount between two given millisecond numbers.
* IMPORTANT: Don't call from main thread of the UI, otherwise the UI will freeze.
*
* @param minMilliseconds The minimum amount of milliseconds.
* @param maxMilliseconds The maximum amount of milliseconds
*/
public static void sleepRandom(final int minMilliseconds, final int maxMilliseconds) {
try {
int randomInt = getRandom(minMilliseconds, maxMilliseconds);
System.out.println("Waiting " + (randomInt / 1000.0F) + " seconds.");
final int randomInt = getRandom(minMilliseconds, maxMilliseconds);
System.out.println("Waiting " + ((double) randomInt / CALCULATION_FACTOR_1000) + " seconds.");
TimeUnit.MILLISECONDS.sleep(randomInt);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static int getRandom(int minMilliseconds, int maxMilliseconds) {
int from = Math.max(minMilliseconds, maxMilliseconds);
int to = Math.min(minMilliseconds, maxMilliseconds);
return random.nextInt((from - to) + 1) + to;
/**
* Returns a random number between two numbers.
*
* @param minimum The minimum number, inclusive.
* @param maximum The maximum number, inclusive.
* @return The random number.
*/
public static int getRandom(final int minimum, final int maximum) {
final int from = Math.min(minimum, maximum);
final int to = Math.max(minimum, maximum);
return RANDOM.nextInt((to - from) + 1) + from;
}

/**
* Returns a random number between two numbers.
*
* @param minimum The minimum number, inclusive.
* @param maximum The maximum number, inclusive.
* @return The random number.
*/
public static double getRandom(final double minimum, final double maximum) {
final double from = Math.min(minimum, maximum);
final double to = Math.max(minimum, maximum);
return RANDOM.nextDouble() * (to - from) + from;
}

public static String getRealExceptionMessage(Exception e) {
String message = e.getMessage();
if (e instanceof InvalidProtocolBufferException || "Contents of buffer are null".equals(message)) {
/**
* Abstracts the exception message and makes a more readable exception out of it for edge cases.
*
* @param exception The exception.
* @return The real exception message.
*/
public static String getRealExceptionMessage(final Exception exception) {
String message = exception.getMessage();
if (exception instanceof InvalidProtocolBufferException || "Contents of buffer are null".equals(message)) {
message = "Server hasn't responded in time. "
+ "Seems to be busy. "
+ "The action may have been successful though. (" + message + ")";
}
return message;
}

public static String concatString(char delimeter, String... strings) {
/**
* Concat a given count of string with given delimiter.
*
* @param delimiter The delimiter to put between the strings.
* @param strings The strings that should be combined.
* @return The combined string.
*/
public static String concatString(final char delimiter, final String... strings) {
if (strings.length == 0) {
return "";
}

String s = strings[0];
String combined = strings[0];
for (int i = 1; i < strings.length; i++) {
s += delimeter + strings[i];
combined += delimiter + strings[i];
}
return s;
return combined;
}
}
Expand Up @@ -45,7 +45,7 @@ public ColorTransitioner(final List<ColorPoint> colorPoints) {
* @return The transitioned color.
*/
public Color getColor(final double percentage) {
final double transitionPoint = Utilities.limit(0d, percentage, 1d);
final double transitionPoint = Utilities.limit(0.0, percentage, 1.0);
if (colors.size() == 1) {
return colors.get(0).color;
}
Expand Down

0 comments on commit 128bdcb

Please sign in to comment.