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 21 import com.google.caliper.model.VmSpec; 22 import com.google.caliper.platform.Platform; 23 import com.google.common.annotations.VisibleForTesting; 24 import com.google.common.base.MoreObjects; 25 import com.google.common.base.Objects; 26 import com.google.common.collect.ImmutableList; 27 import com.google.common.collect.ImmutableSet; 28 29 import java.io.File; 30 31 import javax.annotation.concurrent.GuardedBy; 32 33 /** 34 * This is the configuration passed to the VM by the user. This differs from the {@link VmSpec} 35 * in that any number of configurations can yield the same spec (due to default flag values) and any 36 * number of specs can come from a single configuration (due to 37 * <a href="http://www.oracle.com/technetwork/java/ergo5-140223.html">ergonomics</a>). 38 * 39 * @author gak (at) google.com (Gregory Kick) 40 */ 41 public final class VmConfig { 42 private final Platform platform; 43 private final File vmHome; 44 private final ImmutableList<String> options; 45 46 @GuardedBy("this") 47 private File vmExecutable; 48 49 private VmConfig(Builder builder) { 50 this.platform = builder.platform; 51 this.vmHome = builder.vmHome; 52 this.options = builder.optionsBuilder.build(); 53 } 54 55 @VisibleForTesting 56 public VmConfig(File vmHome, Iterable<String> options, File vmExecutable, Platform platform) { 57 this.platform = platform; 58 this.vmHome = checkNotNull(vmHome); 59 this.vmExecutable = checkNotNull(vmExecutable); 60 this.options = ImmutableList.copyOf(options); 61 } 62 63 public File vmHome() { 64 return vmHome; 65 } 66 67 public synchronized File vmExecutable() { 68 if (vmExecutable == null) { 69 vmExecutable = platform.vmExecutable(vmHome); 70 } 71 return vmExecutable; 72 } 73 74 public ImmutableList<String> options() { 75 return options; 76 } 77 78 public String platformName() { 79 return platform.name(); 80 } 81 82 public String workerClassPath() { 83 return platform.workerClassPath(); 84 } 85 86 public ImmutableSet<String> workerProcessArgs() { 87 return platform.workerProcessArgs(); 88 } 89 90 public ImmutableSet<String> commonInstrumentVmArgs() { 91 return platform.commonInstrumentVmArgs(); 92 } 93 94 @Override public boolean equals(Object obj) { 95 if (obj == this) { 96 return true; 97 } else if (obj instanceof VmConfig) { 98 VmConfig that = (VmConfig) obj; 99 return this.platform.equals(that.platform) 100 && this.vmHome.equals(that.vmHome) 101 && this.options.equals(that.options); 102 } else { 103 return false; 104 } 105 } 106 107 @Override public int hashCode() { 108 return Objects.hashCode(platform, vmHome, options); 109 } 110 111 @Override public String toString() { 112 return MoreObjects.toStringHelper(this) 113 .add("platform", platform) 114 .add("vmHome", vmHome) 115 .add("options", options) 116 .toString(); 117 } 118 119 @VisibleForTesting public static final class Builder { 120 private final Platform platform; 121 private final File vmHome; 122 private final ImmutableList.Builder<String> optionsBuilder = ImmutableList.builder(); 123 124 125 public Builder(Platform platform, File vmHome) { 126 this.platform = checkNotNull(platform); 127 this.vmHome = checkNotNull(vmHome); 128 } 129 130 public Builder addOption(String option) { 131 optionsBuilder.add(option); 132 return this; 133 } 134 135 public Builder addAllOptions(Iterable<String> options) { 136 optionsBuilder.addAll(options); 137 return this; 138 } 139 140 public VmConfig build() { 141 return new VmConfig(this); 142 } 143 } 144 } 145