create ImmutableSets in constructor rather than on demand in getters

This commit is contained in:
jmillman 2015-11-24 13:39:39 -05:00
parent 883e48b043
commit b36d3732dd
5 changed files with 67 additions and 51 deletions

View File

@ -47,7 +47,7 @@ public interface EventBundle<ParentType extends EventBundle<?>> {
Optional<ParentType> getParentBundle(); Optional<ParentType> getParentBundle();
default long getCount() { default int getCount() {
return getEventIDs().size(); return getEventIDs().size();
} }

View File

@ -18,9 +18,9 @@
*/ */
package org.sleuthkit.autopsy.timeline.datamodel; package org.sleuthkit.autopsy.timeline.datamodel;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -89,28 +89,28 @@ public class EventCluster implements EventBundle<EventStripe> {
/** /**
* the set of ids of the clustered events * the set of ids of the clustered events
*/ */
final private Set<Long> eventIDs; final private ImmutableSet<Long> eventIDs;
/** /**
* the ids of the subset of clustered events that have at least one tag * the ids of the subset of clustered events that have at least one tag
* applied to them * applied to them
*/ */
private final Set<Long> tagged; private final ImmutableSet<Long> tagged;
/** /**
* the ids of the subset of clustered events that have at least one hash set * the ids of the subset of clustered events that have at least one hash set
* hit * hit
*/ */
private final Set<Long> hashHits; private final ImmutableSet<Long> hashHits;
private EventCluster(Interval spanningInterval, EventType type, Set<Long> eventIDs, Set<Long> hashHits, Set<Long> tagged, String description, DescriptionLoD lod, EventStripe parent) { private EventCluster(Interval spanningInterval, EventType type, Set<Long> eventIDs, Set<Long> hashHits, Set<Long> tagged, String description, DescriptionLoD lod, EventStripe parent) {
this.span = spanningInterval; this.span = spanningInterval;
this.type = type; this.type = type;
this.hashHits = hashHits; this.hashHits = ImmutableSet.copyOf(hashHits);
this.tagged = tagged; this.tagged = ImmutableSet.copyOf(tagged);
this.description = description; this.description = description;
this.eventIDs = eventIDs; this.eventIDs = ImmutableSet.copyOf(eventIDs);
this.lod = lod; this.lod = lod;
this.parent = parent; this.parent = parent;
} }
@ -139,18 +139,21 @@ public class EventCluster implements EventBundle<EventStripe> {
} }
@Override @Override
public Set<Long> getEventIDs() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSet(eventIDs); public ImmutableSet<Long> getEventIDs() {
return eventIDs;
} }
@Override @Override
public Set<Long> getEventIDsWithHashHits() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSet(hashHits); public ImmutableSet<Long> getEventIDsWithHashHits() {
return hashHits;
} }
@Override @Override
public Set<Long> getEventIDsWithTags() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSet(tagged); public ImmutableSet<Long> getEventIDsWithTags() {
return tagged;
} }
@Override @Override

View File

@ -19,13 +19,11 @@
package org.sleuthkit.autopsy.timeline.datamodel; package org.sleuthkit.autopsy.timeline.datamodel;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.util.Collections; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import org.python.google.common.base.Objects; import org.python.google.common.base.Objects;
import org.sleuthkit.autopsy.timeline.datamodel.eventtype.EventType; import org.sleuthkit.autopsy.timeline.datamodel.eventtype.EventType;
@ -50,7 +48,7 @@ public final class EventStripe implements EventBundle<EventCluster> {
private final EventCluster parent; private final EventCluster parent;
private final SortedSet<EventCluster> clusters = new TreeSet<>(Comparator.comparing(EventCluster::getStartMillis)); private final ImmutableSortedSet<EventCluster> clusters;
/** /**
* the type of all the events * the type of all the events
@ -70,59 +68,70 @@ public final class EventStripe implements EventBundle<EventCluster> {
/** /**
* the set of ids of the events * the set of ids of the events
*/ */
private final Set<Long> eventIDs = new HashSet<>(); private final ImmutableSet<Long> eventIDs;
/** /**
* the ids of the subset of events that have at least one tag applied to * the ids of the subset of events that have at least one tag applied to
* them * them
*/ */
private final Set<Long> tagged = new HashSet<>(); private final ImmutableSet<Long> tagged;
/** /**
* the ids of the subset of events that have at least one hash set hit * the ids of the subset of events that have at least one hash set hit
*/ */
private final Set<Long> hashHits = new HashSet<>(); private final ImmutableSet<Long> hashHits;
public EventStripe withParent(EventCluster parent) { public EventStripe withParent(EventCluster parent) {
EventStripe eventStripe = new EventStripe(parent, this.type, this.description, this.lod); EventStripe eventStripe = new EventStripe(parent, this.type, this.description, this.lod, clusters, eventIDs, tagged, hashHits);
eventStripe.clusters.addAll(clusters);
eventStripe.eventIDs.addAll(eventIDs);
eventStripe.tagged.addAll(tagged);
eventStripe.hashHits.addAll(hashHits);
return eventStripe; return eventStripe;
} }
private EventStripe(EventCluster parent, EventType type, String description, DescriptionLoD lod) { private EventStripe(EventCluster parent, EventType type, String description, DescriptionLoD lod, SortedSet<EventCluster> clusters, ImmutableSet<Long> eventIDs, ImmutableSet<Long> tagged, ImmutableSet<Long> hashHits) {
this.parent = parent; this.parent = parent;
this.type = type; this.type = type;
this.description = description; this.description = description;
this.lod = lod; this.lod = lod;
this.clusters = ImmutableSortedSet.copyOf(Comparator.comparing(EventCluster::getStartMillis), clusters);
this.eventIDs = eventIDs;
this.tagged = tagged;
this.hashHits = hashHits;
} }
public EventStripe(EventCluster cluster, EventCluster parent) { public EventStripe(EventCluster cluster, EventCluster parent) {
clusters.add(cluster); this.clusters = ImmutableSortedSet.orderedBy(Comparator.comparing(EventCluster::getStartMillis))
.add(cluster).build();
type = cluster.getEventType(); type = cluster.getEventType();
description = cluster.getDescription(); description = cluster.getDescription();
lod = cluster.getDescriptionLoD(); lod = cluster.getDescriptionLoD();
eventIDs.addAll(cluster.getEventIDs()); eventIDs = cluster.getEventIDs();
tagged.addAll(cluster.getEventIDsWithTags()); tagged = cluster.getEventIDsWithTags();
hashHits.addAll(cluster.getEventIDsWithHashHits()); hashHits = cluster.getEventIDsWithHashHits();
this.parent = parent; this.parent = parent;
} }
private EventStripe(EventStripe u, EventStripe v) { private EventStripe(EventStripe u, EventStripe v) {
clusters.addAll(u.clusters); clusters = ImmutableSortedSet.orderedBy(Comparator.comparing(EventCluster::getStartMillis))
clusters.addAll(v.clusters); .addAll(u.getClusters())
.addAll(v.getClusters())
.build();
type = u.getEventType(); type = u.getEventType();
description = u.getDescription(); description = u.getDescription();
lod = u.getDescriptionLoD(); lod = u.getDescriptionLoD();
eventIDs.addAll(u.getEventIDs()); eventIDs = ImmutableSet.<Long>builder()
eventIDs.addAll(v.getEventIDs()); .addAll(u.getEventIDs())
tagged.addAll(u.getEventIDsWithTags()); .addAll(v.getEventIDs())
tagged.addAll(v.getEventIDsWithTags()); .build();
hashHits.addAll(u.getEventIDsWithHashHits()); tagged = ImmutableSet.<Long>builder()
hashHits.addAll(v.getEventIDsWithHashHits()); .addAll(u.getEventIDsWithTags())
.addAll(v.getEventIDsWithTags())
.build();
hashHits = ImmutableSet.<Long>builder()
.addAll(u.getEventIDsWithHashHits())
.addAll(v.getEventIDsWithHashHits())
.build();
parent = u.getParentBundle().orElse(v.getParentBundle().orElse(null)); parent = u.getParentBundle().orElse(v.getParentBundle().orElse(null));
} }
@ -147,18 +156,21 @@ public final class EventStripe implements EventBundle<EventCluster> {
} }
@Override @Override
public Set<Long> getEventIDs() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSet(eventIDs); public ImmutableSet<Long> getEventIDs() {
return eventIDs;
} }
@Override @Override
public Set<Long> getEventIDsWithHashHits() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSet(hashHits); public ImmutableSet<Long> getEventIDsWithHashHits() {
return hashHits;
} }
@Override @Override
public Set<Long> getEventIDsWithTags() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSet(tagged); public ImmutableSet<Long> getEventIDsWithTags() {
return tagged;
} }
@Override @Override
@ -172,8 +184,9 @@ public final class EventStripe implements EventBundle<EventCluster> {
} }
@Override @Override
public SortedSet< EventCluster> getClusters() { @SuppressWarnings("ReturnOfCollectionOrArrayField")
return Collections.unmodifiableSortedSet(clusters); public ImmutableSortedSet< EventCluster> getClusters() {
return clusters;
} }
@Override @Override

View File

@ -143,7 +143,7 @@ final public class EventClusterNode extends EventBundleNodeBase<EventCluster, Ev
@Override @Override
void setDescriptionVisibiltiyImpl(DescriptionVisibility descrVis) { void setDescriptionVisibiltiyImpl(DescriptionVisibility descrVis) {
final int size = getEventBundle().getEventIDs().size(); final int size = getEventCluster().getCount();
switch (descrVis) { switch (descrVis) {
case HIDDEN: case HIDDEN:
countLabel.setText(""); countLabel.setText("");

View File

@ -68,13 +68,13 @@ final public class EventStripeNode extends EventBundleNodeBase<EventStripe, Even
super(chart, eventStripe, parentNode); super(chart, eventStripe, parentNode);
setMinHeight(48); setMinHeight(48);
//setup description label //setup description label
eventTypeImageView.setImage(getEventType().getFXImage()); eventTypeImageView.setImage(getEventType().getFXImage());
descrLabel.setTextOverrun(OverrunStyle.CENTER_ELLIPSIS); descrLabel.setTextOverrun(OverrunStyle.CENTER_ELLIPSIS);
descrLabel.setGraphic(eventTypeImageView); descrLabel.setGraphic(eventTypeImageView);
descrLabel.setPrefWidth(USE_COMPUTED_SIZE); descrLabel.setPrefWidth(USE_COMPUTED_SIZE);
setAlignment(subNodePane, Pos.BOTTOM_LEFT); setAlignment(subNodePane, Pos.BOTTOM_LEFT);
for (EventCluster cluster : eventStripe.getClusters()) { for (EventCluster cluster : eventStripe.getClusters()) {
subNodes.add(createChildNode(cluster)); subNodes.add(createChildNode(cluster));
} }
@ -124,7 +124,7 @@ final public class EventStripeNode extends EventBundleNodeBase<EventStripe, Even
@Override @Override
void setDescriptionVisibiltiyImpl(DescriptionVisibility descrVis) { void setDescriptionVisibiltiyImpl(DescriptionVisibility descrVis) {
final int size = getEventStripe().getEventIDs().size(); final int size = getEventStripe().getCount();
switch (descrVis) { switch (descrVis) {
case HIDDEN: case HIDDEN: