fixed codacy issue

This commit is contained in:
Kelly Kelly 2020-08-27 10:32:23 -04:00
parent 4cfbe6d18b
commit cf3bd7c831

View File

@ -146,12 +146,12 @@ final class DataSourceInfoUtilities {
DESCENDING, DESCENDING,
ASCENDING ASCENDING
} }
/** /**
* Returns a list of all artifacts of the given type that have an attribute * Returns a list of all artifacts of the given type that have an attribute
* of the given type sorted by given attribute type value. Artifacts that * of the given type sorted by given attribute type value. Artifacts that do
* do not have the given attribute will not be included in the list. * not have the given attribute will not be included in the list.
* *
* Sorting on attributes of type byte[] and JSON is not currently supported. * Sorting on attributes of type byte[] and JSON is not currently supported.
* *
* @param skCase SleuthkitCase instance. * @param skCase SleuthkitCase instance.
@ -173,8 +173,8 @@ final class DataSourceInfoUtilities {
/** /**
* Return a list of artifacts that have been sorted by their attribute of * Return a list of artifacts that have been sorted by their attribute of
* attributeType. If an artifact of the given type does not have the * attributeType. If an artifact of the given type does not have the given
* given attribute it will not be included in the returned list. * attribute it will not be included in the returned list.
* *
* Sorting on attributes of type byte[] and JSON is not currently supported. * Sorting on attributes of type byte[] and JSON is not currently supported.
* *
@ -194,10 +194,36 @@ final class DataSourceInfoUtilities {
* @throws TskCoreException * @throws TskCoreException
*/ */
static List<BlackboardArtifact> getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder, int maxCount) throws TskCoreException { static List<BlackboardArtifact> getArtifacts(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder, int maxCount) throws TskCoreException {
if (maxCount < 0 ) { if (maxCount < 0) {
throw new IllegalArgumentException("Invalid maxCount passed to getArtifacts, value must be at greater 0"); throw new IllegalArgumentException("Invalid maxCount passed to getArtifacts, value must be at greater 0");
} }
return createListFromMap(getArtifactMap(skCase, artifactType, dataSource, attributeType, sortOrder), maxCount);
}
/**
* Empty private constructor
*/
private DataSourceInfoUtilities() {
}
/**
* Create a Map of lists of artifacts sorted by the given attribute.
*
* @param skCase SleuthkitCase instance.
* @param artifactType Type of artifacts to sort.
* @param dataSource Data Source that the artifact belongs to.
* @param attributeType Attribute type to sort by.
* @param sortOrder Sort order of the attributes, either ascending or
* descending.
*
* @return A Map of lists of artifacts sorted by the value of attribute
* given type. Artifacts that do not have an attribute of the given
* type will not be included.
*
* @throws TskCoreException
*/
static private TreeMap<BlackboardAttribute, List<BlackboardArtifact>> getArtifactMap(SleuthkitCase skCase, BlackboardArtifact.Type artifactType, DataSource dataSource, BlackboardAttribute.Type attributeType, SortOrder sortOrder) throws TskCoreException {
TreeMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap = new TreeMap<>(new AttributeComparator(sortOrder)); TreeMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap = new TreeMap<>(new AttributeComparator(sortOrder));
List<BlackboardArtifact> artifactList = skCase.getBlackboard().getArtifacts(artifactType.getTypeID(), dataSource.getId()); List<BlackboardArtifact> artifactList = skCase.getBlackboard().getArtifacts(artifactType.getTypeID(), dataSource.getId());
@ -206,31 +232,43 @@ final class DataSourceInfoUtilities {
if (attribute == null) { if (attribute == null) {
continue; continue;
} }
List<BlackboardArtifact> mapArtifactList = sortedMap.get(attribute); List<BlackboardArtifact> mapArtifactList = sortedMap.get(attribute);
if(mapArtifactList == null) { if (mapArtifactList == null) {
mapArtifactList = new ArrayList<>(); mapArtifactList = new ArrayList<>();
sortedMap.put(attribute, mapArtifactList); sortedMap.put(attribute, mapArtifactList);
} }
mapArtifactList.add(artifact); mapArtifactList.add(artifact);
} }
artifactList = new ArrayList<>(); return sortedMap;
}
/**
* Creates the list of artifacts from the sorted map and the given count.
*
* @param sortedMap Sorted map of artifact lists.
* @param maxCount Maximum number of artifacts to return.
*
* @return List of artifacts, list will be empty if none were found.
*/
static private List<BlackboardArtifact> createListFromMap(TreeMap<BlackboardAttribute, List<BlackboardArtifact>> sortedMap, int maxCount) {
List<BlackboardArtifact> artifactList = new ArrayList<>();
for (List<BlackboardArtifact> mapArtifactList : sortedMap.values()) { for (List<BlackboardArtifact> mapArtifactList : sortedMap.values()) {
if(maxCount == artifactList.size()) { if (maxCount == artifactList.size()) {
break; break;
} }
if(maxCount == 0 || (artifactList.size() + mapArtifactList.size()) <= maxCount ) { if (maxCount == 0 || (artifactList.size() + mapArtifactList.size()) <= maxCount) {
artifactList.addAll(mapArtifactList); artifactList.addAll(mapArtifactList);
continue; continue;
} }
for(BlackboardArtifact artifact: mapArtifactList) { for (BlackboardArtifact artifact : mapArtifactList) {
if(artifactList.size() < maxCount) { if (artifactList.size() < maxCount) {
artifactList.add(artifact); artifactList.add(artifact);
} else { } else {
break; break;
@ -240,20 +278,14 @@ final class DataSourceInfoUtilities {
return artifactList; return artifactList;
} }
/**
* Empty private constructor
*/
private DataSourceInfoUtilities() {
}
/** /**
* Compares the value of two BlackboardAttributes that are of the same type. * Compares the value of two BlackboardAttributes that are of the same type.
* This comparator is specialized for data source summary and only supports * This comparator is specialized for data source summary and only supports
* the basic attribute types of string, integer, long, datetime (long), and * the basic attribute types of string, integer, long, datetime (long), and
* double. * double.
* *
* Note: A runtime exception will be thrown from the compare if the attributes * Note: A runtime exception will be thrown from the compare if the
* are not of the same type or if their type is not supported. * attributes are not of the same type or if their type is not supported.
*/ */
private static class AttributeComparator implements Comparator<BlackboardAttribute> { private static class AttributeComparator implements Comparator<BlackboardAttribute> {
@ -280,13 +312,13 @@ final class DataSourceInfoUtilities {
/** /**
* Compared two attributes of the given type. Note, that not all * Compared two attributes of the given type. Note, that not all
* attribute types are supported. A runtime exception will be thrown * attribute types are supported. A runtime exception will be thrown if
* if an unsupported attribute is supplied. * an unsupported attribute is supplied.
* *
* @param type Attribute type. * @param type Attribute type.
* @param attribute1 First attribute to compare. * @param attribute1 First attribute to compare.
* @param attribute2 Second attribute to compare. * @param attribute2 Second attribute to compare.
* *
* @return Compare result. * @return Compare result.
*/ */
private int compare(BlackboardAttribute.Type type, BlackboardAttribute attribute1, BlackboardAttribute attribute2) { private int compare(BlackboardAttribute.Type type, BlackboardAttribute attribute1, BlackboardAttribute attribute2) {