Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present 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.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; |
| 34 | private final List<T> records; |
| 35 | |
| 36 | public TransactionLog(TransactionId transactionId, List<T> records) { |
| 37 | this.transactionId = transactionId; |
| 38 | this.records = ImmutableList.copyOf(records); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the transaction identifier. |
| 43 | * |
| 44 | * @return transaction id |
| 45 | */ |
| 46 | public TransactionId transactionId() { |
| 47 | return transactionId; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the list of transaction log records. |
| 52 | * |
| 53 | * @return a list of transaction log records |
| 54 | */ |
| 55 | public List<T> records() { |
| 56 | return records; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public boolean equals(Object object) { |
| 61 | if (object instanceof TransactionLog) { |
| 62 | TransactionLog that = (TransactionLog) object; |
| 63 | return this.transactionId.equals(that.transactionId) |
| 64 | && this.records.equals(that.records); |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int hashCode() { |
| 71 | return Objects.hash(transactionId, records); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String toString() { |
| 76 | return MoreObjects.toStringHelper(getClass()) |
| 77 | .add("transactionId", transactionId) |
| 78 | .add("records", records) |
| 79 | .toString(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Maps this instance to another {@code MapTransaction} with different key and value types. |
| 84 | * |
| 85 | * @param mapper function for mapping record types |
| 86 | * @return newly typed instance |
| 87 | * |
| 88 | * @param <U> record type of returned instance |
| 89 | */ |
| 90 | public <U> TransactionLog<U> map(Function<T, U> mapper) { |
| 91 | return new TransactionLog<>(transactionId, Lists.transform(records, mapper::apply)); |
| 92 | } |
| 93 | } |