-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
faf7544
commit 239efc1
Showing
19 changed files
with
1,094 additions
and
460 deletions.
There are no files selected for viewing
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
210 changes: 210 additions & 0 deletions
210
warehouse/query-core/src/main/java/datawave/query/attributes/SummarySize.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
package datawave.query.attributes; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
import datawave.query.Constants; | ||
import datawave.query.postprocessing.tf.PhraseIndexes; | ||
|
||
/** | ||
* Represents options for a summary that have been specified within an #SUMMARY_SIZE function. An instance of {@link SummarySize} can easily be captured as a | ||
* parameter string using {@link SummarySize#toString()}, and transformed back into a {@link SummarySize} instance via {@link SummarySize#from(String)}. | ||
*/ | ||
public class SummarySize implements Serializable { | ||
|
||
private static final long serialVersionUID = 6769159729743311079L; | ||
|
||
private static final int DEFAULT_SIZE = 150; | ||
private static final Logger log = LoggerFactory.getLogger(SummarySize.class); | ||
|
||
private int summarySize; | ||
private ArrayList<String> contentNamesList; | ||
private boolean only; | ||
|
||
public SummarySize() { | ||
summarySize = DEFAULT_SIZE; | ||
contentNamesList = new ArrayList<>(); | ||
only = false; | ||
} | ||
|
||
/** | ||
* Returns a new {@link SummarySize} parsed from the string. The provided string is expected to have the format returned by {@link SummarySize#toString()}. | ||
* <ul> | ||
* <li>Given null, null will be returned.</li> | ||
* <li>Given an empty or blank string, an empty {@link SummarySize} will be returned.</li> | ||
* <li>Given {@code 50/ONLY/CONTENT1,CONTENT2}, an {@link SummarySize} will be returned with a size of 50, only using the specified content names, and list | ||
* of content names of (CONTENT1, CONTENT2). | ||
* </ul> | ||
* | ||
* @param string | ||
* the string to parse | ||
* @return the parsed {@link SummarySize} | ||
*/ | ||
@JsonCreator | ||
public static SummarySize from(String string) { | ||
if (string == null) { | ||
return null; | ||
} | ||
// Strip whitespaces. | ||
string = PhraseIndexes.whitespacePattern.matcher(string).replaceAll(""); | ||
|
||
if (string.isEmpty()) { | ||
return new SummarySize(); | ||
} | ||
|
||
SummarySize summarySize = new SummarySize(); | ||
|
||
String[] parameterParts = string.split(Constants.FORWARD_SLASH); | ||
// add the size | ||
summarySize.summarySize = Integer.parseInt(parameterParts[0]); | ||
// if 2 parts, assume the second part is a list of content names | ||
if (parameterParts.length == 2) { | ||
Collections.addAll(summarySize.contentNamesList, parameterParts[1].split(Constants.COMMA)); | ||
} else if (parameterParts.length >= 3) { // if 3 parts, assume part 2 is "only" and part 3 is a list of content names | ||
if (parameterParts[1].equalsIgnoreCase("ONLY")) { | ||
summarySize.only = true; | ||
} | ||
Collections.addAll(summarySize.contentNamesList, parameterParts[2].split(Constants.COMMA)); | ||
} | ||
|
||
return summarySize; | ||
} | ||
|
||
/** | ||
* Returns a copy of the given {@link SummarySize} | ||
* | ||
* @param other | ||
* the instance to copy | ||
* @return the copy | ||
*/ | ||
public static SummarySize copyOf(SummarySize other) { | ||
if (other == null) { | ||
return null; | ||
} | ||
SummarySize summarySize = new SummarySize(); | ||
summarySize.summarySize = other.summarySize; | ||
summarySize.contentNamesList = new ArrayList<>(other.contentNamesList); | ||
summarySize.only = other.only; | ||
return summarySize; | ||
} | ||
|
||
public List<String> getContentNames() { | ||
return contentNamesList; | ||
} | ||
|
||
public int getSummarySize() { | ||
return summarySize; | ||
} | ||
|
||
public boolean onlyListedContents() { | ||
return only; | ||
} | ||
|
||
public void addContentName(String contentName) { | ||
contentNamesList.add(contentName); | ||
} | ||
|
||
public void addContentName(String contentName, int index) { | ||
if (index < contentNamesList.size() && index >= 0) { | ||
contentNamesList.add(index, contentName); | ||
} else { | ||
log.info("index out of bounds, adding to beginning of list"); | ||
contentNamesList.add(0, contentName); | ||
} | ||
} | ||
|
||
public void addContentNameToBeginning(String contentName) { | ||
contentNamesList.add(0, contentName); | ||
} | ||
|
||
/** | ||
* Replace a content name with another content name | ||
* | ||
* @param contentName | ||
* the one to replace | ||
* @param replacement | ||
* the one to replace the other | ||
*/ | ||
public void replace(String contentName, String replacement) { | ||
int index = contentNamesList.indexOf(contentName); | ||
if (index != -1) { | ||
contentNamesList.set(index, replacement); | ||
} | ||
} | ||
|
||
/** | ||
* Return whether this {@link SummarySize} content names list is empty. | ||
* | ||
* @return true if empty, or false otherwise | ||
*/ | ||
public boolean isEmpty() { | ||
return contentNamesList.isEmpty(); | ||
} | ||
|
||
public String contentNamesListToString() { | ||
if (contentNamesList.isEmpty()) { | ||
return ""; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (String contentName : contentNamesList) { | ||
sb.append(contentName).append(Constants.COMMA); | ||
} | ||
return sb.substring(0, sb.length() - 1); | ||
} | ||
|
||
public static String[] contentNamesListFromString(String string) { | ||
return string.split(Constants.COMMA); | ||
} | ||
|
||
/** | ||
* Returns this {@link SummarySize} as a formatted string that can later be parsed back into a {@link SummarySize} using {@link SummarySize#from(String)}. | ||
* This is also what will be used when serializing a {@link SummarySize} to JSON/XML. The string will have the format | ||
* {@code size/[only]/[contentName1, contentName2, ....]}. | ||
* | ||
* @return a formatted string | ||
*/ | ||
@JsonValue | ||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(summarySize); | ||
if (only) { | ||
sb.append("/").append("ONLY"); | ||
} | ||
if (!contentNamesList.isEmpty()) { | ||
sb.append("/"); | ||
for (String contentName : contentNamesList) { | ||
sb.append(contentName).append(Constants.COMMA); | ||
} | ||
return sb.substring(0, sb.length() - 1); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
SummarySize that = (SummarySize) o; | ||
return Objects.equals(summarySize, that.summarySize) && Objects.equals(contentNamesList, that.contentNamesList) && Objects.equals(only, that.only); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(summarySize, contentNamesList, only); | ||
} | ||
} |
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
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
Oops, something went wrong.