Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. /**
  5. * Represents the filter that operates the filter functions for messages
  6. *
  7. * @author Muhammad
  8. */
  9. public class Filter {
  10.  
  11. String filterType;
  12. String argumentValue;
  13.  
  14. //Constructor that takes a parameter of filter type and argument value.
  15. public Filter(String filterType, String argumentValue) {
  16. this.filterType = filterType;
  17. this.argumentValue = argumentValue;
  18. }
  19.  
  20. //Method that filters a conversation by a specific user and return filterd conversation.
  21. public static Conversation filterByUser(Conversation conversation, String specificUser) {
  22. List<Message> messageList = new ArrayList<>();
  23. //Filter by used id
  24. for (Message message : conversation.messages) {
  25. if (message.senderId.equals(specificUser)) {
  26. messageList.add(message);
  27. Conversation filteredConversation = new Conversation(conversation.name, messageList);
  28. conversation = filteredConversation;
  29. }
  30. }
  31. return conversation;
  32. }
  33.  
  34. //Method that filters a conversation that contains a specific keywod and returns filterd conversation.
  35. public static Conversation filterByWord(Conversation conversation, String specificWord) {
  36. List<Message> messageList = new ArrayList<>();
  37. //Filter by keyword
  38. for (Message message : conversation.messages) {
  39. if (message.content.contains(specificWord)) {
  40. messageList.add(message);
  41. Conversation filteredConversation = new Conversation(conversation.name, messageList);
  42. conversation = filteredConversation;
  43. }
  44. }
  45. return conversation;
  46. }
  47.  
  48. //Method that hides a word in a conversation by a specificword
  49. public static Conversation hideWord(Conversation conversation, String specificWord) {
  50. List<Message> messageList = new ArrayList<>();
  51. //Filter by used id
  52. for (Message message : conversation.messages) {
  53. if (message.content.contains(specificWord)) {
  54. message.content = message.content.replaceAll(specificWord, "*redacted*");
  55. messageList.add(message);
  56. Conversation filteredConversation = new Conversation(conversation.name, messageList);
  57. conversation = filteredConversation;
  58. }
  59. }
  60. return conversation;
  61. }
  62. }
  63.  
  64. private void filter(Filter filter, Conversation conversation, String outputFilePath) throws Exception {
  65.  
  66. String filterType = filter.filterType; //used to get the type of filter
  67. String argumentValue = filter.argumentValue;
  68.  
  69. //Filterers
  70. switch (filterType) {
  71. case "filteruser":
  72. conversation = Filter.filterByUser(conversation, argumentValue);
  73. this.writeConversation(conversation, outputFilePath);
  74. break;
  75. case "filterword":
  76. conversation = Filter.filterByWord(conversation, argumentValue);
  77. this.writeConversation(conversation, outputFilePath);
  78. break;
  79. case "hideword":
  80. conversation = Filter.hideWord(conversation, argumentValue);
  81. this.writeConversation(conversation, outputFilePath);
  82. break;
  83. default:
  84. this.writeConversation(conversation, outputFilePath);
  85. break;
  86. }
  87.  
  88. }
  89.  
  90. public enum FilterType {
  91. USER, WORD, HIDE_WORD
  92. }
  93.  
  94. private void filter(Filter filter, Conversation conversation, String outputFilePath) {
  95. switch (filter.filterType) {
  96. case FilterType.USER:
  97. conversation = Filter.filterByUser(conversation, filter.argumentValue);
  98. writeConversation(conversation, outputFilePath);
  99. break;
  100. case FilterType.WORD:
  101. conversation = Filter.filterByWord(conversation filter.argumentValue);
  102. writeConversation(conversation, outputFilePath);
  103. break;
  104. // ...
  105.  
  106. switch (filter.filterType) {
  107. // ...
  108. }
  109. this.writeConversation(conversation, outputFilePath);
  110.  
  111. public interface Filter {
  112. Conversation apply(Conversation conversation, String argumentValue);
  113. }
  114.  
  115. public class UserFilter implements Filter {
  116. @Override
  117. public Conversation apply(Conversation conversation, String specificUser) {
  118. List<Message> messageList = new ArrayList<>();
  119. //Filter by used id
  120. for (Message message : conversation.messages) {
  121. if (message.senderId.equals(specificUser)) {
  122. messageList.add(message);
  123. Conversation filteredConversation =
  124. new Conversation(conversation.name, messageList);
  125. conversation = filteredConversation;
  126. }
  127. }
  128. return conversation;
  129. }
  130. }
  131.  
  132. private void filter(Filter filter, Conversation conversation, String outputFilePath) throws Exception {
  133. if (filter != null) {
  134. String argumentValue = filter.argumentValue;
  135. conv = filter.apply(conversation, argumentValue);
  136. }
  137. this.writeConversation(conversation, outputFilePath);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement