1 // Copyright 2017 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.devtools.common.options.testing; 16 17 import com.google.common.collect.ForwardingMap; 18 import com.google.common.collect.ImmutableMap; 19 import com.google.devtools.common.options.Converter; 20 import java.util.Map; 21 22 /** 23 * An immutable mapping from {@link Converter} classes to {@link ConverterTester}s which test them. 24 * 25 * <p>Note that the ConverterTesters within are NOT immutable. 26 */ 27 public final class ConverterTesterMap 28 extends ForwardingMap<Class<? extends Converter<?>>, ConverterTester> { 29 30 private final ImmutableMap<Class<? extends Converter<?>>, ConverterTester> delegate; 31 ConverterTesterMap( ImmutableMap<Class<? extends Converter<?>>, ConverterTester> delegate)32 private ConverterTesterMap( 33 ImmutableMap<Class<? extends Converter<?>>, ConverterTester> delegate) { 34 this.delegate = delegate; 35 } 36 37 @Override delegate()38 protected Map<Class<? extends Converter<?>>, ConverterTester> delegate() { 39 return delegate; 40 } 41 42 /** A builder to construct new {@link ConverterTesterMap}s. */ 43 public static final class Builder { 44 private final ImmutableMap.Builder<Class<? extends Converter<?>>, ConverterTester> delegate; 45 Builder()46 public Builder() { 47 this.delegate = ImmutableMap.builder(); 48 } 49 50 /** 51 * Adds a new ConverterTester, mapping it to the class of converter it tests. Only one tester 52 * for each class is permitted; duplicates will cause {@link #build} to fail. 53 */ add(ConverterTester item)54 public Builder add(ConverterTester item) { 55 delegate.put(item.getConverterClass(), item); 56 return this; 57 } 58 59 /** 60 * Adds the entries from the given {@link ConverterTesterMap}. Only one tester for each class is 61 * permitted; duplicates will cause {@link #build} to fail. 62 */ addAll(ConverterTesterMap map)63 public Builder addAll(ConverterTesterMap map) { 64 // this is safe because we know the other map was constructed the same way this one was 65 delegate.putAll(map); 66 return this; 67 } 68 69 /** 70 * Adds all of the ConverterTesters from the given iterable. Only one tester for each class is 71 * permitted; duplicates will cause {@link #build} to fail. 72 */ addAll(Iterable<ConverterTester> items)73 public Builder addAll(Iterable<ConverterTester> items) { 74 items.forEach(this::add); 75 return this; 76 } 77 build()78 public ConverterTesterMap build() { 79 return new ConverterTesterMap(delegate.build()); 80 } 81 } 82 } 83