Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 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 | |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 24 | import com.google.common.base.MoreObjects; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
| 26 | import com.google.common.collect.Lists; |
| 27 | |
| 28 | /** |
| 29 | * Collection of map updates to be committed atomically. |
| 30 | * |
| 31 | * @param <K> key type |
| 32 | * @param <V> value type |
| 33 | */ |
| 34 | public class MapTransaction<K, V> { |
| 35 | |
| 36 | private final TransactionId transactionId; |
| 37 | private final List<MapUpdate<K, V>> updates; |
| 38 | |
| 39 | public MapTransaction(TransactionId transactionId, List<MapUpdate<K, V>> updates) { |
| 40 | this.transactionId = transactionId; |
| 41 | this.updates = ImmutableList.copyOf(updates); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns the transaction identifier. |
| 46 | * |
| 47 | * @return transaction id |
| 48 | */ |
| 49 | public TransactionId transactionId() { |
| 50 | return transactionId; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the list of map updates. |
| 55 | * |
| 56 | * @return map updates |
| 57 | */ |
| 58 | public List<MapUpdate<K, V>> updates() { |
| 59 | return updates; |
| 60 | } |
| 61 | |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 62 | @Override |
| 63 | public String toString() { |
| 64 | return MoreObjects.toStringHelper(getClass()) |
| 65 | .add("transactionId", transactionId) |
| 66 | .add("updates", updates) |
| 67 | .toString(); |
| 68 | } |
| 69 | |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Maps this instance to another {@code MapTransaction} with different key and value types. |
| 72 | * |
| 73 | * @param keyMapper function for mapping key types |
| 74 | * @param valueMapper function for mapping value types |
| 75 | * @return newly typed instance |
| 76 | * |
| 77 | * @param <S> key type of returned instance |
| 78 | * @param <T> value type of returned instance |
| 79 | */ |
| 80 | public <S, T> MapTransaction<S, T> map(Function<K, S> keyMapper, Function<V, T> valueMapper) { |
| 81 | return new MapTransaction<>(transactionId, Lists.transform(updates, u -> u.map(keyMapper, valueMapper))); |
| 82 | } |
| 83 | } |