|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.profiles.internal; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | +import java.util.function.Supplier; |
| 25 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 26 | +import software.amazon.awssdk.profiles.ProfileFile; |
| 27 | +import software.amazon.awssdk.profiles.ProfileFileSupplier; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link ProfileFileSupplier} that combines the {@link ProfileFile} objects from multiple |
| 31 | + * {@code ProfileFileSupplier}s. Objects are passed into {@link ProfileFile.Aggregator}. |
| 32 | + */ |
| 33 | +@SdkInternalApi |
| 34 | +public class AggregateProfileFileSupplier implements ProfileFileSupplier { |
| 35 | + final List<ProfileFileSupplier> suppliers; |
| 36 | + |
| 37 | + // supplier values and the resulting aggregate must always be updated atomically together |
| 38 | + final AtomicReference<SupplierState> state = |
| 39 | + new AtomicReference<>(new SupplierState(Collections.emptyMap(), null)); |
| 40 | + |
| 41 | + public AggregateProfileFileSupplier(ProfileFileSupplier... suppliers) { |
| 42 | + this.suppliers = Collections.unmodifiableList(Arrays.asList(suppliers)); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public ProfileFile get() { |
| 47 | + SupplierState currentState = state.get(); |
| 48 | + Map<Supplier<ProfileFile>, ProfileFile> currentValues = currentState.values; |
| 49 | + Map<Supplier<ProfileFile>, ProfileFile> changedValues = changedSupplierValues(currentValues); |
| 50 | + |
| 51 | + if (changedValues == null) { |
| 52 | + // no suppliers have changed values, return the current aggregate |
| 53 | + return currentState.aggregate; |
| 54 | + } |
| 55 | + |
| 56 | + // one or more supplier values have changed, we need to update the aggregate (and the state) |
| 57 | + // the order of the suppliers matters so we MUST preserve it using LinkedHashMap with insertion ordering |
| 58 | + Map<Supplier<ProfileFile>, ProfileFile> nextValues = new LinkedHashMap<>(currentValues); |
| 59 | + nextValues.putAll(changedValues); |
| 60 | + |
| 61 | + ProfileFile.Aggregator aggregator = ProfileFile.aggregator(); |
| 62 | + nextValues.values().forEach(aggregator::addFile); |
| 63 | + ProfileFile nextAggregate = aggregator.build(); |
| 64 | + |
| 65 | + SupplierState nextState = new SupplierState(nextValues, nextAggregate); |
| 66 | + if (state.compareAndSet(currentState, nextState)) { |
| 67 | + return nextAggregate; |
| 68 | + } |
| 69 | + // else: another thread has modified the state in between, assume it is up to date and use the new state |
| 70 | + return state.get().aggregate; |
| 71 | + } |
| 72 | + |
| 73 | + // return the suppliers with changed values. Returns null if no values have changed |
| 74 | + private Map<Supplier<ProfileFile>, ProfileFile> changedSupplierValues(Map<Supplier<ProfileFile>, ProfileFile> currentValues) { |
| 75 | + Map<Supplier<ProfileFile>, ProfileFile> changedValues = null; |
| 76 | + for (ProfileFileSupplier supplier : suppliers) { |
| 77 | + ProfileFile next = supplier.get(); |
| 78 | + ProfileFile prev = currentValues.get(supplier); |
| 79 | + // we ONLY care about if the reference has changed, we don't care about object equality here |
| 80 | + if (prev != next) { |
| 81 | + if (changedValues == null) { |
| 82 | + // changed values must also preserve supplier order |
| 83 | + changedValues = new LinkedHashMap<>(); |
| 84 | + } |
| 85 | + changedValues.put(supplier, next); |
| 86 | + } |
| 87 | + } |
| 88 | + return changedValues; |
| 89 | + } |
| 90 | + |
| 91 | + private static final class SupplierState { |
| 92 | + final Map<Supplier<ProfileFile>, ProfileFile> values; |
| 93 | + final ProfileFile aggregate; |
| 94 | + |
| 95 | + private SupplierState(Map<Supplier<ProfileFile>, ProfileFile> values, ProfileFile aggregate) { |
| 96 | + this.values = values; |
| 97 | + this.aggregate = aggregate; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments