Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 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.checkArgument; |
| 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | |
| 22 | import com.google.common.base.Function; |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.base.Objects; |
| 25 | import com.google.common.collect.ImmutableListMultimap; |
| 26 | import com.google.common.collect.Multimaps; |
| 27 | |
| 28 | import java.io.Serializable; |
| 29 | |
| 30 | /** |
| 31 | * A single, weighted measurement. |
| 32 | * |
| 33 | * @author gak@google.com (Gregory Kick) |
| 34 | */ |
| 35 | public class Measurement implements Serializable { |
| 36 | private static final long serialVersionUID = 1L; |
| 37 | |
| 38 | public static ImmutableListMultimap<String, Measurement> indexByDescription( |
| 39 | Iterable<Measurement> measurements) { |
| 40 | return Multimaps.index(measurements, new Function<Measurement, String>() { |
| 41 | @Override public String apply(Measurement input) { |
| 42 | return input.description; |
| 43 | } |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | @ExcludeFromJson |
| 48 | private int id; |
| 49 | private Value value; |
| 50 | private double weight; |
| 51 | private String description; |
| 52 | |
| 53 | private Measurement() { |
| 54 | this.value = Value.DEFAULT; |
| 55 | this.weight = 0.0; |
| 56 | this.description = ""; |
| 57 | } |
| 58 | |
| 59 | private Measurement(Builder builder) { |
| 60 | this.value = builder.value; |
| 61 | this.description = builder.description; |
| 62 | this.weight = builder.weight; |
| 63 | } |
| 64 | |
| 65 | @Override public boolean equals(Object obj) { |
| 66 | if (obj == this) { |
| 67 | return true; |
| 68 | } else if (obj instanceof Measurement) { |
| 69 | Measurement that = (Measurement) obj; |
| 70 | return this.value.equals(that.value) |
| 71 | && this.weight == that.weight |
| 72 | && this.description.equals(that.description); |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override public int hashCode() { |
| 79 | return Objects.hashCode(value, weight, description); |
| 80 | } |
| 81 | |
| 82 | @Override public String toString() { |
| 83 | return MoreObjects.toStringHelper(this) |
| 84 | .add("value", value) |
| 85 | .add("weight", weight) |
| 86 | .add("description", description) |
| 87 | .toString(); |
| 88 | } |
| 89 | |
| 90 | public Value value() { |
| 91 | return value; |
| 92 | } |
| 93 | |
| 94 | public double weight() { |
| 95 | return weight; |
| 96 | } |
| 97 | |
| 98 | public String description() { |
| 99 | return description; |
| 100 | } |
| 101 | |
| 102 | public static final class Builder { |
| 103 | private Value value; |
| 104 | private Double weight; |
| 105 | private String description; |
| 106 | |
| 107 | public Builder value(Value value) { |
| 108 | this.value = checkNotNull(value); |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | public Builder weight(double weight) { |
| 113 | checkArgument(weight > 0); |
| 114 | this.weight = weight; |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | public Builder description(String description) { |
| 119 | this.description = checkNotNull(description); |
| 120 | return this; |
| 121 | } |
| 122 | |
| 123 | public Measurement build() { |
| 124 | checkArgument(value != null); |
| 125 | checkArgument(weight != null); |
| 126 | checkArgument(description != null); |
| 127 | return new Measurement(this); |
| 128 | } |
| 129 | } |
| 130 | } |