Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.consistent.impl; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.onosproject.store.service.DatabaseUpdate; |
| 21 | import org.onosproject.store.service.Transaction; |
| 22 | |
| 23 | import com.google.common.collect.ImmutableList; |
| 24 | |
| 25 | /** |
| 26 | * A Default transaction implementation. |
| 27 | */ |
| 28 | public class DefaultTransaction implements Transaction { |
| 29 | |
| 30 | private final long transactionId; |
| 31 | private final List<DatabaseUpdate> updates; |
| 32 | private final State state; |
| 33 | private final long lastUpdated; |
| 34 | |
| 35 | public DefaultTransaction(long transactionId, List<DatabaseUpdate> updates) { |
| 36 | this(transactionId, updates, State.PREPARING, System.currentTimeMillis()); |
| 37 | } |
| 38 | |
| 39 | private DefaultTransaction(long transactionId, List<DatabaseUpdate> updates, State state, long lastUpdated) { |
| 40 | this.transactionId = transactionId; |
| 41 | this.updates = ImmutableList.copyOf(updates); |
| 42 | this.state = state; |
| 43 | this.lastUpdated = lastUpdated; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public long id() { |
| 48 | return transactionId; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public List<DatabaseUpdate> updates() { |
| 53 | return updates; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public State state() { |
| 58 | return state; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Transaction transition(State newState) { |
| 63 | return new DefaultTransaction(transactionId, updates, newState, System.currentTimeMillis()); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public long lastUpdated() { |
| 68 | return lastUpdated; |
| 69 | } |
| 70 | } |