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.caliper.model.PersistentHashing.getPersistentHashFunction; |
| 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.collect.ImmutableSortedMap; |
| 25 | import com.google.common.collect.Maps; |
| 26 | import com.google.common.hash.Funnel; |
| 27 | import com.google.common.hash.PrimitiveSink; |
| 28 | |
| 29 | import java.io.Serializable; |
| 30 | import java.util.Map; |
| 31 | import java.util.SortedMap; |
| 32 | |
| 33 | /** |
| 34 | * A specification by which a benchmark method invocation can be uniquely identified. |
| 35 | * |
| 36 | * @author gak@google.com (Gregory Kick) |
| 37 | */ |
| 38 | public final class BenchmarkSpec implements Serializable { |
| 39 | private static final long serialVersionUID = 1L; |
| 40 | |
| 41 | static final BenchmarkSpec DEFAULT = new BenchmarkSpec(); |
| 42 | |
| 43 | @ExcludeFromJson private int id; |
| 44 | private String className; |
| 45 | private String methodName; |
| 46 | private SortedMap<String, String> parameters; |
| 47 | @ExcludeFromJson private int hash; |
| 48 | |
| 49 | private BenchmarkSpec() { |
| 50 | this.className = ""; |
| 51 | this.methodName = ""; |
| 52 | this.parameters = Maps.newTreeMap(); |
| 53 | } |
| 54 | |
| 55 | private BenchmarkSpec(Builder builder) { |
| 56 | this.className = builder.className; |
| 57 | this.methodName = builder.methodName; |
| 58 | this.parameters = Maps.newTreeMap(builder.parameters); |
| 59 | } |
| 60 | |
| 61 | public String className() { |
| 62 | return className; |
| 63 | } |
| 64 | |
| 65 | public String methodName() { |
| 66 | return methodName; |
| 67 | } |
| 68 | |
| 69 | public ImmutableSortedMap<String, String> parameters() { |
| 70 | return ImmutableSortedMap.copyOf(parameters); |
| 71 | } |
| 72 | |
| 73 | @Override public boolean equals(Object obj) { |
| 74 | if (obj == this) { |
| 75 | return true; |
| 76 | } else if (obj instanceof BenchmarkSpec) { |
| 77 | BenchmarkSpec that = (BenchmarkSpec) obj; |
| 78 | return this.className.equals(that.className) |
| 79 | && this.methodName.equals(that.methodName) |
| 80 | && this.parameters.equals(that.parameters); |
| 81 | } else { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private void initHash() { |
| 87 | if (hash == 0) { |
| 88 | this.hash = getPersistentHashFunction() |
| 89 | .hashObject(this, BenchmarkSpecFunnel.INSTANCE).asInt(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Override public int hashCode() { |
| 94 | initHash(); |
| 95 | return hash; |
| 96 | } |
| 97 | |
| 98 | @Override public String toString() { |
| 99 | return MoreObjects.toStringHelper(this) |
| 100 | .add("className", className) |
| 101 | .add("methodName", methodName) |
| 102 | .add("parameters", parameters) |
| 103 | .toString(); |
| 104 | } |
| 105 | |
| 106 | enum BenchmarkSpecFunnel implements Funnel<BenchmarkSpec> { |
| 107 | INSTANCE; |
| 108 | |
| 109 | @Override public void funnel(BenchmarkSpec from, PrimitiveSink into) { |
| 110 | into.putUnencodedChars(from.className) |
| 111 | .putUnencodedChars(from.methodName); |
| 112 | StringMapFunnel.INSTANCE.funnel(from.parameters, into); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public static final class Builder { |
| 117 | private String className; |
| 118 | private String methodName; |
| 119 | private final SortedMap<String, String> parameters = Maps.newTreeMap(); |
| 120 | |
| 121 | public Builder className(String className) { |
| 122 | this.className = checkNotNull(className); |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public Builder methodName(String methodName) { |
| 127 | this.methodName = checkNotNull(methodName); |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public Builder addParameter(String parameterName, String value) { |
| 132 | this.parameters.put(checkNotNull(parameterName), checkNotNull(value)); |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | public Builder addAllParameters(Map<String, String> parameters) { |
| 137 | this.parameters.putAll(parameters); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | public BenchmarkSpec build() { |
| 142 | checkState(className != null); |
| 143 | checkState(methodName != null); |
| 144 | return new BenchmarkSpec(this); |
| 145 | } |
| 146 | } |
| 147 | } |