blob: acedf176bc6048e6b15c9ad90a866a25df62e9f1 [file] [log] [blame]
Madan Jampani74da78b2016-02-09 21:18:36 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani74da78b2016-02-09 21:18:36 -08003 *
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 */
16package org.onosproject.store.service;
17
18import java.util.List;
19import java.util.function.Function;
20
21import org.onosproject.store.primitives.MapUpdate;
22import org.onosproject.store.primitives.TransactionId;
23
Madan Jampani39fff102016-02-14 13:17:28 -080024import com.google.common.base.MoreObjects;
Madan Jampani74da78b2016-02-09 21:18:36 -080025import com.google.common.collect.ImmutableList;
26import 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 */
34public 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 Jampani39fff102016-02-14 13:17:28 -080062 @Override
63 public String toString() {
64 return MoreObjects.toStringHelper(getClass())
65 .add("transactionId", transactionId)
66 .add("updates", updates)
67 .toString();
68 }
69
Madan Jampani74da78b2016-02-09 21:18:36 -080070 /**
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}