Skip to content

Commit

Permalink
Made models serializable & some comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
widavies committed Jul 9, 2017
1 parent 611869d commit 68ecc26
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils;
package main;

/**
* @since 1.0.0
Expand Down
1 change: 0 additions & 1 deletion src/main/java/main/TBA.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import models.standard.Match;
import models.standard.Team;
import requests.*;
import utils.Constants;

/**
* This is the main interface for the API, let's talk about that.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/APIStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import lombok.Data;

import java.io.Serializable;

/**
*
* @since 1.0.0
* @author Will Davies
*/
@Data
public class APIStatus {
public class APIStatus implements Serializable {
/**
* Year of the current FRC season.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/Award.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class Award {
public class Award implements Serializable{
/**
* The name of the award as provided by FIRST. May vary for the same award type.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/AwardRecipient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.Data;

import java.io.Serializable;

/**
* An Award_Recipient object represents the team and/or person who received an award at an event.
*/
@Data
public class AwardRecipient {
public class AwardRecipient implements Serializable {
/**
* The TBA team key for the team that was given the award. May be null.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/District.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class District {
public class District implements Serializable {
/**
* The short identifier for the district.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/DistrictRanking.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import lombok.Data;

import java.io.Serializable;

/**
* Created by Will Davies on 7/7/2017.
*/
public class DistrictRanking {
public class DistrictRanking implements Serializable {
private String teamKey;
private long rank;
private long rookieBonus;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/EliminationAlliance.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class EliminationAlliance {
public class EliminationAlliance implements Serializable {
private String name;
private Backup[] backups;
private String[] declines;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/Media.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class Media {
public class Media implements Serializable {
/**
* TBA identifier for this media.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/Ranking.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.Data;

import java.io.Serializable;

/**
* Created by Will Davies on 7/7/2017.
*/
@Data
public class Ranking {
public class Ranking implements Serializable {
/**
* Number of times disqualified.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/SortInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import lombok.Data;

import java.io.Serializable;

/**
* List of year-specific values provided in the `sort_orders` array for each team.
*/
@Data
public class SortInfo {
public class SortInfo implements Serializable {
private String name;
private long precision;
}
4 changes: 3 additions & 1 deletion src/main/java/models/other/WLTRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class WLTRecord {
public class WLTRecord implements Serializable {
/**
* Number of losses.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/models/other/events/EventOPR.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

/**
Expand All @@ -10,7 +11,7 @@
* @author Will Davies
*/
@Data
public class EventOPR {
public class EventOPR implements Serializable {
/**
* The team's key
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/events/EventRanking.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import models.other.SortInfo;
import models.other.WLTRecord;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class EventRanking {
public class EventRanking implements Serializable {

/**
* List of rankings at the event.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/events/Webcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class Webcast {
public class Webcast implements Serializable {
private String type;
private String channel;
private String file;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/matches/MatchAlliance.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Data;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class MatchAlliance {
public class MatchAlliance implements Serializable {
/**
* Score for this alliance. Will be null or -1 for an unplayed match.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/models/other/teams/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.Data;

import java.io.Serializable;

@Data
public class Robot {
public class Robot implements Serializable {
/**
* Year this robot competed in.
*/
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/models/simple/SEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import lombok.Data;
import models.other.District;
import models.standard.Event;

import java.io.Serializable;
import java.util.Calendar;

/**
* This is the Simple Event Model, as defined by the V3 TBA api.
Expand All @@ -10,7 +14,7 @@
* @author Will Davies
*/
@Data
public class SEvent {
public class SEvent implements Serializable, Comparable<Event> {
/**
* TBA event key with the format yyyy[EVENT_CODE], where yyyy is the year, and EVENT_CODE is the event code of the event.
*/
Expand Down Expand Up @@ -54,4 +58,19 @@ public class SEvent {
* Year the event data is for.
*/
private long year;

public long getTimeInMillis(String date) {
String[] tokens = date.split("-");

Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(tokens[0]));
c.set(Calendar.MONTH, Integer.parseInt(tokens[1]));
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(tokens[2]));
return c.getTimeInMillis();
}

@Override
public int compareTo(Event o) {
return Long.compare(getTimeInMillis(startDate), getTimeInMillis(o.getStartDate()));
}
}
30 changes: 29 additions & 1 deletion src/main/java/models/simple/SMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import lombok.Data;
import models.other.matches.MatchAlliance;
import models.standard.Match;

import java.io.Serializable;

/**
* @since 1.0.0
* @author Will Davies
*/
@Data
public class SMatch {
public class SMatch implements Serializable, Comparable<Match> {
/**
* TBA event key with the format yyyy[EVENT_CODE]_[COMP_LEVEL]m[MATCH_NUMBER], where yyyy is the year, and EVENT_CODE is the event code of the event, COMP_LEVEL is (qm, ef, qf, sf, f), and MATCH_NUMBER is the match number in the competition level. A set number may append the competition level if more than one match in required per set.
*
Expand Down Expand Up @@ -54,4 +57,29 @@ public class SMatch {
* UNIX timestamp (seconds since 1-Jan-1970 00:00:00) of actual match start time.
*/
private long actualTime;

/**
* Sorts matches by:
* -Quals
* -Quarters
* -Semis
* -Finals
*/

@Override
public int compareTo(Match o) {
long localScore = matchNumber;
if(compLevel.equals("qf")) localScore += 1000;
else if(compLevel.equals("sf")) localScore += 10000;
else if(compLevel.equals("f")) localScore += 100000;
localScore += matchNumber;

long compareScore = o.getMatchNumber();
if(o.getCompLevel().equals("qf")) compareScore += 1000;
else if(o.getCompLevel().equals("sf")) compareScore += 10000;
else if(o.getCompLevel().equals("f")) compareScore += 100000;
compareScore += o.getMatchNumber();

return Long.compare(localScore, compareScore);
}
}
4 changes: 3 additions & 1 deletion src/main/java/models/simple/STeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import lombok.Data;

import java.io.Serializable;

/**
* The simple Team model as defined by the V3 TPA api.
*
* @since 1.0.0
* @author Will Davies
*/
@Data
public class STeam {
public class STeam implements Serializable {
/**
* TBA team key with the format frcXXXX with XXXX representing the team number.
*/
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/requests/OtherRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import models.other.APIStatus;
import utils.IO;
import utils.Parser;
import utils.exceptions.DataNotFoundException;

/**
* @since 1.0.0
Expand All @@ -15,7 +16,10 @@ public class OtherRequest extends Parser {
* @return APIStatus representing the state of the TBA API interface
*/
public APIStatus getStatus() {
return parseStatus(IO.doRequest("status"));

APIStatus status = parseStatus(IO.doRequest("status"));
if (status == null) throw new DataNotFoundException("Unable to fetch API status.");
return status;
}

/**
Expand All @@ -24,7 +28,9 @@ public APIStatus getStatus() {
* @return an Object (json formatted), representing the data received from the server
*/
public Object customCall(String URL) {
return IO.doRequest(URL);
Object o = IO.doRequest(URL);
if(o == null) throw new DataNotFoundException("No response for your custom URL call");
return o;
}

}
1 change: 1 addition & 0 deletions src/main/java/utils/IO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils;

import main.Constants;
import org.json.simple.parser.JSONParser;
import utils.exceptions.AuthTokenNotFoundException;

Expand Down
Loading

0 comments on commit 68ecc26

Please sign in to comment.