Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [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.Objects; |
| 20 | import java.util.function.Function; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.google.common.collect.Lists; |
| 25 | import org.onosproject.store.primitives.TransactionId; |
| 26 | |
| 27 | /** |
| 28 | * Collection of transaction updates to be applied atomically. |
| 29 | * |
| 30 | * @param <T> log record type |
| 31 | */ |
| 32 | public class TransactionLog<T> { |
| 33 | private final TransactionId transactionId; |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 34 | private final long version; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 35 | private final List<T> records; |
| 36 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 37 | public TransactionLog(TransactionId transactionId, long version, List<T> records) { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 38 | this.transactionId = transactionId; |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 39 | this.version = version; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 40 | 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 Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 53 | * Returns the transaction lock version. |
| 54 | * |
| 55 | * @return the transaction lock version |
| 56 | */ |
| 57 | public long version() { |
| 58 | return version; |
| 59 | } |
| 60 | |
| 61 | /** |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 62 | * 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 Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 89 | .add("version", version) |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 90 | .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 Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 103 | return new TransactionLog<>(transactionId, version, Lists.transform(records, mapper::apply)); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 104 | } |
| 105 | } |