Skip to content

Commit

Permalink
Merge cb4afdd into 53c8abd
Browse files Browse the repository at this point in the history
  • Loading branch information
tsoonjin committed Feb 17, 2016
2 parents 53c8abd + cb4afdd commit c33a513
Show file tree
Hide file tree
Showing 11 changed files with 892 additions and 823 deletions.
138 changes: 138 additions & 0 deletions src/main/java/backend/resource/TurboLabel.java
Expand Up @@ -4,11 +4,17 @@
import backend.resource.serialization.SerializableLabel;
import javafx.scene.Node;
import javafx.scene.control.Tooltip;

import org.eclipse.egit.github.core.Label;
import util.Utility;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class TurboLabel implements Comparable<TurboLabel> {
Expand Down Expand Up @@ -74,6 +80,12 @@ public TurboLabel(String repoId, SerializableLabel label) {
private void ______METHODS______() {
}

/**
* Extracts delimiters from a label that belongs to a group to classify
* a label as exclusive or not.
* @param name
* @return
*/
public static Optional<String> getDelimiter(String name) {

// Escaping due to constants not being valid regexes
Expand Down Expand Up @@ -211,4 +223,130 @@ public int hashCode() {
public int compareTo(TurboLabel o) {
return actualName.compareTo(o.getActualName());
}

/**
* Returns the TurboLabel in labels that matches labelName
* Assumption: the labelName matches exactly 1 TurboLabel
*
* @param labels
* @param labelName
* @return
*/
public static TurboLabel getMatchingTurboLabel(List<TurboLabel> labels, String labelName) {
assert labels.stream()
.filter(label -> label.getActualName().equals(labelName))
.findFirst()
.isPresent();
return labels.stream()
.filter(label -> label.getActualName().equals(labelName))
.findFirst()
.get();
}

public static List<String> getLabelsNameList(List<TurboLabel> labels) {
return labels.stream()
.map(TurboLabel::getActualName)
.collect(Collectors.toList());
}

/**
* Returns the list of labels which name contains labelName
* @param repoLabels
* @param labelName
* @return
*/
public static List<String> filterByPartialName(List<String> repoLabels, String labelName) {
return repoLabels
.stream()
.filter(name -> Utility.containsIgnoreCase(getName(name), labelName))
.collect(Collectors.toList());
}

/**
* Returns the list of labels which group contains labelGroup.
* Uses partial keyword matching and keyword is case-insensitive
* @param repoLabels
* @param labelGroup
* @return
*/
public static List<String> filterByPartialGroupName(List<String> repoLabels, String labelGroup) {
if (labelGroup.isEmpty()) return repoLabels;

return repoLabels
.stream()
.filter(name -> {
if (hasGroup(name)) {
return Utility.containsIgnoreCase(getGroup(name), labelGroup);
}
return false;
})
.collect(Collectors.toList());
}

/**
* Returns the first label that matches the keyword
* i.e. the label's group contains keyword's group and label's name contains keyword's name
* @param repoLabels
* @param keyword
* @return
*/
public static String getMatchedLabelName(List<String> repoLabels, String keyword) {
List<String> newMatchedLabels = new ArrayList<>();
newMatchedLabels.addAll(repoLabels);
newMatchedLabels = filterByPartialName(newMatchedLabels, getName(keyword));
newMatchedLabels = filterByPartialGroupName(newMatchedLabels, getGroup(keyword));
return newMatchedLabels.get(0);
}

/**
* Returns true if there is exactly 1 matching label for keyword
*
* A label is matching if:
* the label's group contains keyword's group and label's name contains keyword's name
* @param repoLabels
* @param keyword
* @return
*/
public static boolean hasExactlyOneMatchedLabel(List<String> repoLabels, String keyword) {
List<String> newMatchedLabels = new ArrayList<>();
newMatchedLabels.addAll(repoLabels);
newMatchedLabels = filterByPartialName(newMatchedLabels, getName(keyword));
newMatchedLabels = filterByPartialGroupName(newMatchedLabels, getGroup(keyword));
return newMatchedLabels.size() == 1;
}

/**
* Identifies if a label belongs to an exclusive group. Only one label
* from an exclusive group is allowed to be assigned to an issue
* @param labelName
* @return
*/
public static boolean hasExclusiveGroup(String labelName) {
return TurboLabel.getDelimiter(labelName).isPresent() && TurboLabel.getDelimiter(labelName).get().equals(".");
}

/**
* Determines the group that labelName belongs to
* A labelName is considered to be in a group if getDelimiter(labelName).isPresent() is true.
* @param labelName
* @return
*/
public static String getGroup(String labelName) {
if (!hasGroup(labelName)) return "";
return labelName.substring(0, labelName.indexOf(TurboLabel.getDelimiter(labelName).get()));
}

/**
* Returns the name of labelName after removing its group
* @param labelName
* @return
*/
public static String getName(String labelName) {
if (!hasGroup(labelName)) return labelName;
return labelName.substring(labelName.indexOf(TurboLabel.getDelimiter(labelName).get()) + 1);
}

public static boolean hasGroup(String name) {
return TurboLabel.getDelimiter(name).isPresent();
}
}

0 comments on commit c33a513

Please sign in to comment.