Skip to content

Commit 3fbaadd

Browse files
committed
added a skill type enum for getting mapped values to help prevent invalid parameters
1 parent 512fae3 commit 3fbaadd

8 files changed

Lines changed: 75 additions & 70 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ Simple open source Java wrapper for the OSRS portion of the [RuneScape](https://
1717

1818
**#1** - Loop all skills and print their data.
1919
```java
20-
Map<String, Skill> stats = RuneAPI.getStats(username);
20+
Map<Type, Skill> stats = RuneAPI.getStats(username);
2121

2222
for(Skill skill: stats.values()) {
23-
System.out.printf("%s - %,d - %,d - %,d \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
23+
System.out.printf("%s: #%,d - %,d - %,dxp \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
2424
}
2525
```
2626

2727
**#2** - Find specific skill and print its data.
2828
```java
29-
Map<String, Skill> stats = RuneAPI.getStats(username);
30-
Skill overall = stats.get("Overall");
29+
Map<Type, Skill> stats = RuneAPI.getStats(username);
30+
Skill overall = stats.get(Type.OVERALL);
3131

32-
System.out.printf("%s - %,d - %,d - %,d \n", overall.getName(), overall.getRank(), overall.getLevel(), overall.getExperience());
32+
System.out.printf("%s: #%,d - %,d - %,dxp \n", overall.getName(), overall.getRank(), overall.getLevel(), overall.getExperience());
3333
```
3434

3535
## Download
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.baseketbandit.runeapi;
22

33
import com.baseketbandit.runeapi.entity.Skill;
4+
import com.baseketbandit.runeapi.entity.Type;
45
import com.baseketbandit.runeapi.io.RequestHandler;
56

67
import java.util.Map;
78

89
public class RuneAPI {
910

10-
public static Map<String, Skill> getStats(String username) {
11+
public static Map<Type, Skill> getStats(String username) {
1112
return RequestHandler.doGetRequest(username.replace(" ", "%20"));
1213
}
1314
}

src/main/java/com/baseketbandit/runeapi/entity/Skill.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package com.baseketbandit.runeapi.entity;
22

33
public class Skill {
4-
5-
private final String name;
4+
private final Type name;
65
private final int rank;
76
private final int level;
87
private final int experience;
98

10-
public Skill(String name, int rank, int level, int experience) {
9+
public Skill(Type name, int rank, int level, int experience) {
1110
this.name = name;
1211
this.rank = rank;
1312
this.level = level;
1413
this.experience = experience;
1514
}
1615

1716
public String getName() {
18-
return name;
17+
return name.name;
1918
}
2019

2120
public int getRank() {

src/main/java/com/baseketbandit/runeapi/entity/Skills.java

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baseketbandit.runeapi.entity;
2+
3+
public enum Type {
4+
OVERALL("Overall"),
5+
ATTACK("Attack"),
6+
DEFENSE("Defense"),
7+
STRENGTH("Strength"),
8+
HITPOINTS("Hitpoints"),
9+
RANGED("Ranged"),
10+
PRAYER("Prayer"),
11+
MAGIC("Magic"),
12+
COOKING("Cooking"),
13+
WOODCUTTING("Woodcutting"),
14+
FLETCHING("Fletching"),
15+
FISHING("Fishing"),
16+
FIREMAKING("Firemaking"),
17+
CRAFTING("Crafting"),
18+
SMITHING("Smithing"),
19+
MINING("Mining"),
20+
HERBLORE("Herblore"),
21+
AGILITY("Agility"),
22+
THIEVING("Thieving"),
23+
SLAYER("Slayer"),
24+
FARMING("Farming"),
25+
RUNECRAFT("Runecraft"),
26+
HUNTER("Hunter"),
27+
CONSTRUCTION("Construction");
28+
29+
final String name;
30+
31+
Type(String name) {
32+
this.name = name;
33+
}
34+
}

src/main/java/com/baseketbandit/runeapi/io/Parser.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ class Parser {
1010
* Breaks down raw data from the API to a hash map of Skill objects.
1111
*
1212
* @param data raw data from the RuneScape API
13-
* @return Map<String, Skill> HashMap of objects of type Skill
13+
* @return Map<Type, Skill> Map of objects of type Skill
1414
*/
15-
static Map<String, Skill> parseStats(String data) {
16-
List<Enum> names = Arrays.asList(Skills.values());
17-
Map<String, Skill> skills = new HashMap<>();
18-
String[] raw = data.split("\n");
15+
static Map<Type, Skill> parseStats(String data) {
16+
List<Type> types = Arrays.asList(Type.values());
17+
EnumMap<Type, Skill> skillMap = new EnumMap<>(Type.class);
18+
String[] lineSplitData = data.split("\n");
1919

20-
for(int i = 0; i < raw.length; i++) {
21-
if(i < Skills.values().length) {
22-
String[] r = raw[i].split(",");
23-
skills.put(names.get(i).name(), new Skill(names.get(i).name(), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2])));
20+
for(int i = 0; i < lineSplitData.length; i++) {
21+
if(i < Type.values().length) {
22+
String[] r = lineSplitData[i].split(",");
23+
skillMap.put(types.get(i), new Skill(types.get(i), Integer.parseInt(r[0]), Integer.parseInt(r[1]), Integer.parseInt(r[2])));
2424
continue;
2525
}
2626
break;
2727
}
2828

29-
return skills;
29+
return skillMap;
3030
}
3131
}

src/main/java/com/baseketbandit/runeapi/io/RequestHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public class RequestHandler {
2020
* A OkHttp method used to connect to and return a HashMap of data from the server.
2121
*
2222
* @param username the username to do the request against
23-
* @return Map<String, Skill> HashMap of objects of type Skill or an `empty` map if no results are found.
23+
* @return Map<Type, Skill> Map of objects of type Skill or an `empty` map if no results are found.
2424
*/
25-
public static Map<String, Skill> doGetRequest(String username) {
25+
public static Map<Type, Skill> doGetRequest(String username) {
2626
try {
2727
Request request = new Request.Builder()
2828
.url(API_BASE + username)
2929
.build();
3030
Response response = client.newCall(request).execute();
3131

3232
if(response.code() != 200) {
33-
log.error(response.code() + " - No results found for username `" + username.replace("%20", " ") + "`");
33+
log.error(response.code() + ": No results found for username `" + username.replace("%20", " ") + "`");
3434
response.close();
3535
return new HashMap<>();
3636
}
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
11
package com.baseketbandit.runeapi;
22

33
import com.baseketbandit.runeapi.entity.Skill;
4+
import com.baseketbandit.runeapi.entity.Type;
45
import org.junit.Test;
56

6-
import java.util.HashMap;
7-
import java.util.List;
87
import java.util.Map;
98

10-
import static org.junit.Assert.*;
11-
129
public class RuneAPITest {
1310

1411
@Test
1512
public void getStats() {
16-
Map<String, Skill> stats = RuneAPI.getStats("xwr");
13+
Map<Type, Skill> stats = RuneAPI.getStats("xwr");
14+
assert(!stats.isEmpty());
15+
assert(stats.size() == 24);
16+
1717
for(Skill skill: stats.values()) {
18-
System.out.println(skill.getName() + " - " + skill.getLevel());
18+
System.out.printf("%s: #%,d - %,d - %,dxp \n", skill.getName(), skill.getRank(), skill.getLevel(), skill.getExperience());
1919
}
2020
}
2121

2222
@Test
2323
public void getStatsNotFound() {
24-
Map<String, Skill> stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz");
25-
for(Skill skill: stats.values()) {
26-
System.out.println(skill.getName() + " - " + skill.getLevel());
27-
}
24+
Map<Type, Skill> stats = RuneAPI.getStats("abcdefghijklmnopqrstuvwxyz");
25+
assert(stats.isEmpty());
2826
}
2927

3028
@Test
3129
public void getStatsNameSpace() {
32-
Map<String, Skill> stats = RuneAPI.getStats("Iron Lewiso");
30+
Map<Type, Skill> stats = RuneAPI.getStats("Iron Lewiso");
31+
assert(!stats.isEmpty());
32+
assert(stats.size() == 24);
33+
3334
for(Skill skill: stats.values()) {
3435
System.out.println(skill.getName() + " - " + skill.getLevel());
3536
}
3637
}
3738

3839
@Test
3940
public void getStatsMapped() {
40-
Map<String, Skill> stats = RuneAPI.getStats("xwr");
41-
Skill cooking = stats.get("Cooking");
42-
System.out.println(cooking.getLevel());
43-
}
41+
Map<Type, Skill> stats = RuneAPI.getStats("xwr");
42+
assert(!stats.isEmpty());
43+
assert(stats.size() == 24);
44+
45+
Skill cooking = stats.get(Type.COOKING);
46+
assert(!cooking.getName().equals(""));
4447

45-
@Test
46-
public void getStatsMappedLowercase() {
47-
Map<String, Skill> stats = RuneAPI.getStats("xwr");
48-
Skill cooking = stats.get("Cooking");
4948
System.out.println(cooking.getLevel());
5049
}
5150
}

0 commit comments

Comments
 (0)