| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright 2011 Google Inc. |
| 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 |
| 17 package com.google.ipc.invalidation.ticl; |
| 18 |
| 19 import com.google.ipc.invalidation.ticl.proto.Client.RunStateP; |
| 20 import com.google.ipc.invalidation.util.Marshallable; |
| 21 |
| 22 /** |
| 23 * An abstraction that keeps track of whether the caller is started or stopped a
nd only allows |
| 24 * the following transitions NOT_STARTED -> STARTED -> STOPPED. This class is th
read-safe. |
| 25 * |
| 26 */ |
| 27 public class RunState implements Marshallable<RunStateP> { |
| 28 /** Current run state ({@link RunStateP}). */ |
| 29 private Integer currentState; |
| 30 private Object lock = new Object(); |
| 31 |
| 32 /** Constructs a new instance in the {@code NOT_STARTED} state. */ |
| 33 public RunState() { |
| 34 currentState = RunStateP.State.NOT_STARTED; |
| 35 } |
| 36 |
| 37 /** Constructs a new instance with the state given in {@code runState}. */ |
| 38 RunState(RunStateP runState) { |
| 39 this.currentState = runState.getState(); |
| 40 } |
| 41 |
| 42 /** |
| 43 * Marks the current state to be STARTED. |
| 44 * <p> |
| 45 * REQUIRES: Current state is NOT_STARTED. |
| 46 */ |
| 47 public void start() { |
| 48 synchronized (lock) { |
| 49 if (currentState != RunStateP.State.NOT_STARTED) { |
| 50 throw new IllegalStateException("Cannot start: " + currentState); |
| 51 } |
| 52 currentState = RunStateP.State.STARTED; |
| 53 } |
| 54 } |
| 55 |
| 56 /** |
| 57 * Marks the current state to be STOPPED. |
| 58 * <p> |
| 59 * REQUIRES: Current state is STARTED. |
| 60 */ |
| 61 public void stop() { |
| 62 synchronized (lock) { |
| 63 if (currentState != RunStateP.State.STARTED) { |
| 64 throw new IllegalStateException("Cannot stop: " + currentState); |
| 65 } |
| 66 currentState = RunStateP.State.STOPPED; |
| 67 } |
| 68 } |
| 69 |
| 70 /** |
| 71 * Returns true iff {@link #start} has been called on this but {@link #stop} h
as not been called. |
| 72 */ |
| 73 public boolean isStarted() { |
| 74 synchronized (lock) { |
| 75 return currentState == RunStateP.State.STARTED; |
| 76 } |
| 77 } |
| 78 |
| 79 /** Returns true iff {@link #start} and {@link #stop} have been called on this
object. */ |
| 80 public boolean isStopped() { |
| 81 synchronized (lock) { |
| 82 return currentState == RunStateP.State.STOPPED; |
| 83 } |
| 84 } |
| 85 |
| 86 @Override |
| 87 public RunStateP marshal() { |
| 88 return RunStateP.create(currentState); |
| 89 } |
| 90 |
| 91 @Override |
| 92 public String toString() { |
| 93 return "<RunState: " + currentState + ">"; |
| 94 } |
| 95 } |
| OLD | NEW |