Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | package org.onosproject.store.service; |
| 17 | |
| 18 | import java.util.List; |
| 19 | import java.util.function.Function; |
| 20 | |
| 21 | import org.onosproject.store.primitives.MapUpdate; |
| 22 | import org.onosproject.store.primitives.TransactionId; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.Lists; |
| 26 | |
| 27 | /** |
| 28 | * Collection of map updates to be committed atomically. |
| 29 | * |
| 30 | * @param <K> key type |
| 31 | * @param <V> value type |
| 32 | */ |
| 33 | public class MapTransaction<K, V> { |
| 34 | |
| 35 | private final TransactionId transactionId; |
| 36 | private final List<MapUpdate<K, V>> updates; |
| 37 | |
| 38 | public MapTransaction(TransactionId transactionId, List<MapUpdate<K, V>> updates) { |
| 39 | this.transactionId = transactionId; |
| 40 | this.updates = ImmutableList.copyOf(updates); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the transaction identifier. |
| 45 | * |
| 46 | * @return transaction id |
| 47 | */ |
| 48 | public TransactionId transactionId() { |
| 49 | return transactionId; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the list of map updates. |
| 54 | * |
| 55 | * @return map updates |
| 56 | */ |
| 57 | public List<MapUpdate<K, V>> updates() { |
| 58 | return updates; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Maps this instance to another {@code MapTransaction} with different key and value types. |
| 63 | * |
| 64 | * @param keyMapper function for mapping key types |
| 65 | * @param valueMapper function for mapping value types |
| 66 | * @return newly typed instance |
| 67 | * |
| 68 | * @param <S> key type of returned instance |
| 69 | * @param <T> value type of returned instance |
| 70 | */ |
| 71 | public <S, T> MapTransaction<S, T> map(Function<K, S> keyMapper, Function<V, T> valueMapper) { |
| 72 | return new MapTransaction<>(transactionId, Lists.transform(updates, u -> u.map(keyMapper, valueMapper))); |
| 73 | } |
| 74 | } |