Home | History | Annotate | Download | only in model
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.caliper.model;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 import static com.google.common.base.Preconditions.checkState;
     21 
     22 import com.google.common.base.MoreObjects;
     23 import com.google.common.base.Objects;
     24 import com.google.common.collect.ImmutableList;
     25 import com.google.common.collect.Iterables;
     26 import com.google.common.collect.Lists;
     27 
     28 import java.util.List;
     29 import java.util.UUID;
     30 
     31 /**
     32  * An invocation of a single scenario measured with a single instrument and the results thereof.
     33  *
     34  * @author gak (at) google.com (Gregory Kick)
     35  */
     36 public final class Trial { // used to be Result
     37   public static final Trial DEFAULT = new Trial();
     38 
     39   private UUID id;
     40   private Run run;
     41   private InstrumentSpec instrumentSpec;
     42   private Scenario scenario;
     43   private List<Measurement> measurements;
     44 
     45   private Trial() {
     46     this.id = Defaults.UUID;
     47     this.run = Run.DEFAULT;
     48     this.instrumentSpec = InstrumentSpec.DEFAULT;
     49     this.scenario = Scenario.DEFAULT;
     50     this.measurements = Lists.newArrayList();
     51   }
     52 
     53   private Trial(Builder builder) {
     54     this.id = builder.id;
     55     this.run = builder.run;
     56     this.instrumentSpec = builder.instrumentSpec;
     57     this.scenario = builder.scenario;
     58     this.measurements = Lists.newArrayList(builder.measurements);
     59   }
     60 
     61   public UUID id() {
     62     return id;
     63   }
     64 
     65   public Run run() {
     66     return run;
     67   }
     68 
     69   public InstrumentSpec instrumentSpec() {
     70     return instrumentSpec;
     71   }
     72 
     73   public Scenario scenario() {
     74     return scenario;
     75   }
     76 
     77   public ImmutableList<Measurement> measurements() {
     78     return ImmutableList.copyOf(measurements);
     79   }
     80 
     81   @Override public boolean equals(Object obj) {
     82     if (obj == this) {
     83       return true;
     84     } else if (obj instanceof Trial) {
     85       Trial that = (Trial) obj;
     86       return this.id.equals(that.id)
     87           && this.run.equals(that.run)
     88           && this.instrumentSpec.equals(that.instrumentSpec)
     89           && this.scenario.equals(that.scenario)
     90           && this.measurements.equals(that.measurements);
     91     } else {
     92       return false;
     93     }
     94   }
     95 
     96   @Override public int hashCode() {
     97     return Objects.hashCode(id, run, instrumentSpec, scenario, measurements);
     98   }
     99 
    100   @Override public String toString() {
    101     return MoreObjects.toStringHelper(this)
    102         .add("id", id)
    103         .add("run", run)
    104         .add("instrumentSpec", instrumentSpec)
    105         .add("scenario", scenario)
    106         .add("measurements", measurements)
    107         .toString();
    108   }
    109 
    110   public static final class Builder {
    111     private final UUID id;
    112     private Run run;
    113     private InstrumentSpec instrumentSpec;
    114     private Scenario scenario;
    115     private final List<Measurement> measurements = Lists.newArrayList();
    116 
    117     public Builder(UUID id) {
    118       this.id = checkNotNull(id);
    119     }
    120 
    121     public Builder run(Run.Builder runBuilder) {
    122       return run(runBuilder.build());
    123     }
    124 
    125     public Builder run(Run run) {
    126       this.run = checkNotNull(run);
    127       return this;
    128     }
    129 
    130     public Builder instrumentSpec(InstrumentSpec.Builder instrumentSpecBuilder) {
    131       return instrumentSpec(instrumentSpecBuilder.build());
    132     }
    133 
    134     public Builder instrumentSpec(InstrumentSpec instrumentSpec) {
    135       this.instrumentSpec = checkNotNull(instrumentSpec);
    136       return this;
    137     }
    138 
    139     public Builder scenario(Scenario.Builder scenarioBuilder) {
    140       return scenario(scenarioBuilder.build());
    141     }
    142 
    143     public Builder scenario(Scenario scenario) {
    144       this.scenario = checkNotNull(scenario);
    145       return this;
    146     }
    147 
    148     public Builder addMeasurement(Measurement.Builder measurementBuilder) {
    149       return addMeasurement(measurementBuilder.build());
    150     }
    151 
    152     public Builder addMeasurement(Measurement measurement) {
    153       this.measurements.add(measurement);
    154       return this;
    155     }
    156 
    157     public Builder addAllMeasurements(Iterable<Measurement> measurements) {
    158       Iterables.addAll(this.measurements, measurements);
    159       return this;
    160     }
    161 
    162     public Trial build() {
    163       checkState(run != null);
    164       checkState(instrumentSpec != null);
    165       checkState(scenario != null);
    166       return new Trial(this);
    167     }
    168   }
    169 }
    170