Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Represents the filter that operates the filter functions for messages
- *
- * @author Muhammad
- */
- public class Filter {
- String filterType;
- String argumentValue;
- //Constructor that takes a parameter of filter type and argument value.
- public Filter(String filterType, String argumentValue) {
- this.filterType = filterType;
- this.argumentValue = argumentValue;
- }
- //Method that filters a conversation by a specific user and return filterd conversation.
- public static Conversation filterByUser(Conversation conversation, String specificUser) {
- List<Message> messageList = new ArrayList<>();
- //Filter by used id
- for (Message message : conversation.messages) {
- if (message.senderId.equals(specificUser)) {
- messageList.add(message);
- Conversation filteredConversation = new Conversation(conversation.name, messageList);
- conversation = filteredConversation;
- }
- }
- return conversation;
- }
- //Method that filters a conversation that contains a specific keywod and returns filterd conversation.
- public static Conversation filterByWord(Conversation conversation, String specificWord) {
- List<Message> messageList = new ArrayList<>();
- //Filter by keyword
- for (Message message : conversation.messages) {
- if (message.content.contains(specificWord)) {
- messageList.add(message);
- Conversation filteredConversation = new Conversation(conversation.name, messageList);
- conversation = filteredConversation;
- }
- }
- return conversation;
- }
- //Method that hides a word in a conversation by a specificword
- public static Conversation hideWord(Conversation conversation, String specificWord) {
- List<Message> messageList = new ArrayList<>();
- //Filter by used id
- for (Message message : conversation.messages) {
- if (message.content.contains(specificWord)) {
- message.content = message.content.replaceAll(specificWord, "*redacted*");
- messageList.add(message);
- Conversation filteredConversation = new Conversation(conversation.name, messageList);
- conversation = filteredConversation;
- }
- }
- return conversation;
- }
- }
- private void filter(Filter filter, Conversation conversation, String outputFilePath) throws Exception {
- String filterType = filter.filterType; //used to get the type of filter
- String argumentValue = filter.argumentValue;
- //Filterers
- switch (filterType) {
- case "filteruser":
- conversation = Filter.filterByUser(conversation, argumentValue);
- this.writeConversation(conversation, outputFilePath);
- break;
- case "filterword":
- conversation = Filter.filterByWord(conversation, argumentValue);
- this.writeConversation(conversation, outputFilePath);
- break;
- case "hideword":
- conversation = Filter.hideWord(conversation, argumentValue);
- this.writeConversation(conversation, outputFilePath);
- break;
- default:
- this.writeConversation(conversation, outputFilePath);
- break;
- }
- }
- public enum FilterType {
- USER, WORD, HIDE_WORD
- }
- private void filter(Filter filter, Conversation conversation, String outputFilePath) {
- switch (filter.filterType) {
- case FilterType.USER:
- conversation = Filter.filterByUser(conversation, filter.argumentValue);
- writeConversation(conversation, outputFilePath);
- break;
- case FilterType.WORD:
- conversation = Filter.filterByWord(conversation filter.argumentValue);
- writeConversation(conversation, outputFilePath);
- break;
- // ...
- switch (filter.filterType) {
- // ...
- }
- this.writeConversation(conversation, outputFilePath);
- public interface Filter {
- Conversation apply(Conversation conversation, String argumentValue);
- }
- public class UserFilter implements Filter {
- @Override
- public Conversation apply(Conversation conversation, String specificUser) {
- List<Message> messageList = new ArrayList<>();
- //Filter by used id
- for (Message message : conversation.messages) {
- if (message.senderId.equals(specificUser)) {
- messageList.add(message);
- Conversation filteredConversation =
- new Conversation(conversation.name, messageList);
- conversation = filteredConversation;
- }
- }
- return conversation;
- }
- }
- private void filter(Filter filter, Conversation conversation, String outputFilePath) throws Exception {
- if (filter != null) {
- String argumentValue = filter.argumentValue;
- conv = filter.apply(conversation, argumentValue);
- }
- this.writeConversation(conversation, outputFilePath);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement