blob: 6d7cbd4e42ac68bb523a6fb4acb24d9a62314290 [file] [log] [blame]
Jordan Halterman948d6592017-04-20 17:18:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman948d6592017-04-20 17:18:24 -07003 *
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.Objects;
20import java.util.function.Function;
21
22import com.google.common.base.MoreObjects;
23import com.google.common.collect.ImmutableList;
24import com.google.common.collect.Lists;
25import org.onosproject.store.primitives.TransactionId;
26
27/**
28 * Collection of transaction updates to be applied atomically.
29 *
30 * @param <T> log record type
31 */
32public class TransactionLog<T> {
33 private final TransactionId transactionId;
Jordan Halterman5f97a302017-04-26 23:41:31 -070034 private final long version;
Jordan Halterman948d6592017-04-20 17:18:24 -070035 private final List<T> records;
36
Jordan Halterman5f97a302017-04-26 23:41:31 -070037 public TransactionLog(TransactionId transactionId, long version, List<T> records) {
Jordan Halterman948d6592017-04-20 17:18:24 -070038 this.transactionId = transactionId;
Jordan Halterman5f97a302017-04-26 23:41:31 -070039 this.version = version;
Jordan Halterman948d6592017-04-20 17:18:24 -070040 this.records = ImmutableList.copyOf(records);
41 }
42
43 /**
44 * Returns the transaction identifier.
45 *
46 * @return transaction id
47 */
48 public TransactionId transactionId() {
49 return transactionId;
50 }
51
52 /**
Jordan Halterman5f97a302017-04-26 23:41:31 -070053 * Returns the transaction lock version.
54 *
55 * @return the transaction lock version
56 */
57 public long version() {
58 return version;
59 }
60
61 /**
Jordan Halterman948d6592017-04-20 17:18:24 -070062 * Returns the list of transaction log records.
63 *
64 * @return a list of transaction log records
65 */
66 public List<T> records() {
67 return records;
68 }
69
70 @Override
71 public boolean equals(Object object) {
72 if (object instanceof TransactionLog) {
73 TransactionLog that = (TransactionLog) object;
74 return this.transactionId.equals(that.transactionId)
75 && this.records.equals(that.records);
76 }
77 return false;
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(transactionId, records);
83 }
84
85 @Override
86 public String toString() {
87 return MoreObjects.toStringHelper(getClass())
88 .add("transactionId", transactionId)
Jordan Halterman5f97a302017-04-26 23:41:31 -070089 .add("version", version)
Jordan Halterman948d6592017-04-20 17:18:24 -070090 .add("records", records)
91 .toString();
92 }
93
94 /**
95 * Maps this instance to another {@code MapTransaction} with different key and value types.
96 *
97 * @param mapper function for mapping record types
98 * @return newly typed instance
99 *
100 * @param <U> record type of returned instance
101 */
102 public <U> TransactionLog<U> map(Function<T, U> mapper) {
Jordan Halterman5f97a302017-04-26 23:41:31 -0700103 return new TransactionLog<>(transactionId, version, Lists.transform(records, mapper::apply));
Jordan Halterman948d6592017-04-20 17:18:24 -0700104 }
105}