Skip to content

Commit

Permalink
Static methods of AttributeParser are now public, Javadoc and JSR a…
Browse files Browse the repository at this point in the history
…nnotations added
  • Loading branch information
HardNorth committed Dec 16, 2023
1 parent 3a87b4c commit 966eb85
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Changed
- Static methods of `AttributeParser` are now public, Javadoc and JSR annotations added, by @HardNorth

## [5.1.24]
### Added
Expand Down
61 changes: 53 additions & 8 deletions src/main/java/com/epam/reportportal/utils/AttributeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@
import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;

/**
* This class contains functionality for parsing tags from string.
* This class contains functionality for parsing tags and attributes from string.
*/
public class AttributeParser {

public static final String ATTRIBUTES_SPLITTER = ";";
public static final String KEY_VALUE_SPLITTER = ":";

private AttributeParser() {
throw new IllegalStateException("Static only class");
}

/**
* Parse attribute string.<br>
* Input attribute string should have format: build:4r3wf234;attributeKey:attributeValue;attributeValue2;attributeValue3.<br>
Expand All @@ -42,7 +48,8 @@ public class AttributeParser {
* @param rawAttributes Attributes string
* @return {@link Set} of {@link ItemAttributesRQ}
*/
public static Set<ItemAttributesRQ> parseAsSet(String rawAttributes) {
@Nonnull
public static Set<ItemAttributesRQ> parseAsSet(@Nullable String rawAttributes) {
if (null == rawAttributes) {
return Collections.emptySet();
}
Expand All @@ -58,7 +65,15 @@ public static Set<ItemAttributesRQ> parseAsSet(String rawAttributes) {
return attributes;
}

public static ItemAttributesRQ splitKeyValue(String attribute) {
/**
* Parse a string representation of an attribute to ReportPortal attribute object instance.
* E.G.: 'key:value', ' :value', 'tag'
*
* @param attribute string representation of an attribute
* @return ReportPortal attribute object instance
*/
@Nullable
public static ItemAttributesRQ splitKeyValue(@Nullable String attribute) {
if (null == attribute || attribute.trim().isEmpty()) {
return null;
}
Expand All @@ -75,7 +90,14 @@ public static ItemAttributesRQ splitKeyValue(String attribute) {
return null;
}

public static Set<ItemAttributesRQ> retrieveAttributes(Attributes attributesAnnotation) {
/**
* Parse ReportPortal attributes from {@link Attributes} annotation instance.
*
* @param attributesAnnotation annotation instance
* @return a set of ReportPortal attributes
*/
@Nonnull
public static Set<ItemAttributesRQ> retrieveAttributes(@Nonnull Attributes attributesAnnotation) {
Set<ItemAttributesRQ> itemAttributes = new LinkedHashSet<>();
for (Attribute attribute : attributesAnnotation.attributes()) {
if (!attribute.value().trim().isEmpty()) {
Expand All @@ -97,7 +119,15 @@ public static Set<ItemAttributesRQ> retrieveAttributes(Attributes attributesAnno
return itemAttributes;
}

private static List<ItemAttributesRQ> createItemAttributes(String[] keys, String value) {
/**
* Create list of attributes from key array and a value.
*
* @param keys attribute keys
* @param value attribute value
* @return list of ReportPortal attributes
*/
@Nonnull
public static List<ItemAttributesRQ> createItemAttributes(@Nullable String[] keys, @Nullable String value) {
if (value == null || value.trim().isEmpty()) {
return Collections.emptyList();
}
Expand All @@ -108,18 +138,33 @@ private static List<ItemAttributesRQ> createItemAttributes(String[] keys, String
return Arrays.stream(keys).map(k -> createItemAttribute(k, value)).collect(Collectors.toList());
}

private static List<ItemAttributesRQ> createItemAttributes(String key, String[] values) {
/**
* Create list of attributes from a key and value array.
*
* @param key attribute key
* @param values attribute values
* @return list of ReportPortal attributes
*/
@Nonnull
public static List<ItemAttributesRQ> createItemAttributes(@Nullable String key, @Nullable String[] values) {
if (values != null && values.length > 0) {
return Arrays.stream(values)
.filter(StringUtils::isNotBlank)
.map(v -> createItemAttribute(key, v))
.collect(Collectors.toList());
}

return Collections.emptyList();
}

private static ItemAttributesRQ createItemAttribute(String key, String value) {
/**
* Create an ItemAttributesRQ instance with key and value.
*
* @param key attribute key
* @param value attribute value
* @return ReportPortal attribute
*/
@Nonnull
public static ItemAttributesRQ createItemAttribute(@Nullable String key, @Nonnull String value) {
return new ItemAttributesRQ(key, value);
}
}

0 comments on commit 966eb85

Please sign in to comment.