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.config; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static com.google.common.base.Preconditions.checkState; |
| 21 | |
| 22 | import com.google.caliper.model.InstrumentSpec; |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.base.Objects; |
| 25 | import com.google.common.collect.ImmutableMap; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | |
| 29 | import javax.annotation.concurrent.Immutable; |
| 30 | |
| 31 | /** |
| 32 | * This is the configuration passed to the instrument by the user. This differs from the |
| 33 | * {@link InstrumentSpec} in that any number of configurations can yield the same spec |
| 34 | * (due to default option values). |
| 35 | * |
| 36 | * @author gak@google.com (Gregory Kick) |
| 37 | */ |
| 38 | @Immutable |
| 39 | public final class InstrumentConfig { |
| 40 | private final String className; |
| 41 | private final ImmutableMap<String, String> options; |
| 42 | |
| 43 | private InstrumentConfig(Builder builder) { |
| 44 | this.className = builder.className; |
| 45 | this.options = builder.optionsBuilder.build(); |
| 46 | } |
| 47 | |
| 48 | public String className() { |
| 49 | return className; |
| 50 | } |
| 51 | |
| 52 | public ImmutableMap<String, String> options() { |
| 53 | return options; |
| 54 | } |
| 55 | |
| 56 | @Override public boolean equals(Object obj) { |
| 57 | if (obj == this) { |
| 58 | return true; |
| 59 | } else if (obj instanceof InstrumentConfig) { |
| 60 | InstrumentConfig that = (InstrumentConfig) obj; |
| 61 | return this.className.equals(that.className) |
| 62 | && this.options.equals(that.options); |
| 63 | } else { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override public int hashCode() { |
| 69 | return Objects.hashCode(className, options); |
| 70 | } |
| 71 | |
| 72 | @Override public String toString() { |
| 73 | return MoreObjects.toStringHelper(this) |
| 74 | .add("className", className) |
| 75 | .add("options", options) |
| 76 | .toString(); |
| 77 | } |
| 78 | |
| 79 | static final class Builder { |
| 80 | private String className; |
| 81 | private final ImmutableMap.Builder<String, String> optionsBuilder = ImmutableMap.builder(); |
| 82 | |
| 83 | public Builder className(String className) { |
| 84 | this.className = checkNotNull(className); |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | public Builder instrumentClass(Class<?> insturmentClass) { |
| 89 | return className(insturmentClass.getName()); |
| 90 | } |
| 91 | |
| 92 | public Builder addOption(String option, String value) { |
| 93 | optionsBuilder.put(option, value); |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | public Builder addAllOptions(Map<String, String> options) { |
| 98 | optionsBuilder.putAll(options); |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | public InstrumentConfig build() { |
| 103 | checkState(className != null); |
| 104 | return new InstrumentConfig(this); |
| 105 | } |
| 106 | } |
| 107 | } |