Last active
December 17, 2015 00:00
-
-
Save holyjak/5518100 to your computer and use it in GitHub Desktop.
Simple vs. Easy: Writing A Generic Code To Avoid Duplication - replacing a number of anemic data structures with a generic one - BEFORE See http://wondersofcode.wordpress.com/2013/05/04/simple-vs-easy…id-duplication/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface LogEntry { | |
Date getTimestamp(); | |
/** For dumping into a .tsv file to be imported into Hadoop */ | |
String toTabDelimitedString(); | |
String getSearchTerms(); | |
boolean hasSearchTerms(); | |
String[][] getColumns(); | |
String getKey(); | |
void mergeWith(LogEntry entry); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Another impl. of LogEntry, to showcase the conceptual and, | |
// to a degree, factual duplication | |
public class PlayerEventLogEntry implements LogEntry { | |
public static final String[][] COLUMNS = { ... } | |
private String userId; | |
private String contentId; | |
private String sessionId; | |
... | |
public PlayerEventLogEntry(...) { /* assign all fields ... */ } | |
@Override | |
public String[][] getColumns() { | |
return COLUMNS; | |
} | |
... | |
@Override | |
public String toTabDelimitedString() { | |
StringBuilder sb=new StringBuilder(); | |
sb.append(contentId); | |
sb.append(FIELD_DELIMITER); | |
sb.append(userId); | |
sb.append(FIELD_DELIMITER); | |
sb.append(sessionId); | |
sb.append(FIELD_DELIMITER); | |
... | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Called from a data import job to process individual log lines. | |
// Some time later, the job will call toTabDelimitedString on it. | |
public class PlayerEventsParser ... { | |
@Override | |
public LogEntry parse(String logLine) throws LogParseException { | |
... // tokenizing, processing etc. of the data ... | |
return new PlayerEventLogEntry(userId, contentId, sessionId, ...); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// One of 15 implementations of LogEntry for import of right granted event logs | |
public class RightGrantedLogEntry implements LogEntry { | |
private static final char FIELD_DELIMITER = '\t'; | |
public static final String[][] COLUMNS = { { "messageId", "STRING" } | |
{ "userId", "STRING" },{ "rightId", "STRING" }, ... }; | |
private String messageId; | |
private Date timestamp; | |
private String userId; | |
private String rightId; | |
... | |
public RightGrantedLogEntry(String messageId, Date timestamp, String userId, String rightId, ...) { | |
this.messageId=messageId; | |
this.timestamp=timestamp; | |
this.userId=userId; | |
this.rightId=rightId; | |
... | |
} | |
@Override | |
public String[][] getColumns() { | |
return RightGrantedLogEntry.COLUMNS; | |
} | |
@Override | |
public String getKey() { | |
return messageId; | |
} | |
@Override | |
public String getSearchTerms() { | |
return null; | |
} | |
@Override | |
public Date getTimestamp() { | |
return timestamp; | |
} | |
@Override | |
public boolean hasSearchTerms() { | |
return false; | |
} | |
@Override | |
public void mergeWith(LogEntry arg0) {} | |
@Override | |
public String toTabDelimitedString() { | |
StringBuilder sb=new StringBuilder(); | |
sb.append(messageId); | |
sb.append(FIELD_DELIMITER); | |
sb.append(userId); | |
sb.append(FIELD_DELIMITER); | |
sb.append(rightId); | |
sb.append(FIELD_DELIMITER); | |
... | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment