HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Filtering conversation by filter type and value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversationvaluetypefilterandfiltering

Problem

I have been assigned to developed a feature that filters a conversation. For example, I want to filter the conversation by user id then export it to something either JSON or text file. In this case, I created a class that handles the filters like this.

```
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 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 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;
}

//Meth

Solution

I want to mention only one thing especially:

You're using "magic string" to choose your filter types. Instead of doing that (because it's brittle) you are probably better off with an enum:

public enum FilterType {
    USER, WORD, HIDE_WORD
}


Your code would have to adjust a little, but to give a short look into the filter method:

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;
        // ...


This exposes another small improvement possibility in your code.

In every case you will call this.writeConversation with exactly the same arguments. You can move that to outside the switch-case block:

switch (filter.filterType) {
    // ...
}
this.writeConversation(conversation, outputFilePath);


Another last thing I want to recommend is using Path intead of String to refer to outputFilePath. This makes it blatantly obvious, that you're actually referring to a File. Strings are .. not Paths...

Code Snippets

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);

Context

StackExchange Code Review Q#135632, answer score: 4

Revisions (0)

No revisions yet.